Correct the "Context" section of the README.md.

This seems to have been quite dated!
This commit is contained in:
Jesse Rosenberger 2019-02-26 11:06:57 +02:00
parent 33885db0b9
commit 09b4052bf2
No known key found for this signature in database
GPG key ID: C0CCCF81AA6C08D8

View file

@ -99,14 +99,25 @@ See the links above for more details on a specific integration.
## Context
The context is created for each request. The following code snippet shows the creation of a context. The arguments are the `request`, the request, and `h`, the response toolkit.
A request context is available for each request. When `context` is defined as a function, it will be called on each request and receive an object containing a `req` property which represents the request.
By returning an object from the `context` function, it will be available as the third positional paramter of the resolvers:
```js
new ApolloServer({
typeDefs,
resolvers,
context: async ({ request, h }) => {
return { ... };
resolvers: {
Query: {
books: (parent, args, context, info) => {
console.log(context.myProperty); // Will be `true`!
return books;
},
}
},
context: async ({ req }) => {
return {
myProperty: true
};
},
})
```