Skip to content

Commit

Permalink
feat(handler): Accept a GraphQL execution rootValue
Browse files Browse the repository at this point in the history
Closes #30
  • Loading branch information
enisdenjo committed Dec 24, 2022
1 parent 2fc37d2 commit 0f04fa2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
14 changes: 14 additions & 0 deletions docs/interfaces/handler.HandlerOptions.md
Expand Up @@ -22,6 +22,7 @@
- [onOperation](handler.HandlerOptions.md#onoperation)
- [onSubscribe](handler.HandlerOptions.md#onsubscribe)
- [parse](handler.HandlerOptions.md#parse)
- [rootValue](handler.HandlerOptions.md#rootvalue)
- [schema](handler.HandlerOptions.md#schema)
- [validate](handler.HandlerOptions.md#validate)

Expand Down Expand Up @@ -197,6 +198,19 @@ Throws GraphQLError if a syntax error is encountered.

___

### rootValue

`Optional` **rootValue**: `unknown`

The GraphQL root value or resolvers to go alongside the execution.
Learn more about them here: https://graphql.org/learn/execution/#root-fields-resolvers.

If you return from `onSubscribe`, and the returned value is
missing the `rootValue` field, the relevant operation root
will be used instead.

___

### schema

`Optional` **schema**: `GraphQLSchema` \| (`req`: [`Request`](handler.Request.md)<`RequestRaw`, `RequestContext`\>, `args`: `Omit`<[`OperationArgs`](../modules/handler.md#operationargs)<`Context`\>, ``"schema"``\>) => [`Response`](../modules/handler.md#response) \| `GraphQLSchema` \| `Promise`<[`Response`](../modules/handler.md#response) \| `GraphQLSchema`\>
Expand Down
14 changes: 14 additions & 0 deletions src/handler.ts
Expand Up @@ -215,6 +215,15 @@ export interface HandlerOptions<
* GraphQL operation AST getter used for detecting the operation type.
*/
getOperationAST?: typeof graphqlGetOperationAST;
/**
* The GraphQL root value or resolvers to go alongside the execution.
* Learn more about them here: https://graphql.org/learn/execution/#root-fields-resolvers.
*
* If you return from `onSubscribe`, and the returned value is
* missing the `rootValue` field, the relevant operation root
* will be used instead.
*/
rootValue?: unknown;
/**
* The subscribe callback executed right after processing the request
* before proceeding with the GraphQL operation execution.
Expand Down Expand Up @@ -366,6 +375,7 @@ export function createHandler<
execute = graphqlExecute,
parse = graphqlParse,
getOperationAST = graphqlGetOperationAST,
rootValue,
onSubscribe,
onOperation,
} = options;
Expand Down Expand Up @@ -576,6 +586,10 @@ export function createHandler<
];
}

if (!('rootValue' in args)) {
args.rootValue = rootValue;
}

if (!('contextValue' in args)) {
const resOrContext =
typeof context === 'function' ? await context(req, params) : context;
Expand Down

0 comments on commit 0f04fa2

Please sign in to comment.