|
| 1 | +import type { ExecutionResult } from 'graphql/execution'; |
| 2 | +import type { TypedQueryDocumentNode } from 'graphql/utilities'; |
| 3 | + |
| 4 | +import { parse } from 'graphql/language'; |
| 5 | +import { execute } from 'graphql/execution'; |
| 6 | +import { buildSchema } from 'graphql/utilities'; |
| 7 | + |
| 8 | +const schema = buildSchema(` |
| 9 | + type Query { |
| 10 | + test: String |
| 11 | + } |
| 12 | +`); |
| 13 | + |
| 14 | +// Tests for TS specific TypedQueryDocumentNode type |
| 15 | +const queryDocument = parse('{ test }'); |
| 16 | + |
| 17 | +type ResponseData = { test: string }; |
| 18 | +const typedQueryDocument = queryDocument as TypedQueryDocumentNode< |
| 19 | + ResponseData, |
| 20 | + {} |
| 21 | +>; |
| 22 | + |
| 23 | +// Supports conversion to DocumentNode |
| 24 | +execute({ schema, document: typedQueryDocument }); |
| 25 | + |
| 26 | +function wrappedExecute<T>(document: TypedQueryDocumentNode<T>) { |
| 27 | + return execute({ schema, document }) as ExecutionResult<T>; |
| 28 | +} |
| 29 | + |
| 30 | +const response = wrappedExecute(typedQueryDocument); |
| 31 | +const responseData: ResponseData | undefined | null = response.data; |
| 32 | + |
| 33 | +declare function runQueryA( |
| 34 | + q: TypedQueryDocumentNode<{ output: string }, { input: string | null }>, |
| 35 | +): void; |
| 36 | + |
| 37 | +// valid |
| 38 | +declare const optionalInputRequiredOutput: TypedQueryDocumentNode< |
| 39 | + { output: string }, |
| 40 | + { input: string | null } |
| 41 | +>; |
| 42 | +runQueryA(optionalInputRequiredOutput); |
| 43 | + |
| 44 | +declare function runQueryB( |
| 45 | + q: TypedQueryDocumentNode<{ output: string | null }, { input: string }>, |
| 46 | +): void; |
| 47 | + |
| 48 | +// still valid: We still accept {output: string} as a valid result. |
| 49 | +// We're now passing in {input: string} which is still assignable to {input: string | null} |
| 50 | +runQueryB(optionalInputRequiredOutput); |
| 51 | + |
| 52 | +// valid: we now accept {output: null} as a valid Result |
| 53 | +declare const optionalInputOptionalOutput: TypedQueryDocumentNode< |
| 54 | + { output: string | null }, |
| 55 | + { input: string | null } |
| 56 | +>; |
| 57 | +runQueryB(optionalInputOptionalOutput); |
| 58 | + |
| 59 | +// valid: we now only pass {input: string} to the query |
| 60 | +declare const requiredInputRequiredOutput: TypedQueryDocumentNode< |
| 61 | + { output: string }, |
| 62 | + { input: string } |
| 63 | +>; |
| 64 | +runQueryB(requiredInputRequiredOutput); |
| 65 | + |
| 66 | +// valid: we now accept {output: null} as a valid Result AND |
| 67 | +// we now only pass {input: string} to the query |
| 68 | +declare const requiredInputOptionalOutput: TypedQueryDocumentNode< |
| 69 | + { output: null }, |
| 70 | + { input: string } |
| 71 | +>; |
| 72 | +runQueryB(requiredInputOptionalOutput); |
0 commit comments