Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
integrationTests/ts: split tests into separate files (#3280)
- Loading branch information
1 parent
ec57f06
commit 5ed10ef
Showing
4 changed files
with
165 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import type { ExecutionResult } from 'graphql/execution'; | ||
import type { TypedQueryDocumentNode } from 'graphql/utilities'; | ||
|
||
import { parse } from 'graphql/language'; | ||
import { execute } from 'graphql/execution'; | ||
import { buildSchema } from 'graphql/utilities'; | ||
|
||
const schema = buildSchema(` | ||
type Query { | ||
test: String | ||
} | ||
`); | ||
|
||
// Tests for TS specific TypedQueryDocumentNode type | ||
const queryDocument = parse('{ test }'); | ||
|
||
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 response = wrappedExecute(typedQueryDocument); | ||
const responseData: ResponseData | undefined | null = response.data; | ||
|
||
declare function runQueryA( | ||
q: TypedQueryDocumentNode<{ output: string }, { input: string | null }>, | ||
): void; | ||
|
||
// valid | ||
declare const optionalInputRequiredOutput: TypedQueryDocumentNode< | ||
{ output: string }, | ||
{ input: string | null } | ||
>; | ||
runQueryA(optionalInputRequiredOutput); | ||
|
||
declare function runQueryB( | ||
q: TypedQueryDocumentNode<{ output: string | null }, { input: string }>, | ||
): void; | ||
|
||
// still valid: We still accept {output: string} as a valid result. | ||
// We're now passing in {input: string} which is still assignable to {input: string | null} | ||
runQueryB(optionalInputRequiredOutput); | ||
|
||
// valid: we now accept {output: null} as a valid Result | ||
declare const optionalInputOptionalOutput: TypedQueryDocumentNode< | ||
{ output: string | null }, | ||
{ input: string | null } | ||
>; | ||
runQueryB(optionalInputOptionalOutput); | ||
|
||
// valid: we now only pass {input: string} to the query | ||
declare const requiredInputRequiredOutput: TypedQueryDocumentNode< | ||
{ output: string }, | ||
{ input: string } | ||
>; | ||
runQueryB(requiredInputRequiredOutput); | ||
|
||
// valid: we now accept {output: null} as a valid Result AND | ||
// we now only pass {input: string} to the query | ||
declare const requiredInputOptionalOutput: TypedQueryDocumentNode< | ||
{ output: null }, | ||
{ input: string } | ||
>; | ||
runQueryB(requiredInputOptionalOutput); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { ExecutionResult } from 'graphql/execution'; | ||
|
||
import { graphqlSync } from 'graphql'; | ||
import { GraphQLString, GraphQLSchema, GraphQLObjectType } from 'graphql/type'; | ||
|
||
const queryType: GraphQLObjectType = new GraphQLObjectType({ | ||
name: 'Query', | ||
fields: () => ({ | ||
sayHi: { | ||
type: GraphQLString, | ||
args: { | ||
who: { type: GraphQLString }, | ||
}, | ||
resolve(_root, args) { | ||
return 'Hello ' + (args.who ?? 'World'); | ||
}, | ||
}, | ||
}), | ||
}); | ||
|
||
const schema: GraphQLSchema = new GraphQLSchema({ query: queryType }); | ||
|
||
const result: ExecutionResult = graphqlSync({ | ||
schema, | ||
source: ` | ||
query helloWho($who: String){ | ||
test(who: $who) | ||
} | ||
`, | ||
variableValues: { who: 'Dolly' }, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { GraphQLString, GraphQLObjectType } from 'graphql/type'; | ||
|
||
interface SomeExtension { | ||
number: number; | ||
string: string; | ||
} | ||
|
||
const example: SomeExtension = { | ||
number: 42, | ||
string: 'Meaning of life', | ||
}; | ||
|
||
declare module 'graphql' { | ||
interface GraphQLObjectTypeExtensions<_TSource = any, _TContext = any> { | ||
someObjectExtension?: SomeExtension; | ||
} | ||
|
||
interface GraphQLFieldExtensions< | ||
_TSource, | ||
_TContext, | ||
_TArgs = { [argName: string]: any }, | ||
> { | ||
someFieldExtension?: SomeExtension; | ||
} | ||
|
||
interface GraphQLArgumentExtensions { | ||
someArgumentExtension?: SomeExtension; | ||
} | ||
} | ||
|
||
const queryType: GraphQLObjectType = new GraphQLObjectType({ | ||
name: 'Query', | ||
fields: () => ({ | ||
sayHi: { | ||
type: GraphQLString, | ||
args: { | ||
who: { | ||
type: GraphQLString, | ||
extensions: { | ||
someArgumentExtension: example, | ||
}, | ||
}, | ||
}, | ||
resolve: (_root, args) => 'Hello ' + (args.who || 'World'), | ||
extensions: { | ||
someFieldExtension: example, | ||
}, | ||
}, | ||
}), | ||
extensions: { | ||
someObjectExtension: example, | ||
}, | ||
}); | ||
|
||
function checkExtensionTypes(_test: SomeExtension | null | undefined) {} | ||
|
||
checkExtensionTypes(queryType.extensions.someObjectExtension); | ||
|
||
const sayHiField = queryType.getFields().sayHi; | ||
checkExtensionTypes(sayHiField.extensions.someFieldExtension); | ||
|
||
checkExtensionTypes(sayHiField.args[0].extensions.someArgumentExtension); |
This file was deleted.
Oops, something went wrong.