Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JoviDeCroock committed Mar 23, 2024
1 parent 21ffcc1 commit 845da80
Showing 1 changed file with 301 additions and 1 deletion.
302 changes: 301 additions & 1 deletion packages/executor/src/execution/__tests__/variables-test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// eslint-disable-next-line import/no-extraneous-dependencies
import { inspect } from 'cross-inspect';
import {
GraphQLArgumentConfig,
Expand Down Expand Up @@ -30,6 +30,13 @@ const TestComplexScalar = new GraphQLScalarType({
},
});

const NestedType: GraphQLObjectType = new GraphQLObjectType({
name: 'NestedType',
fields: {
echo: fieldWithInputArg({ type: GraphQLString }),
},
});

const TestInputObject = new GraphQLInputObjectType({
name: 'TestInputObject',
fields: {
Expand Down Expand Up @@ -98,6 +105,10 @@ const TestType = new GraphQLObjectType({
defaultValue: 'Hello World',
}),
list: fieldWithInputArg({ type: new GraphQLList(GraphQLString) }),
nested: {
type: NestedType,
resolve: () => ({}),
},
nnList: fieldWithInputArg({
type: new GraphQLNonNull(new GraphQLList(GraphQLString)),
}),
Expand All @@ -117,6 +128,16 @@ function executeQuery(query: string, variableValues?: { [variable: string]: unkn
return executeSync({ schema, document, variableValues });
}


function executeQueryWithFragmentArguments(
query: string,
variableValues?: { [variable: string]: unknown },
) {
// TODO: figure out how to do custom parser here
const document = parse(query, { experimentalFragmentArguments: true });

Check failure on line 137 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Federation Benchmark with 100 Products

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 137 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Federation Benchmark with 1000 Products

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 137 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Federation Benchmark with 50 Products

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 137 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Type Check on GraphQL v16

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 137 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Federation Benchmark with 3 Products

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 137 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Browser Test

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 137 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Type Check on GraphQL v17.0.0-alpha.1

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 137 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Type Check on GraphQL v15

Object literal may only specify known properties, but 'experimentalFragmentArguments' does not exist in type 'ParseOptions'. Did you mean to write 'experimentalFragmentVariables'?

Check failure on line 137 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / ESM Test

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.

Check failure on line 137 in packages/executor/src/execution/__tests__/variables-test.ts

View workflow job for this annotation

GitHub Actions / Federation Benchmark with 10 Products

Object literal may only specify known properties, and 'experimentalFragmentArguments' does not exist in type 'ParseOptions'.
return executeSync({ schema, document, variableValues });
}

describe('Execute: Handles inputs', () => {
describe('Handles objects and nullability', () => {
describe('using inline structs', () => {
Expand Down Expand Up @@ -1038,4 +1059,283 @@ describe('Execute: Handles inputs', () => {
});
});
});

describe('using fragment arguments', () => {
it('when there are no fragment arguments', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a
}
fragment a on TestType {
fieldWithNonNullableStringInput(input: "A")
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInput: '"A"',
},
});
});

it('when a value is required and provided', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a(value: "A")
}
fragment a($value: String!) on TestType {
fieldWithNonNullableStringInput(input: $value)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInput: '"A"',
},
});
});

it('when a value is required and not provided', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a
}
fragment a($value: String!) on TestType {
fieldWithNullableStringInput(input: $value)
}
`);

expect(result).toHaveProperty('errors');
expect(result.errors).toHaveLength(1);
expect(result.errors?.at(0)?.message).toMatch(
/Argument "value" of required type "String!"/,
);
});

it('when the definition has a default and is provided', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a(value: "A")
}
fragment a($value: String! = "B") on TestType {
fieldWithNonNullableStringInput(input: $value)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInput: '"A"',
},
});
});

it('when the definition has a default and is not provided', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a
}
fragment a($value: String! = "B") on TestType {
fieldWithNonNullableStringInput(input: $value)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInput: '"B"',
},
});
});

it('when a definition has a default, is not provided, and spreads another fragment', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a
}
fragment a($a: String! = "B") on TestType {
...b(b: $a)
}
fragment b($b: String!) on TestType {
fieldWithNonNullableStringInput(input: $b)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInput: '"B"',
},
});
});

it('when the definition has a non-nullable default and is provided null', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a(value: null)
}
fragment a($value: String! = "B") on TestType {
fieldWithNullableStringInput(input: $value)
}
`);

expect(result).toHaveProperty('errors');
expect(result.errors).toHaveLength(1);
expect(result.errors?.at(0)?.message).toMatch(
/Argument "value" of non-null type "String!"/,
);
});

it('when the definition has no default and is not provided', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a
}
fragment a($value: String) on TestType {
fieldWithNonNullableStringInputAndDefaultArgumentValue(input: $value)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInputAndDefaultArgumentValue:
'"Hello World"',
},
});
});

it('when an argument is shadowed by an operation variable', () => {
const result = executeQueryWithFragmentArguments(`
query($x: String! = "A") {
...a(x: "B")
}
fragment a($x: String) on TestType {
fieldWithNullableStringInput(input: $x)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNullableStringInput: '"B"',
},
});
});

it('when a nullable argument with a field default is not provided and shadowed by an operation variable', () => {
const result = executeQueryWithFragmentArguments(`
query($x: String = "A") {
...a
}
fragment a($x: String) on TestType {
fieldWithNonNullableStringInputAndDefaultArgumentValue(input: $x)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNonNullableStringInputAndDefaultArgumentValue:
'"Hello World"',
},
});
});

it('when a fragment-variable is shadowed by an intermediate fragment-spread but defined in the operation-variables', () => {
const result = executeQueryWithFragmentArguments(`
query($x: String = "A") {
...a
}
fragment a($x: String) on TestType {
...b
}
fragment b on TestType {
fieldWithNullableStringInput(input: $x)
}
`);
expectJSON(result).toDeepEqual({
data: {
fieldWithNullableStringInput: '"A"',
},
});
});

it('when a fragment is used with different args', () => {
const result = executeQueryWithFragmentArguments(`
query($x: String = "Hello") {
a: nested {
...a(x: "a")
}
b: nested {
...a(x: "b", b: true)
}
hello: nested {
...a(x: $x)
}
}
fragment a($x: String, $b: Boolean = false) on NestedType {
a: echo(input: $x) @skip(if: $b)
b: echo(input: $x) @include(if: $b)
}
`);
expectJSON(result).toDeepEqual({
data: {
a: {
a: '"a"',
},
b: {
b: '"b"',
},
hello: {
a: '"Hello"',
},
},
});
});

it('when the argument variable is nested in a complex type', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a(value: "C")
}
fragment a($value: String) on TestType {
list(input: ["A", "B", $value, "D"])
}
`);
expectJSON(result).toDeepEqual({
data: {
list: '["A", "B", "C", "D"]',
},
});
});

it('when argument variables are used recursively', () => {
const result = executeQueryWithFragmentArguments(`
query {
...a(aValue: "C")
}
fragment a($aValue: String) on TestType {
...b(bValue: $aValue)
}
fragment b($bValue: String) on TestType {
list(input: ["A", "B", $bValue, "D"])
}
`);
expectJSON(result).toDeepEqual({
data: {
list: '["A", "B", "C", "D"]',
},
});
});

it('when argument passed in as list', () => {
const result = executeQueryWithFragmentArguments(`
query Q($opValue: String = "op") {
...a(aValue: "A")
}
fragment a($aValue: String, $bValue: String) on TestType {
...b(aValue: [$aValue, "B"], bValue: [$bValue, $opValue])
}
fragment b($aValue: [String], $bValue: [String], $cValue: String) on TestType {
aList: list(input: $aValue)
bList: list(input: $bValue)
cList: list(input: [$cValue])
}
`);
expectJSON(result).toDeepEqual({
data: {
aList: '["A", "B"]',
bList: '[null, "op"]',
cList: '[null]',
},
});
});
});
});

0 comments on commit 845da80

Please sign in to comment.