Apollo-Client + Express-Graphql 后端 - 会话不会持续存在?

Apollo-Client + Express-Graphql Backend - Session does not persist?

我想我一定是没有得到什么。我正在尝试将我的 apollo-client localhost:8000 连接到我的后端 localhost:4000,客户端和服务器位于不同的域中。我在前端使用 apollo (apollo-boost),在后端使用 express-graphql (not (!) apollo-server).

当我从客户端向服务器发送一些登录数据时,我得到了一个响应:一个用户对象,或者错误等等。这很好。但是,用户实际上从未登录过服务器(当我签入 graphiql 并请求用户对象时,它始终为空)。

我不知道这是否是一个 CORS 问题,因为过去当我在同一个域中同时拥有客户端和服务器时一切正常吗?

这是客户端代码:

import ApolloClient from "apollo-boost"
import fetch from "isomorphic-fetch"
import { HttpLink } from "apollo-link-http"
import { InMemoryCache } from "apollo-cache-inmemory"

const cache = new InMemoryCache()

const link = new HttpLink({
  // uri: "http://localhost:4000/graphql",
  credentials: "include",
})


export const client = new ApolloClient({
  cache,
  fetch,
  link,
  dataIdFromObject: o => o.id,
  uri: "http://localhost:4000/graphql",
})

服务器上的相关位是:

app.use(
  session({
    resave: true,
    saveUninitialized: true,
    secret: 'whatever',
    cookie: {
      secure: false
    },
    store: new MongoStore({
      url: MONGO_URI,
      autoReconnect: true
    })
  })
);

var corsOptions = {
  origin: 'http://localhost:8000',
  credentials: true
};
app.use(cors(corsOptions));
app.use(passport.initialize());
app.use(passport.session());

// Instruct Express to pass on any request made to the '/graphql' route
// to the GraphQL instance.
app.use(
  '/graphql',
  expressGraphQL({
    schema,
    graphiql: true
  })
);

他们的 API 好像有点变了。 我通过在客户端执行此操作来修复它:

import ApolloClient from "apollo-boost"
import fetch from "isomorphic-fetch"
import { InMemoryCache } from "apollo-cache-inmemory"

const cache = new InMemoryCache()

export const client = new ApolloClient({
  cache,
  fetch,
  link,
  credentials: "include",
  dataIdFromObject: o => o.id,
  uri: "http://localhost:4000/graphql",
})

所以我将 credentials: "include", 包含在 Apollo Client 中,而不是在 httpLink 中 - 实际上删除了那个。