3

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.

  1. 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.
  2. 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);
  },
}
ashu
  • 1,197
  • 2
  • 14
  • 30
  • From https://github.com/graphql/express-graphql/issues/562 IvanGoncharov writes: We actually have two types of resolvers: 1. specified in GraphQLObjectType fields and they get (parent, args, context, info) 2. resolvers specified on parent itself, in this case, their prototype is (args, context, info) and you can access parent using this. – Rebecca Campbell Jul 02 '20 at 01:02
  • https://stackoverflow.com/questions/48630023/wrong-order-of-graphql-resolver-arguments-root-args-context explains it more clearly – Smartkid Feb 07 '21 at 15:28
  • https://github.com/graphql/express-graphql/issues/473#issuecomment-490956690 provides a solution without graphql-tools – Smartkid Feb 07 '21 at 15:54

0 Answers0