来自 apollo-server-lambda 上下文的 `express` object 未定义
`express` object from apollo-server-lambda context is undefined
我正在关注 this guide 使我的 apollo 服务器 运行 具有 Netlify 功能。它适用于未经身份验证的请求,但当我需要阅读 headers.
时遇到问题
指南说我可以从 express
object 中获取 req
,如该片段所示。
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ event, context, express }) => ({
headers: event.headers,
functionName: context.functionName,
event,
context,
expressRequest: express.req,
}),
});
然而,当我自己尝试这个时,我得到了一个未定义的 express
。
这是我的代码。
const { ApolloServer } = require("apollo-server-lambda");
const accountsGraphQL = require("./accounts.js");
const schema = require("./schema/index.js");
const server = new ApolloServer({
schema,
context: async ({ event, context, express }) => {
console.log(express); // outputs undefined
return {
headers: event.headers,
functionName: context.functionName,
event,
context: await accountsGraphQL.context(express.req), // errors `Context creation failed: Cannot read property 'req' of undefined`
}
},
introspection: true,
playground: true,
});
const handler = server.createHandler();
module.exports = { handler };
无论如何,我通过制作另一个包含 req
和 headers
和 authorization
的对象解决了我的问题。
const { ApolloServer } = require("apollo-server-lambda");
const accountsGraphQL = require("./accounts.js");
const schema = require("./schema/index.js");
const server = new ApolloServer({
schema,
context: async ({ event }) => {
return await accountsGraphQL.context({
req: { headers: { authorization: event.headers.authorization } },
});
},
introspection: true,
playground: true,
});
const handler = server.createHandler();
module.exports = { handler };
我正在关注 this guide 使我的 apollo 服务器 运行 具有 Netlify 功能。它适用于未经身份验证的请求,但当我需要阅读 headers.
时遇到问题指南说我可以从 express
object 中获取 req
,如该片段所示。
const server = new ApolloServer({
typeDefs,
resolvers,
context: ({ event, context, express }) => ({
headers: event.headers,
functionName: context.functionName,
event,
context,
expressRequest: express.req,
}),
});
然而,当我自己尝试这个时,我得到了一个未定义的 express
。
这是我的代码。
const { ApolloServer } = require("apollo-server-lambda");
const accountsGraphQL = require("./accounts.js");
const schema = require("./schema/index.js");
const server = new ApolloServer({
schema,
context: async ({ event, context, express }) => {
console.log(express); // outputs undefined
return {
headers: event.headers,
functionName: context.functionName,
event,
context: await accountsGraphQL.context(express.req), // errors `Context creation failed: Cannot read property 'req' of undefined`
}
},
introspection: true,
playground: true,
});
const handler = server.createHandler();
module.exports = { handler };
无论如何,我通过制作另一个包含 req
和 headers
和 authorization
的对象解决了我的问题。
const { ApolloServer } = require("apollo-server-lambda");
const accountsGraphQL = require("./accounts.js");
const schema = require("./schema/index.js");
const server = new ApolloServer({
schema,
context: async ({ event }) => {
return await accountsGraphQL.context({
req: { headers: { authorization: event.headers.authorization } },
});
},
introspection: true,
playground: true,
});
const handler = server.createHandler();
module.exports = { handler };