I am using GraphQL and trying to get request and response object in resolver layer. Currently, I am using express-graphql. I have followed the references for apollo-server and tried to apply the same
https://github.com/apollographql/apollo-server/issues/420 AND https://graphql.org/learn/execution/
However, in resolver layer I am getting values at mismatched orders. Parameter obj has the input that we need to fetch data, args has request and response, context has fieldNodes, paths, schema etc and info is shown as undefined.
- I am not sure if express-graphql follows different ordering or GraphQLHttp sends the parameters in different order to resolvers and if its correct approach to use obj parameter.
- How can I set the headers in resolver layer
Code:
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);
},
}