我正在使用GraphQL并尝试在解析器层中获取请求和响应对象。目前,我正在使用express-graphql。我遵循了有关apollo-server的参考,并尝试应用相同的
https://github.com/apollographql/apollo-server/issues/420并且 https://graphql.org/learn/execution/
但是,在解析器层中,我以不匹配的顺序获取值。 参数obj具有获取数据所需的输入,args具有请求和响应,上下文具有fieldNodes,路径,模式等,并且信息显示为未定义。
代码:
index.js // imports rootSchema.js and resolver.js => rootSchema buit using buildSchema
app.use("/api", (request, response)=>{
return graphqlhttp({
schema: rootSchema,
rootValue : rootResolver,
graphiql : appConfig.graphiQlEnabled,
context : {
request: {request, response},
test: 'Example context value'
}
})(request, response);
});
//resolver.js
const rootResolver = {
login: async (obj, args, context, info) => {
console.log("Obj: ", obj);
console.log("Args: ", args);
console.log("Context: ", context);
console.log("Info: ", info);
return "Amen!";
//return await loginService.auth(credentials, context);
},
}