簡體   English   中英

Apollo-Client + Express-Graphql 后端 - Session 不存在?

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

我想我一定沒有得到什么。 我正在嘗試將我的 apollo-client localhost:8000連接到我的后端localhost:4000 ,客戶端和服務器位於不同的域中。 我在前端使用 apollo ( apollo-boost ),在后端使用express-graphql (不是 (!) apollo-server )。

當我從客戶端向服務器發送一些登錄數據時,我得到一個響應:用戶 object,或錯誤等。這很好。 但是,用戶實際上從未在服務器上登錄(當我簽入 graphiql 並請求用戶 object 時,它始終為空)。

我不知道這是否是 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",
})

所以我在 Apollo 客戶端中包含了credentials: "include",而不是在 httpLink 中 - 實際上刪除了那個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM