Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
Add context to the arguments of the extensions function (#441)
Browse files Browse the repository at this point in the history
* Add context to the arguments of the extensions function

* Update the extensions function example in the read me to better reflect changes
  • Loading branch information
corey-clark authored and IvanGoncharov committed Jun 1, 2018
1 parent c48847b commit 41e26f8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
16 changes: 10 additions & 6 deletions README.md
Expand Up @@ -86,7 +86,7 @@ The `graphqlHTTP` function accepts the following options:
`"extensions"` field in the resulting JSON. This is often a useful place to
add development time metadata such as the runtime of a query or the amount
of resources consumed. This may be an async function. The function is
given one object as an argument: `{ document, variables, operationName, result }`.
given one object as an argument: `{ document, variables, operationName, result, context }`.

* **`validationRules`**: Optional additional validation rules queries must
satisfy in addition to those defined by the GraphQL spec.
Expand Down Expand Up @@ -201,7 +201,7 @@ must return a JSON-serializable Object.
When called, this is provided an argument which you can use to get information
about the GraphQL request:

`{ document, variables, operationName, result }`
`{ document, variables, operationName, result, context }`

This example illustrates adding the amount of time consumed by running the
provided query, which could perhaps be used by your development tools.
Expand All @@ -213,14 +213,18 @@ const app = express();

app.use(session({ secret: 'keyboard cat', cookie: { maxAge: 60000 }}));

const extensions = ({ document, variables, operationName, result, context }) => {
return {
runTime: Date.now() - context.startTime
}
}

app.use('/graphql', graphqlHTTP(request => {
const startTime = Date.now();
return {
schema: MyGraphQLSchema,
context: { startTime: Date.now() },
graphiql: true,
extensions({ document, variables, operationName, result }) {
return { runTime: Date.now() - startTime };
}
extensions
};
}));
```
Expand Down
16 changes: 12 additions & 4 deletions src/__tests__/http-test.js
Expand Up @@ -1907,16 +1907,24 @@ describe('test harness', () => {
it('allows for adding extensions', async () => {
const app = server();

const extensions = ({ context = {} }) => {
if (context !== null && typeof context.startTime === 'number') {
return {
runTime: 1000000010 /* Date.now() */ - context.startTime,
};
}

return {};
};

get(
app,
urlString(),
graphqlHTTP(() => {
const startTime = 1000000000; /* Date.now(); */
return {
schema: TestSchema,
extensions() {
return { runTime: 1000000010 /* Date.now() */ - startTime };
},
context: { startTime: 1000000000 },
extensions,
};
}),
);
Expand Down
12 changes: 10 additions & 2 deletions src/index.js
Expand Up @@ -56,7 +56,7 @@ export type OptionsData = {
schema: GraphQLSchema,

/**
* A value to pass as the context to the graphql() function.
* A value to pass as the context to this middleware.
*/
context?: ?mixed,

Expand Down Expand Up @@ -131,6 +131,11 @@ export type RequestInfo = {
* The result of executing the operation.
*/
result: ?mixed,

/**
* A value to pass as the context to the graphql() function.
*/
context?: ?mixed,
};

type Middleware = (request: $Request, response: $Response) => Promise<void>;
Expand All @@ -148,6 +153,7 @@ function graphqlHTTP(options: Options): Middleware {
return function graphqlMiddleware(request: $Request, response: $Response) {
// Higher scoped variables are referred to at various stages in the
// asynchronous state machine below.
let context;
let params;
let pretty;
let formatErrorFn;
Expand Down Expand Up @@ -190,11 +196,12 @@ function graphqlHTTP(options: Options): Middleware {

// Collect information from the options data object.
const schema = optionsData.schema;
const context = optionsData.context || request;
const rootValue = optionsData.rootValue;
const fieldResolver = optionsData.fieldResolver;
const graphiql = optionsData.graphiql;

context = optionsData.context || request;

let validationRules = specifiedRules;
if (optionsData.validationRules) {
validationRules = validationRules.concat(optionsData.validationRules);
Expand Down Expand Up @@ -297,6 +304,7 @@ function graphqlHTTP(options: Options): Middleware {
variables,
operationName,
result,
context,
}),
).then(extensions => {
if (extensions && typeof extensions === 'object') {
Expand Down

0 comments on commit 41e26f8

Please sign in to comment.