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

Commit

Permalink
Allow to replace default execute function
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Aug 31, 2017
1 parent fc0063f commit 64f68e1
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -91,6 +91,9 @@ The `graphqlHTTP` function accepts the following options:
* **`validationRules`**: Optional additional validation rules queries must
satisfy in addition to those defined by the GraphQL spec.

* **`execute`**: function which will be used to execute query instead of default
`execute` from `graphql-js`.

In addition to an object defining each option, options can also be provided as
a function (or async function) which returns this options object. This function
is provided the arguments `(request, response, graphQLParams)` and is called
Expand Down
32 changes: 32 additions & 0 deletions src/__tests__/http-test.js
Expand Up @@ -31,6 +31,7 @@ import {
GraphQLString,
GraphQLError,
BREAK,
execute,
} from 'graphql';
import graphqlHTTP from '../';

Expand Down Expand Up @@ -1803,6 +1804,37 @@ describe('test harness', () => {
});
});

describe('Custom execute', () => {
it('allow to replace default execute.', async () => {
const app = server();

let seenExecuteArgs;

get(
app,
urlString(),
graphqlHTTP(() => {
return {
schema: TestSchema,
async execute(args) {
seenExecuteArgs = args;
const result = await execute(args);
result.data.test2 = 'Modification';
return result;
},
};
}),
);

const response = await request(app).get(urlString({ query: '{test}' }));

expect(response.text).to.equal(
'{"data":{"test":"Hello World","test2":"Modification"}}',
);
expect(seenExecuteArgs).to.not.equal(null);
});
});

describe('Custom result extensions', () => {
it('allows for adding extensions', async () => {
const app = server();
Expand Down
19 changes: 14 additions & 5 deletions src/index.js
Expand Up @@ -18,6 +18,7 @@ import {
getOperationAST,
specifiedRules,
} from 'graphql';
import type { ExecutionArgs, ExecutionResult } from 'graphql';
import httpError from 'http-errors';
import url from 'url';

Expand All @@ -42,6 +43,7 @@ export type Options =
) => OptionsResult)
| OptionsResult;
export type OptionsResult = OptionsData | Promise<OptionsData>;

export type OptionsData = {
/**
* A GraphQL schema from graphql-js.
Expand Down Expand Up @@ -92,6 +94,12 @@ export type OptionsData = {
* A boolean to optionally enable GraphiQL mode.
*/
graphiql?: ?boolean,

/**
* An optional function which will be used to execute query instead of default
* "execute" from graphql-js.
*/
execute?: ?(args: ExecutionArgs) => Promise<ExecutionResult>,
};

/**
Expand Down Expand Up @@ -176,6 +184,7 @@ function graphqlHTTP(options: Options): Middleware {
const context = optionsData.context || request;
const rootValue = optionsData.rootValue;
const graphiql = optionsData.graphiql;
const executeFn = optionsData.execute || execute;
pretty = optionsData.pretty;
formatErrorFn = optionsData.formatError;
extensionsFn = optionsData.extensions;
Expand Down Expand Up @@ -249,14 +258,14 @@ function graphqlHTTP(options: Options): Middleware {
}
// Perform the execution, reporting any errors creating the context.
try {
return execute(
return executeFn({
schema,
documentAST,
document: documentAST,
rootValue,
context,
variables,
contextValue: context,
variableValues: variables,
operationName,
);
});
} catch (contextError) {
// Return 400: Bad Request if any execution context errors exist.
response.statusCode = 400;
Expand Down

0 comments on commit 64f68e1

Please sign in to comment.