Skip to content

Commit

Permalink
Add TypedDocumentNode support (#139)
Browse files Browse the repository at this point in the history
* Add TypedDocumentNode support

* Remove extra changes

* Remove unnecessary change

* Fix lint/format
  • Loading branch information
ardatan authored Nov 1, 2021
1 parent 6a9c4bb commit d75988c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
"typescript": "^3.9.5"
},
"dependencies": {
"@graphql-typed-document-node/core": "3.1.0",
"fast-json-stringify": "^1.13.0",
"generate-function": "^2.3.1",
"json-schema": "^0.2.3",
Expand Down
27 changes: 18 additions & 9 deletions src/execution.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TypedDocumentNode } from "@graphql-typed-document-node/core";
import fastJson from "fast-json-stringify";
import genFn from "generate-function";
import {
Expand Down Expand Up @@ -162,18 +163,23 @@ interface DeferredField {
args: Arguments;
}

export interface CompiledQuery {
export interface CompiledQuery<
TResult = { [key: string]: any },
TVariables = { [key: string]: any }
> {
operationName?: string;
query: (
root: any,
context: any,
variables: Maybe<{ [key: string]: any }>
) => Promise<ExecutionResult> | ExecutionResult;
variables: Maybe<TVariables>
) => Promise<ExecutionResult<TResult>> | ExecutionResult<TResult>;
subscribe?: (
root: any,
context: any,
variables: Maybe<{ [key: string]: any }>
) => Promise<AsyncIterableIterator<ExecutionResult> | ExecutionResult>;
variables: Maybe<TVariables>
) => Promise<
AsyncIterableIterator<ExecutionResult<TResult>> | ExecutionResult<TResult>
>;
stringify: (v: any) => string;
}

Expand All @@ -189,12 +195,15 @@ interface InternalCompiledQuery extends CompiledQuery {
* @param partialOptions compilation options to tune the compiler features
* @returns {CompiledQuery} the cacheable result
*/
export function compileQuery(
export function compileQuery<
TResult = { [key: string]: any },
TVariables = { [key: string]: any }
>(
schema: GraphQLSchema,
document: DocumentNode,
document: TypedDocumentNode<TResult, TVariables>,
operationName?: string,
partialOptions?: Partial<CompilerOptions>
): CompiledQuery | ExecutionResult {
): CompiledQuery<TResult, TVariables> | ExecutionResult<TResult> {
if (!schema) {
throw new Error(`Expected ${schema} to be a GraphQL schema.`);
}
Expand Down Expand Up @@ -285,7 +294,7 @@ export function compileQuery(
// and visualization tools like try-jit.
compiledQuery.__DO_NOT_USE_THIS_OR_YOU_WILL_BE_FIRED_compilation = functionBody;
}
return compiledQuery;
return compiledQuery as CompiledQuery<TResult, TVariables>;
} catch (err) {
return {
errors: normalizeErrors(err)
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@
aggregate-error "3.0.1"
camel-case "4.1.1"

"@graphql-typed-document-node/[email protected]":
version "3.1.0"
resolved "https://registry.yarnpkg.com/@graphql-typed-document-node/core/-/core-3.1.0.tgz#0eee6373e11418bfe0b5638f654df7a4ca6a3950"
integrity sha512-wYn6r8zVZyQJ6rQaALBEln5B1pzxb9shV5Ef97kTvn6yVGrqyXVnDqnU24MXnFubR+rZjBY9NWuxX3FB2sTsjg==

"@jest/console@^24.7.1":
version "24.7.1"
resolved "https://registry.yarnpkg.com/@jest/console/-/console-24.7.1.tgz#32a9e42535a97aedfe037e725bd67e954b459545"
Expand Down

0 comments on commit d75988c

Please sign in to comment.