Skip to content

Commit 5ed10ef

Browse files
integrationTests/ts: split tests into separate files (#3280)
1 parent ec57f06 commit 5ed10ef

File tree

4 files changed

+165
-145
lines changed

4 files changed

+165
-145
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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);

integrationTests/ts/basic-test.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { ExecutionResult } from 'graphql/execution';
2+
3+
import { graphqlSync } from 'graphql';
4+
import { GraphQLString, GraphQLSchema, GraphQLObjectType } from 'graphql/type';
5+
6+
const queryType: GraphQLObjectType = new GraphQLObjectType({
7+
name: 'Query',
8+
fields: () => ({
9+
sayHi: {
10+
type: GraphQLString,
11+
args: {
12+
who: { type: GraphQLString },
13+
},
14+
resolve(_root, args) {
15+
return 'Hello ' + (args.who ?? 'World');
16+
},
17+
},
18+
}),
19+
});
20+
21+
const schema: GraphQLSchema = new GraphQLSchema({ query: queryType });
22+
23+
const result: ExecutionResult = graphqlSync({
24+
schema,
25+
source: `
26+
query helloWho($who: String){
27+
test(who: $who)
28+
}
29+
`,
30+
variableValues: { who: 'Dolly' },
31+
});
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { GraphQLString, GraphQLObjectType } from 'graphql/type';
2+
3+
interface SomeExtension {
4+
number: number;
5+
string: string;
6+
}
7+
8+
const example: SomeExtension = {
9+
number: 42,
10+
string: 'Meaning of life',
11+
};
12+
13+
declare module 'graphql' {
14+
interface GraphQLObjectTypeExtensions<_TSource = any, _TContext = any> {
15+
someObjectExtension?: SomeExtension;
16+
}
17+
18+
interface GraphQLFieldExtensions<
19+
_TSource,
20+
_TContext,
21+
_TArgs = { [argName: string]: any },
22+
> {
23+
someFieldExtension?: SomeExtension;
24+
}
25+
26+
interface GraphQLArgumentExtensions {
27+
someArgumentExtension?: SomeExtension;
28+
}
29+
}
30+
31+
const queryType: GraphQLObjectType = new GraphQLObjectType({
32+
name: 'Query',
33+
fields: () => ({
34+
sayHi: {
35+
type: GraphQLString,
36+
args: {
37+
who: {
38+
type: GraphQLString,
39+
extensions: {
40+
someArgumentExtension: example,
41+
},
42+
},
43+
},
44+
resolve: (_root, args) => 'Hello ' + (args.who || 'World'),
45+
extensions: {
46+
someFieldExtension: example,
47+
},
48+
},
49+
}),
50+
extensions: {
51+
someObjectExtension: example,
52+
},
53+
});
54+
55+
function checkExtensionTypes(_test: SomeExtension | null | undefined) {}
56+
57+
checkExtensionTypes(queryType.extensions.someObjectExtension);
58+
59+
const sayHiField = queryType.getFields().sayHi;
60+
checkExtensionTypes(sayHiField.extensions.someFieldExtension);
61+
62+
checkExtensionTypes(sayHiField.args[0].extensions.someArgumentExtension);

integrationTests/ts/index.ts

Lines changed: 0 additions & 145 deletions
This file was deleted.

0 commit comments

Comments
 (0)