Skip to content

Commit 0f04fa2

Browse files
committed
feat(handler): Accept a GraphQL execution rootValue
Closes #30
1 parent 2fc37d2 commit 0f04fa2

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

docs/interfaces/handler.HandlerOptions.md

+14
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [onOperation](handler.HandlerOptions.md#onoperation)
2323
- [onSubscribe](handler.HandlerOptions.md#onsubscribe)
2424
- [parse](handler.HandlerOptions.md#parse)
25+
- [rootValue](handler.HandlerOptions.md#rootvalue)
2526
- [schema](handler.HandlerOptions.md#schema)
2627
- [validate](handler.HandlerOptions.md#validate)
2728

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

198199
___
199200

201+
### rootValue
202+
203+
`Optional` **rootValue**: `unknown`
204+
205+
The GraphQL root value or resolvers to go alongside the execution.
206+
Learn more about them here: https://graphql.org/learn/execution/#root-fields-resolvers.
207+
208+
If you return from `onSubscribe`, and the returned value is
209+
missing the `rootValue` field, the relevant operation root
210+
will be used instead.
211+
212+
___
213+
200214
### schema
201215

202216
`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`\>

src/handler.ts

+14
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ export interface HandlerOptions<
215215
* GraphQL operation AST getter used for detecting the operation type.
216216
*/
217217
getOperationAST?: typeof graphqlGetOperationAST;
218+
/**
219+
* The GraphQL root value or resolvers to go alongside the execution.
220+
* Learn more about them here: https://graphql.org/learn/execution/#root-fields-resolvers.
221+
*
222+
* If you return from `onSubscribe`, and the returned value is
223+
* missing the `rootValue` field, the relevant operation root
224+
* will be used instead.
225+
*/
226+
rootValue?: unknown;
218227
/**
219228
* The subscribe callback executed right after processing the request
220229
* before proceeding with the GraphQL operation execution.
@@ -366,6 +375,7 @@ export function createHandler<
366375
execute = graphqlExecute,
367376
parse = graphqlParse,
368377
getOperationAST = graphqlGetOperationAST,
378+
rootValue,
369379
onSubscribe,
370380
onOperation,
371381
} = options;
@@ -576,6 +586,10 @@ export function createHandler<
576586
];
577587
}
578588

589+
if (!('rootValue' in args)) {
590+
args.rootValue = rootValue;
591+
}
592+
579593
if (!('contextValue' in args)) {
580594
const resOrContext =
581595
typeof context === 'function' ? await context(req, params) : context;

0 commit comments

Comments
 (0)