Skip to content

Instantly share code, notes, and snippets.

@steveluscher
Last active September 26, 2016 13:01
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save steveluscher/ffc1dfefbb10ad280c8a4c520a5c201c to your computer and use it in GitHub Desktop.
Save steveluscher/ffc1dfefbb10ad280c8a4c520a5c201c to your computer and use it in GitHub Desktop.
GraphQL 0.6.0 migration guide

GraphQL 0.6.0 migration guide

This version of graphql-js introduces a breaking change to the method signature of the resolve() method.

Previously, resolve() had this method signature:

type GraphQLResolveInfo = {
  fieldName: string,
  fieldASTs: Array<Field>,
  returnType: GraphQLOutputType,
  parentType: GraphQLCompositeType,
  schema: GraphQLSchema,
  fragments: { [fragmentName: string]: FragmentDefinition },
  rootValue: mixed,
  operation: OperationDefinition,
  variableValues: { [variableName: string]: mixed },
};

resolve(
  source: mixed,
  args: {[argName: string]: mixed},
  info: GraphQLResolveInfo
): mixed

Typically, when you wanted to thread request or other context-specific information down to the resolve functions, you might have included it in rootValue when calling graphql():

// Pre 0.6.0
graphql(req => {
  return {
    rootValue: {viewer: req.userID},
    schema,
  };
});

Now there exists a bonafide context property that you can set for exactly this purpose:

// New in 0.6.0
graphql(req => {
  return {
    context: {viewer: req.userID},  // NEW
    schema,
  };
});

graphql-js 0.6.0 will make this context prop available to each one of your resolve() methods as the third argument:

resolve(
  source: mixed,
  args: {[argName: string]: mixed},
  context: mixed,  // NEW
  info: GraphQLResolveInfo  // MOVED
): mixed

Note that the info argument has moved to the fourth position. Anyone previously making use of the info argument will have to update the method signatures of their resolve() methods.

@ryancole
Copy link

Now that there's this context property, what are the different intended use cases for context and rootValue? Why wasn't the user info sufficient being in rootValue and now that it is called context fixes? I guess I'm more curious what rootValue is intended for that should not go in context?

Copy link

ghost commented May 30, 2016

Does this change apply to mutateAndGetPayload too?

@helfer
Copy link

helfer commented Jun 8, 2016

@steveluscher: Just noticed this excellent gist and thought it might be useful to know that this change actually happened with v0.5.0 already.

The main change from v0.5.0 to v0.6.0 was the addition of experimental schema directives, but that requires no code changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment