File tree Expand file tree Collapse file tree 4 files changed +46
-2
lines changed Expand file tree Collapse file tree 4 files changed +46
-2
lines changed Original file line number Diff line number Diff line change
1
+ import { parse } from 'graphql/language' ;
1
2
import { GraphQLString , GraphQLSchema , GraphQLObjectType } from 'graphql/type' ;
2
- import { ExecutionResult } from 'graphql/execution' ;
3
- import { graphqlSync } from 'graphql' ;
3
+ import { ExecutionResult , execute } from 'graphql/execution' ;
4
+ import { TypedQueryDocumentNode , graphqlSync } from 'graphql' ;
4
5
5
6
interface SomeExtension {
6
7
number : number ;
@@ -69,3 +70,28 @@ const result: ExecutionResult = graphqlSync({
69
70
` ,
70
71
variableValues : { who : 'Dolly' } ,
71
72
} ) ;
73
+
74
+ // Tests for TS specific TypedQueryDocumentNode type
75
+ const queryDocument = parse ( `
76
+ query helloWho($who: String){
77
+ test(who: $who)
78
+ }
79
+ ` ) ;
80
+
81
+ type ResponseData = { test : string } ;
82
+ const typedQueryDocument = queryDocument as TypedQueryDocumentNode <
83
+ ResponseData ,
84
+ { }
85
+ > ;
86
+
87
+ // Supports conversion to DocumentNode
88
+ execute ( { schema, document : typedQueryDocument } ) ;
89
+
90
+ function wrappedExecute < T > ( document : TypedQueryDocumentNode < T > ) {
91
+ return execute ( { schema, document } ) as ExecutionResult < T > ;
92
+ }
93
+
94
+ const { data } = wrappedExecute ( typedQueryDocument ) ;
95
+ if ( data != null ) {
96
+ const typedData : ResponseData = data ;
97
+ }
Original file line number Diff line number Diff line change @@ -461,4 +461,5 @@ export {
461
461
BuildSchemaOptions ,
462
462
BreakingChange ,
463
463
DangerousChange ,
464
+ TypedQueryDocumentNode ,
464
465
} from './utilities/index' ;
Original file line number Diff line number Diff line change @@ -112,5 +112,8 @@ export {
112
112
DangerousChange ,
113
113
} from './findBreakingChanges' ;
114
114
115
+ // Wrapper type that contains DocumentNode and types that can be deduced from it.
116
+ export { TypedQueryDocumentNode } from './typedQueryDocumentNode' ;
117
+
115
118
// @deprecated : Report all deprecated usage within a GraphQL document.
116
119
export { findDeprecatedUsages } from './findDeprecatedUsages' ;
Original file line number Diff line number Diff line change
1
+ import { DocumentNode , ExecutableDefinitionNode } from '../language/ast' ;
2
+
3
+ /**
4
+ * Wrapper type that contains DocumentNode and types that can be deduced from it.
5
+ */
6
+ interface TypedQueryDocumentNode <
7
+ TResponseData = Record < string , any > ,
8
+ TRequestVariables = Record < string , any >
9
+ > extends DocumentNode {
10
+ readonly definitions : ReadonlyArray < ExecutableDefinitionNode > ;
11
+ // FIXME: remove once TS implements proper way to enforce nominal typing
12
+ readonly __enforceStructuralTypingOnResponseDataType ?: TResponseData ;
13
+ readonly __enforceStructuralTypingOnRequestVariablesType ?: TRequestVariables ;
14
+ }
You can’t perform that action at this time.
0 commit comments