Saturday 27 May 2017

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.



via metahamza

No comments:

Post a Comment