有人可以帮我吗,我的设置是在Apollo 2.0之前的,我有一个server.js,其中我使用了express和graphql-server-express 我有一个仅HTTP的cookie会话,当用户登录时,我将jwt令牌设置为cookie,并且在浏览器中将其设置为仅http。 在后续请求中,我验证浏览器返回的cookie。一切都很好,我可以访问 来自其他任何解析器中req.session.token的令牌,并验证cookie会话中保存的jwt令牌。
server.js
import express from 'express';
import { graphqlExpress, graphiqlExpress } from 'graphql-server-express';
import { ApolloEngine } from 'apollo-engine';
import bodyParser from 'body-parser';
import cors from 'cors';
import cookieSession from 'cookie-session';
import schema from './schema/';
const server = express();
server.use(
cookieSession({
name: 'session',
keys: 'k1,k2',
maxAge: 30 * 60 * 1000,
domain: '.mydomain.com',
path: '/',
}),
);
const corsOptions = {
origin: 'http://local.mydomain.com:3000',
credentials: true,
methods: ['GET', 'PUT', 'POST', 'OPTIONS'],
};
server.use(cors(corsOptions));
server.use(
'/graphql',
bodyParser.json(),
graphqlExpress(req => ({
schema,
tracing: true,
context: { req },
})),
);
if (process.env.NODE_ENV !== 'production') {
server.use('/graphiql',graphiqlExpress({endpointURL: '/graphql'}));
}
const engine = new ApolloEngine({
apiKey: engineConfig.apiKey,
});
engine.listen(
{
port: 3000,
graphqlPaths: ['/graphql'],
expressApp: server,
},
() => {console.log('GraphiQL is now running');},
);
authenticateResolver.js
const authenticateResolver = {
Query: {
authenticate: async (root, args, context) => {
const { req } = context;
const auth = `Basic ${Buffer.from(`${args.username}:${args.password}`).toString('base64')}`;
const axiosResponse = await axios.post("localhot:8080/login, 'true',
{
headers: {
Authorization: auth,
},
});
if (axiosResponse.status === 200 && axiosResponse.data.token) {
req.session.token = axiosResponse.data.token;
}
return {
status: 200,
};
},
但是当我升级到Apollo 2.0时,我的server.js代码更改了,authenticateResolver保持原样。 由于未设置cookie会话,我现在无法在任何后续请求中访问req.session.token。 当我在Chrome中打开开发者工具时,在调用身份验证时看不到设置的Cookie。 我在这里做错了什么?
server.js#在Apollo 2.0升级之后
import express from 'express';
import { ApolloServer, gql } from 'apollo-server-express';
import cors from 'cors';
import cookieSession from 'cookie-session';
import { mergedTypes, resolvers } from './schema/';
const server = express();
server.use(
cookieSession({
name: 'session',
keys: 'k1,k2',
maxAge: 30 * 60 * 1000,
domain: '.mydomain.com',
path: '/',
}),
);
const corsOptions = {
origin: 'http://local.mydomain.com:3000',
credentials: true,
methods: ['GET', 'PUT', 'POST', 'OPTIONS'],
};
server.use(cors(corsOptions));
server.listen({ port: 3000 }, () => {
console.log('Server ready');
console.log('Try your health check at: .well-known/apollo/app-health');
});
const apollo = new ApolloServer({
typeDefs: gql`
${mergedTypes}
`,
resolvers,
engine: false,
context: ({ req }) => ({ req }),
});
apollo.applyMiddleware({
server
});
答案 0 :(得分:6)
是的,如果您在graphql操场上查看,则有一个设置选项,如果单击该选项,您可以观察到一些设置,其中之一是 “ request.credentials”:“省略”只需将其更改为 “ request.credentials”:“ include”并保存设置,它现在应该可以工作
我的代码也如下所示,
const app = express();
app.use(
cookieSession({
name: 'session',
keys: corsConfig.cookieSecret.split(','),
maxAge: 60 * 60 * 1000,
domain: corsConfig.cookieDomain,
path: '/',
}),
);
const corsOptions = {
origin: corsConfig.corsWhitelist.split(','),
credentials: true,
methods: ['GET', 'PUT', 'POST', 'OPTIONS'],
};
app.use(cors(corsOptions));
const apollo = new ApolloServer({
typeDefs: gql`
${mergedTypes}
`,
resolvers,
engine: false,
context: ({ req }) => ({ req }),
tracing: true,
debug: !process.env.PRODUCTION,
introspection: !process.env.PRODUCTION,
});
apollo.applyMiddleware({
app,
path: '/',
cors: corsOptions,
});
app.listen({ port: engineConfig.port }, () => {
console.log(' - Server ready');
console.log('Try your health check at: .well-known/apollo/app-health');
});