From d75988cb12de4f223aed32a64d03c34d417cc1cd Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Mon, 1 Nov 2021 13:23:27 -0400 Subject: [PATCH] Add TypedDocumentNode support (#139) * Add TypedDocumentNode support * Remove extra changes * Remove unnecessary change * Fix lint/format --- package.json | 1 + src/execution.ts | 27 ++++++++++++++++++--------- yarn.lock | 5 +++++ 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/package.json b/package.json index 21b3860f..68d273d2 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/execution.ts b/src/execution.ts index 11ca2b1f..ba863622 100644 --- a/src/execution.ts +++ b/src/execution.ts @@ -1,3 +1,4 @@ +import { TypedDocumentNode } from "@graphql-typed-document-node/core"; import fastJson from "fast-json-stringify"; import genFn from "generate-function"; import { @@ -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; + variables: Maybe + ) => Promise> | ExecutionResult; subscribe?: ( root: any, context: any, - variables: Maybe<{ [key: string]: any }> - ) => Promise | ExecutionResult>; + variables: Maybe + ) => Promise< + AsyncIterableIterator> | ExecutionResult + >; stringify: (v: any) => string; } @@ -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, operationName?: string, partialOptions?: Partial -): CompiledQuery | ExecutionResult { +): CompiledQuery | ExecutionResult { if (!schema) { throw new Error(`Expected ${schema} to be a GraphQL schema.`); } @@ -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; } catch (err) { return { errors: normalizeErrors(err) diff --git a/yarn.lock b/yarn.lock index 6c31c003..4d465a18 100644 --- a/yarn.lock +++ b/yarn.lock @@ -162,6 +162,11 @@ aggregate-error "3.0.1" camel-case "4.1.1" +"@graphql-typed-document-node/core@3.1.0": + 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"