Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does one set up (database, or other) context in a GraphQL resolver?

The GraphQL docs give an example of a resolver function that accepts an argument called "context".

They have to say this about it -

context A value which is provided to every resolver and holds important contextual information like the currently logged in user, or access to a database.

And their code example looks like this -

Query: {
  human(obj, args, context) {
    return context.db.loadHumanByID(args.id).then(
      userData => new Human(userData)
    )
  }
}

This seems to me a perfectly natural pattern to want database access inside a resolver function, and unsurprisingly it is what I need to do.

This database context is not set up automatically, obviously, since GraphQL is completely agnostic about your particular means of data persistence.

My question is, how does one configure this context to provide one's specific db interface? I can't find mention of this in the tutorials/docs, or anywhere really.

like image 920
metahamza Avatar asked May 27 '17 18:05

metahamza


People also ask

How does GraphQL connect to database?

Your GraphQL API can interact with any combination of data sources. Apollo provides a DataSource class that we can extend to handle interaction logic for a particular type of data source. In this section, we'll extend DataSource to connect both a REST API and a SQL database to Apollo Server.

How does resolver work in GraphQL?

A resolver is a function that resolves a value for a type or field in a schema. Resolvers can return objects or scalars like Strings, Numbers, Booleans, etc. If an Object is returned, execution continues to the next child field. If a scalar is returned (typically at a leaf node), execution completes.

What is resolver in GraphQL?

Resolver is a collection of functions that generate response for a GraphQL query. In simple terms, a resolver acts as a GraphQL query handler. Every resolver function in a GraphQL schema accepts four positional arguments as given below −

How many arguments does every resolver function in a GraphQL schema accept?

Every resolver function in a GraphQL schema accepts four positional arguments as given below − //resolver function with no parameters and returning string greeting: () => { return "hello from TutorialsPoint !!!"

What is resolveparams in GraphQL?

BE AWARE: that all arguments (source, args, context, info) are passed inside one argument called as resolveParams ( rp for brevity in the code). description public description which will be passed to graphql schema and will be available via introspection deprecationReason if you want to hide field from schema, but leave it working for old clients

How do I query my database in graphiql?

A GraphiQL browser launches at localhost:5001 in which you can query your database. Try this query: Feel free to try other queries using the GraphQL Builder tab in GraphiQL. Go to the Code Exporter tab in the GraphiQL browser (under the Beta tab).


2 Answers

You may pass the context to graphql when calling the graphql function.
It is specified here.

Here is the flow definition of the graphql function:

graphql(
  schema: GraphQLSchema,
  requestString: string,
  rootValue?: ?any,
  contextValue?: ?any, // Arbitrary context
  variableValues?: ?{[key: string]: any},
  operationName?: ?string
): Promise<GraphQLResult>
like image 200
yachaka Avatar answered Sep 28 '22 08:09

yachaka


context is defined when you set up your server. I couldn't see it in the docs either.

graphqlExpress(req => {
  return {
    schema: makeExecutableSchema({
      typeDefs: schema.ast,
      resolvers,
      logger
    }),
    context: {
      db: mongodb.MongoClient.connect(...)
    }
  };
})
like image 26
otissv Avatar answered Sep 28 '22 07:09

otissv