Skip to content

Commit

Permalink
Add 'TypedQueryDocumentNode' TS type (graphql#2749)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Aug 22, 2020
1 parent b83a9fe commit d6be64c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
30 changes: 28 additions & 2 deletions integrationTests/ts/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parse } from 'graphql/language';
import { GraphQLString, GraphQLSchema, GraphQLObjectType } from 'graphql/type';
import { ExecutionResult } from 'graphql/execution';
import { graphqlSync } from 'graphql';
import { ExecutionResult, execute } from 'graphql/execution';
import { TypedQueryDocumentNode, graphqlSync } from 'graphql';

interface SomeExtension {
number: number;
Expand Down Expand Up @@ -69,3 +70,28 @@ const result: ExecutionResult = graphqlSync({
`,
variableValues: { who: 'Dolly' },
});

// Tests for TS specific TypedQueryDocumentNode type
const queryDocument = parse(`
query helloWho($who: String){
test(who: $who)
}
`);

type ResponseData = { test: string };
const typedQueryDocument = queryDocument as TypedQueryDocumentNode<
ResponseData,
{}
>;

// Supports conversion to DocumentNode
execute({ schema, document: typedQueryDocument });

function wrappedExecute<T>(document: TypedQueryDocumentNode<T>) {
return execute({ schema, document }) as ExecutionResult<T>;
}

const { data } = wrappedExecute(typedQueryDocument);
if (data != null) {
const typedData: ResponseData = data;
}
1 change: 1 addition & 0 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,5 @@ export {
BuildSchemaOptions,
BreakingChange,
DangerousChange,
TypedQueryDocumentNode,
} from './utilities/index';
3 changes: 3 additions & 0 deletions src/utilities/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,8 @@ export {
DangerousChange,
} from './findBreakingChanges';

// Wrapper type that contains DocumentNode and types that can be deduced from it.
export { TypedQueryDocumentNode } from './typedQueryDocumentNode';

// @deprecated: Report all deprecated usage within a GraphQL document.
export { findDeprecatedUsages } from './findDeprecatedUsages';
14 changes: 14 additions & 0 deletions src/utilities/typedQueryDocumentNode.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { DocumentNode, ExecutableDefinitionNode } from '../language/ast';

/**
* Wrapper type that contains DocumentNode and types that can be deduced from it.
*/
interface TypedQueryDocumentNode<
TResponseData = Record<string, any>,
TRequestVariables = Record<string, any>
> extends DocumentNode {
readonly definitions: ReadonlyArray<ExecutableDefinitionNode>;
// FIXME: remove once TS implements proper way to enforce nominal typing
readonly __enforceStructuralTypingOnResponseDataType?: TResponseData;
readonly __enforceStructuralTypingOnRequestVariablesType?: TRequestVariables;
}

0 comments on commit d6be64c

Please sign in to comment.