Graphql-js: rootValue vs contextValue

Created on 2 Mar 2017  路  3Comments  路  Source: graphql/graphql-js

Can you give guidelines and usage examples to clarify appropriate uses of rootValue and contextValue, please?

Thanks

Most helpful comment

Lots of great answers here, so I'll close this issue.

Generally, the context value is information that is useful at any point during query execution. A canonical example is the current logged in user. The root value represents the "top" of your metaphorical graph of data, and is useful to include functions or data to help resolve the root fields in your schema.

All 3 comments

READ UPDATE: ...

Would be nice to know the "standard" my current goto is

"The thing that expresses my database or collection of end points"

rootValue = {
  bookshelf: bookshelfConnection,
  knex: knexObject,
  redis: redisConnection
}

The context value is constructed from the request, so mostly comprises of things user specific

ctx = {
  get user() { return req.user }
  get session() { return req.session }
  set session(s) { req.session = s }
}

I use the getter setters, because I wasn't exactly clear on how sessions were saved and handled, and if the session object got overridden, it would be propagated back to the req object, just in case... IT may not be need.

EDIT: -> I misspoke here, my appolgies...

The above is correct except that rootValue, is null in all of my graph queries, ctx.db was assigned rootValue

"rootValue" is the previous node in a graph, it's the answer to "what came before me."

I made the mistake early on of not understanding the role of rootValue and wrote code that used the variable name rootValue...

So the simple answer is, it seems that for simple graphs, the rootValue can be null, if all the data is being fetched, then throw the db/collection/external resources and current session data into into the ctx argument.

This is more what my context actually looks like

ctx = {
  get user() { return req.user }
  get session() { return req.session }
  set session(s) { req.session = s }
  db: {
    bookshelf: bookshelfConnection,
    knex: knexObject,
    redis: redisConnection
  }
}

Also the graphiql interface this was not documented well? IIRC, but the configuration object can be a function, which gets the req argument, I use another middleware to compose the schema and context, because I dog food the schema for "all access to external databases" I wrote a session store for graphql, there are various other "root level things" like creating new users, or authorizing users empowered by the graph..

app.use('/graphql', graphqlHTTP(
  (req) => {
    return {
      graphiql: true,
      schema: req['graphql'].schema,
      context: req['graphql'].context
    };
  })
);

Lots of great answers here, so I'll close this issue.

Generally, the context value is information that is useful at any point during query execution. A canonical example is the current logged in user. The root value represents the "top" of your metaphorical graph of data, and is useful to include functions or data to help resolve the root fields in your schema.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

matthewgertner picture matthewgertner  路  4Comments

galki picture galki  路  3Comments

pranshuchittora picture pranshuchittora  路  3Comments

thomasdingemanse picture thomasdingemanse  路  4Comments

sudheerj picture sudheerj  路  3Comments