Skip to content

Set default value for contextValue #365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
mattkrick opened this issue Apr 22, 2016 · 4 comments
Closed

Set default value for contextValue #365

mattkrick opened this issue Apr 22, 2016 · 4 comments

Comments

@mattkrick
Copy link
Contributor

Following up from #326 (comment) often times it does make sense that the contextValue is an object. This is because in addition to an authToken, you might have another token (resetPasswordToken, verifyEmailToken, etc) or even something like a socket for subscriptions. However, if you don't need any of those things, it's still nice to call graphql(Schema, query) instead of graphql(Schema, query, null, {}). To keep it that way, execute should default the value to an object here: https://github.com/graphql/graphql-js/blob/master/src/execution/execute.js#L115

Thoughts?

@leebyron
Copy link
Contributor

I'm not sure I follow. If you don't need any of the things you would put in context, why do you want an object? Also, if you want the empty object, is there something wrong with providing that directly?

@mattkrick
Copy link
Contributor Author

Without an implied guarantee of an object we either need to assure it's there:
graphql(Schema, query, null, {})

Or, we need to make sure to code a little more defensively in the resolve functions.
For example, in the previous version, a standard resolve might look like:
resolve(source,args, {rootValue: {authToken}})

This was great because the rootValue is guaranteed to be an object.

If we try something similar without an object present:
resolve(source, args, {authToken}) // Broken

@leebyron
Copy link
Contributor

leebyron commented Apr 25, 2016

I think if you have any usage of context or rootValue in your schema, then it's reasonable to assume you're providing something for them when executing, and so it's not necessary to code defensively in resolution functions.

Also in the previous version where rootValue was used in place of context, this situation still existed since rootValue also does not have a default value and is undefined if not provided, so resolve(source,args, {rootValue: {authToken}}) could still be considered broken if a rootValue was not provided.

@Kosta-Github
Copy link

BTW, the documentation of the contextValue (https://github.com/graphql/graphql-js/blob/master/src/graphql.js#L47) is missing in the comment right above the function graphql() (https://github.com/graphql/graphql-js/blob/master/src/graphql.js#L28-L41)...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants