Express-graphql: How to access the value of parent resolver inside a field resolver?

Created on 20 Feb 2019  路  3Comments  路  Source: graphql/express-graphql

When we are using graphqlHTTP, the first argument passed to the resolve method is actually the parameters passed by the client query not root this is fine for a query resolver. But for a usecase where field resolver needs to know of a value of the parent, how to achieve this?

type Person {
    name: String,
    cityId: String,
    city: City,
}

In the above scenario I would like where city would be a field resolver, and it needs access to cityId which is a property on parent type. I assume this should have been passed as parameter to the field resolver but that is not the case.
Is there a way to achieve this?

question

Most helpful comment

Hi@PavanBahuguni, I hit this problem as well. I followed the graphql.org tutorial which states that the graphqlHTTP() _rootValue_ option takes a resolver function. The error is to assume that it will follow the _(parent, args, context, info)_ convention. As far as I know, this rootValue function cannot pass data down to leaf nodes, so _args_ is your first parameter. The solution is to embed resolvers into the schema by using graphql type construction or a tool like graphql-tools.

IMO it would be a great if graphqlHTTP would take a resolver function as well, following the convention mentioned above.

Thanks to https://marmelab.com/blog/2017/09/06/dive-into-graphql-part-iii-building-a-graphql-server-with-nodejs.html#writing-resolvers for pointing this out.

All 3 comments

@PavanBahuguni Can you please post a simplified code sample?

Hi@PavanBahuguni, I hit this problem as well. I followed the graphql.org tutorial which states that the graphqlHTTP() _rootValue_ option takes a resolver function. The error is to assume that it will follow the _(parent, args, context, info)_ convention. As far as I know, this rootValue function cannot pass data down to leaf nodes, so _args_ is your first parameter. The solution is to embed resolvers into the schema by using graphql type construction or a tool like graphql-tools.

IMO it would be a great if graphqlHTTP would take a resolver function as well, following the convention mentioned above.

Thanks to https://marmelab.com/blog/2017/09/06/dive-into-graphql-part-iii-building-a-graphql-server-with-nodejs.html#writing-resolvers for pointing this out.

@mdjaere You can write your resolvers as JS objects/classes here is more complete example:
https://github.com/IvanGoncharov/swapi-demo/blob/master/src/index.ts

Also, you can use the output of graphql-tools together with express-graphql:

import { makeExecutableSchema } from 'graphql-tools';

const executableSchema = makeExecutableSchema({
  typeDefs,
  resolvers,
});

const app = express();

app.use('/graphql', graphqlHTTP({
  schema: executableSchema,
  graphiql: true
}));

app.listen(4000);
Was this page helpful?
0 / 5 - 0 ratings

Related issues

stevvvn picture stevvvn  路  3Comments

naivefun picture naivefun  路  3Comments

arealmaas picture arealmaas  路  4Comments

violet-day picture violet-day  路  3Comments

ivanhdzd picture ivanhdzd  路  3Comments