From deb7d8beaff5d43da749c5bc0f2020cdcaaabb6b Mon Sep 17 00:00:00 2001 From: Curtis Layne Date: Tue, 3 Jul 2018 13:02:54 -0400 Subject: [PATCH] [@types/graphql] Add generic type on ExecutionResult (#26763) * [@types/graphql] Add generic type on ExecutionResult Without a generic type on ExecutionResult we can't have typesafe operations. * Add default type to the generic so it's not a breaking change * Don't make TData readonly --- types/graphql/execution/execute.d.ts | 15 ++++++++++----- types/graphql/graphql.d.ts | 14 +++++++------- types/graphql/index.d.ts | 1 + types/graphql/subscription/subscribe.d.ts | 14 +++++++------- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/types/graphql/execution/execute.d.ts b/types/graphql/execution/execute.d.ts index 1b0eccbd92..fc6ad898d1 100644 --- a/types/graphql/execution/execute.d.ts +++ b/types/graphql/execution/execute.d.ts @@ -36,15 +36,19 @@ export interface ExecutionContext { errors: GraphQLError[]; } +export interface ExecutionResultDataDefault { + [key: string]: any +} + /** * The result of GraphQL execution. * * - `errors` is included when any errors occurred as a non-empty array. * - `data` is the result of a successful execution of the query. */ -export interface ExecutionResult { +export interface ExecutionResult { errors?: ReadonlyArray; - data?: { [key: string]: any }; + data?: TData; } export type ExecutionArgs = { @@ -69,8 +73,8 @@ export type ExecutionArgs = { * * Accepts either an object with named arguments, or individual arguments. */ -export function execute(args: ExecutionArgs): MaybePromise; -export function execute( +export function execute(args: ExecutionArgs): MaybePromise>; +export function execute( schema: GraphQLSchema, document: DocumentNode, rootValue?: any, @@ -78,7 +82,7 @@ export function execute( variableValues?: Maybe<{ [key: string]: any }>, operationName?: Maybe, fieldResolver?: Maybe> -): MaybePromise; +): MaybePromise>; /** * Given a ResponsePath (found in the `path` entry in the information provided @@ -89,6 +93,7 @@ export function responsePathAsArray(path: ResponsePath): ReadonlyArray>; } -export function graphql(args: GraphQLArgs): Promise; -export function graphql( +export function graphql(args: GraphQLArgs): Promise>; +export function graphql( schema: GraphQLSchema, source: Source | string, rootValue?: any, @@ -53,7 +53,7 @@ export function graphql( variableValues?: Maybe<{ [key: string]: any }>, operationName?: Maybe, fieldResolver?: Maybe> -): Promise; +): Promise>; /** * The graphqlSync function also fulfills GraphQL operations by parsing, @@ -61,8 +61,8 @@ export function graphql( * However, it guarantees to complete synchronously (or throw an error) assuming * that all field resolvers are also synchronous. */ -export function graphqlSync(args: GraphQLArgs): ExecutionResult; -export function graphqlSync( +export function graphqlSync(args: GraphQLArgs): ExecutionResult; +export function graphqlSync( schema: GraphQLSchema, source: Source | string, rootValue?: any, @@ -70,4 +70,4 @@ export function graphqlSync( variableValues?: Maybe<{ [key: string]: any }>, operationName?: Maybe, fieldResolver?: Maybe> -): ExecutionResult; +): ExecutionResult; diff --git a/types/graphql/index.d.ts b/types/graphql/index.d.ts index c4a425a541..5a4e0d77d6 100644 --- a/types/graphql/index.d.ts +++ b/types/graphql/index.d.ts @@ -14,6 +14,7 @@ // Alessio Dionisi // Divyendu Singh // Brad Zacher +// Curtis Layne // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 diff --git a/types/graphql/subscription/subscribe.d.ts b/types/graphql/subscription/subscribe.d.ts index f5b29ebbb8..b58c36bd8a 100644 --- a/types/graphql/subscription/subscribe.d.ts +++ b/types/graphql/subscription/subscribe.d.ts @@ -2,7 +2,7 @@ import Maybe from "../tsutils/Maybe"; import { GraphQLSchema } from "../type/schema"; import { DocumentNode } from "../language/ast"; import { GraphQLFieldResolver } from "../type/definition"; -import { ExecutionResult } from "../execution/execute"; +import { ExecutionResult, ExecutionResultDataDefault } from "../execution/execute"; /** * Implements the "Subscribe" algorithm described in the GraphQL specification. @@ -24,7 +24,7 @@ import { ExecutionResult } from "../execution/execute"; * * Accepts either an object with named arguments, or individual arguments. */ -export function subscribe(args: { +export function subscribe(args: { schema: GraphQLSchema; document: DocumentNode; rootValue?: any; @@ -33,9 +33,9 @@ export function subscribe(args: { operationName?: Maybe; fieldResolver?: Maybe>; subscribeFieldResolver?: Maybe>; -}): Promise | ExecutionResult>; +}): Promise> | ExecutionResult>; -export function subscribe( +export function subscribe( schema: GraphQLSchema, document: DocumentNode, rootValue?: any, @@ -44,7 +44,7 @@ export function subscribe( operationName?: Maybe, fieldResolver?: Maybe>, subscribeFieldResolver?: Maybe> -): Promise | ExecutionResult>; +): Promise> | ExecutionResult>; /** * Implements the "CreateSourceEventStream" algorithm described in the @@ -64,7 +64,7 @@ export function subscribe( * or otherwise separating these two steps. For more on this, see the * "Supporting Subscriptions at Scale" information in the GraphQL specification. */ -export function createSourceEventStream( +export function createSourceEventStream( schema: GraphQLSchema, document: DocumentNode, rootValue?: any, @@ -72,4 +72,4 @@ export function createSourceEventStream( variableValues?: { [key: string]: any }, operationName?: Maybe, fieldResolver?: Maybe> -): Promise | ExecutionResult>; +): Promise | ExecutionResult>;