Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the intended use cases for the resolve function's context and rootValue parameters?

Tags:

graphql

The 0.5.0 release of graphql-js contains a breaking change to the resolve function - it now receives an additional context parameter.

It looks to me like context is intended to carry user session data. What is the use case for the rootValue parameter, now?

like image 416
Ryan Avatar asked Apr 25 '16 23:04

Ryan


1 Answers

RootValue is the first argument to top-level resolvers, and it's still useful in that function.

The root query/mutation type could technically also be the resolve type of another field in the schema. It would be awkward if it was only possible to provide an input value to that resolve function when accessing it through the non-root type. Here's an example:

type Mutation {
  someMutationField: Query
}

type Query {
  someField: String
}

schema {
  query: Query
  mutation: Mutation
}

Another reason to keep the rootValue: it could be used to merge two GraphQL sub-schemas together into a larger schema, where the sub-schemas reside on different servers. In order to be able to do that, you need to have a way to pass the root value into a top-level resolve function along with the query. The current HTTP transport used for express-graphql doesn't let you do that, but it could easily be added.

Even though I haven't seen anyone use rootValue in this way before, I think it's likely that sooner or later someone will use it that way, or find some other use for it, which is probably why it wasn't removed from GraphQL-JS.

like image 139
helfer Avatar answered Sep 28 '22 03:09

helfer