Skip to content
This repository was archived by the owner on Oct 17, 2020. It is now read-only.

Commit 6128585

Browse files
committed
fix(context): document context
1 parent 773b399 commit 6128585

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

content/GraphQL-Binding/02-API-Reference.md

+43-6
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ delegateSubscription(fieldName: string, args?: {
193193
```js
194194
const Subscription = {
195195
userCreatedSub: (parent, args, context, info) => {
196-
return userServiceDelegate.delegateSubscription(
196+
return delegate.delegateSubscription(
197197
`userCreated`,
198198
args,
199199
// passing the info AST from the parent resolver
@@ -245,11 +245,13 @@ Can be used to ensure that specific fields are included in the `info` object pas
245245
**Example: Ensure the binding fetches the `email` field from the `User`**.
246246

247247
```js
248-
import userBinding from 'graphql-binding-users'
248+
import ExampleBinding from 'graphql-binding-example'
249249
import { addFragmentToInfo } from 'graphql-binding'
250250

251+
const exampleBinding = new ExampleBinding()
252+
251253
async findUser(parent, args, context, info) {
252-
const user = await userBinding.query.user({ id: args.id }, addFragmentToInfo(info, 'fragment EnsureEmail on User { email }'), { context })
254+
const user = await exampleBinding.query.user({ id: args.id }, addFragmentToInfo(info, 'fragment EnsureEmail on User { email }'), { context })
253255

254256
return user
255257
}
@@ -264,10 +266,45 @@ The `Options` type has two fields:
264266

265267
### Transforms
266268

267-
Coming soon
269+
Schema transforms are a tool for making modified copies of GraphQLSchema objects, while preserving the possibility of delegating back to original schema.
270+
271+
Transforms are useful when working with remote schemas, building GraphQL gateways, and working with GraphQL microservices.
272+
273+
More information on `transforms` can be found [here](https://www.apollographql.com/docs/graphql-tools/schema-transforms.html)
268274

269-
<!-- > [Learn more about the `info` object.](https://blog.graph.cool/graphql-server-basics-demystifying-the-info-argument-in-graphql-resolvers-6f26249f613a) -->
270275

271276
### Context
272277

273-
Coming soon
278+
In GraphQL APIs, context can be provided to every resolver that holds important contextual information like the currently logged in user, or access to a database. Utilizing context is extremely crucial for bindings to GraphQL servers that may inherit context from a HTTP request or encode user-specific information to the request.
279+
280+
In the example below we pass through context from the incoming GraphQL resolver to the underlying binding.
281+
282+
```js
283+
import ExampleBinding from 'graphql-binding-example'
284+
import { addFragmentToInfo } from 'graphql-binding'
285+
286+
const exampleBinding = new ExampleBinding()
287+
288+
async findUser(parent, args, context, info) {
289+
const user = await exampleBinding.query.user({ id: args.id }, info, { context })
290+
return user
291+
}
292+
```
293+
294+
You could also stamp these values directly:
295+
296+
```js
297+
import ExampleBinding from 'graphql-binding-example'
298+
import { addFragmentToInfo } from 'graphql-binding'
299+
300+
const exampleBinding = new ExampleBinding()
301+
302+
async findUser(parent, args, context, info) {
303+
// For this request hardcode the user you want to call this api on behalf of.
304+
const user = await exampleBinding.query.user({ id: args.id }, info, { context: {
305+
...context,
306+
'userid': '4792742323',
307+
}})
308+
return user
309+
}
310+
```

0 commit comments

Comments
 (0)