Skip to content

Commit

Permalink
OverlappingFieldsCanBeMergedRule suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Sep 3, 2024
1 parent 32107eb commit aabf95d
Show file tree
Hide file tree
Showing 3 changed files with 414 additions and 193 deletions.
6 changes: 6 additions & 0 deletions src/validation/ValidationContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,12 @@ export class ValidationContext extends ASTValidationContext {
return this._typeInfo.getFragmentSignature();
}

getFragmentSignatureByName(): (
fragmentName: string,
) => Maybe<FragmentSignature> {
return this._typeInfo.getFragmentSignatureByName();
}

getEnumValue(): Maybe<GraphQLEnumValue> {
return this._typeInfo.getEnumValue();
}
Expand Down
121 changes: 112 additions & 9 deletions src/validation/__tests__/OverlappingFieldsCanBeMergedRule-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1303,31 +1303,134 @@ describe('Validate: Overlapping fields can be merged', () => {
]);
});

it('encounters conflict in fragment - field no args', () => {
expectErrors(`
it('allows operations with overlapping fields with arguments using identical operation variables', () => {
expectValid(`
query ($y: Int = 1) {
a(x: $y)
...WithArgs
...WithArgs(x: 1)
}
fragment WithArgs on Type {
fragment WithArgs($x: Int = 1) on Type {
a(x: $y)
}
`).toDeepEqual([]);
`);
});

it('encounters conflict in fragment/field', () => {
expectErrors(`
it('allows operations with overlapping fields with identical variable arguments passed via fragment arguments', () => {
expectValid(`
query ($y: Int = 1) {
a(x: $y)
...WithArgs(x: $y)
}
fragment WithArgs($x: Int) on Type {
a(x: $x)
}
`).toDeepEqual([]);
`);
});

it('allows operations with overlapping fields with identical variable arguments passed via nested fragment arguments', () => {
expectValid(`
query ($z: Int = 1) {
a(x: $z)
...WithArgs(y: $z)
}
fragment WithArgs($y: Int) on Type {
...NestedWithArgs(x: $y)
}
fragment NestedWithArgs($x: Int) on Type {
a(x: $x)
}
`);
});

it('allows operations with overlapping fields with identical arguments via fragment variable defaults', () => {
expectValid(`
query {
a(x: 1)
...WithArgs
}
fragment WithArgs($x: Int = 1) on Type {
a(x: $x)
}
`);
});

it('raises errors with overlapping fields with arguments that conflict via operation variables even with defaults and fragment variable defaults', () => {
expectErrors(`
query ($y: Int = 1) {
a(x: $y)
...WithArgs
}
fragment WithArgs($x: Int = 1) on Type {
a(x: $x)
}
`).toDeepEqual([
{
message:
'Fields "a" conflict because they have differing arguments. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{ line: 3, column: 11 },
{ line: 7, column: 11 },
],
},
]);
});

it('allows operations with overlapping list fields with identical variable arguments passed via fragment arguments', () => {
expectValid(`
query Query($stringListVarY: [String]) {
complicatedArgs {
stringListArgField(stringListArg: $stringListVarY)
...WithArgs(stringListVarX: $stringListVarY)
}
}
fragment WithArgs($stringListVarX: [String]) on Type {
stringListArgField(stringListArg: $stringListVarX)
}
`);
});

it('allows operations with overlapping list fields with identical variable arguments in item position passed via fragment arguments', () => {
expectValid(`
query Query($stringListVarY: [String]) {
complicatedArgs {
stringListArgField(stringListArg: [$stringListVarY])
...WithArgs(stringListVarX: $stringListVarY)
}
}
fragment WithArgs($stringListVarX: [String]) on Type {
stringListArgField(stringListArg: [$stringListVarX])
}
`);
});

it('allows operations with overlapping input object fields with identical variable arguments passed via fragment arguments', () => {
expectValid(`
query Query($complexVarY: ComplexInput) {
complicatedArgs {
complexArgField(complexArg: $complexVarY)
...WithArgs(complexVarX: $complexVarY)
}
}
fragment WithArgs($complexVarX: ComplexInput) on Type {
complexArgField(complexArg: $complexVarX)
}
`);
});

it('allows operations with overlapping input object fields with identical variable arguments in field position passed via fragment arguments', () => {
expectValid(`
query Query($boolVarY: Boolean) {
complicatedArgs {
complexArgField(complexArg: {requiredArg: $boolVarY})
...WithArgs(boolVarX: $boolVarY)
}
}
fragment WithArgs($boolVarX: Boolean) on Type {
complexArgField(complexArg: {requiredArg: $boolVarX})
}
`);
});

// This is currently not validated, should we?
it('encounters nested field conflict in fragments that could otherwise merge', () => {
expectErrors(`
query ValidDifferingFragmentArgs($command1: DogCommand, $command2: DogCommand) {
Expand Down
Loading

0 comments on commit aabf95d

Please sign in to comment.