Add context parameter that passes through the API to resolvers. #82
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds a net/context.Context parameter that is threaded through from
the calling API to any resolver functions. This allows an application
to provide custom, per-request handling when resolving queries.
For example, when working on App Engine, all interactions with the
datastore require a per-request context. Other examples include
authentication, logging, or auditing of graphql operations.
An alternative that was considered was to use an arbitrary, application-
provided interface{} value -- that is, the application could stick
anything in that field and it would be up to the app to handle it. This
is fairly reasonable, however using context.Context has a few other
advantages:
parallelizing and deadlining/cancelling requests. Doing so would
provide a consistent API to developers to also hook into such
operations.
for most HTTP handlers.
Going with an arbitrary interface{} now, but later using context.Context
for its other uses as well would result in redundant mechanisms to provide
external (application) metadata to requests.
Another potential alternative is to specifically provide just the
_http.Request pointer. Many libraries do this and use a global,
synchronized map[_http.Request]metadata lookup table. This would satisfy
the AppEngine requirements and provide a minimal mechanism to provide
additional metadata, but the global LUT is clumsy and, again, if
context.Context were later used to manage subprocessing it would provide
a redundant metadata mechanism.