Skip to content

Commit a738c6d

Browse files
committed
Merge pull request #230 from graphql/diverging-directives
[validation] Allow differing directives.
2 parents f010e86 + 9b11df2 commit a738c6d

File tree

2 files changed

+12
-91
lines changed

2 files changed

+12
-91
lines changed

src/validation/__tests__/OverlappingFieldsCanBeMerged.js

Lines changed: 12 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,18 @@ describe('Validate: Overlapping fields can be merged', () => {
8686
`);
8787
});
8888

89+
it('different skip/include directives accepted', () => {
90+
// Note: Differing skip/include directives don't create an ambiguous return
91+
// value and are acceptable in conditions where differing runtime values
92+
// may have the same desired effect of including or skipping a field.
93+
expectPassesRule(OverlappingFieldsCanBeMerged, `
94+
fragment differentDirectivesWithDifferentAliases on Dog {
95+
name @include(if: true)
96+
name @include(if: false)
97+
}
98+
`);
99+
});
100+
89101
it('Same aliases with different field targets', () => {
90102
expectFailsRule(OverlappingFieldsCanBeMerged, `
91103
fragment sameAliasesWithDifferentFieldTargets on Dog {
@@ -191,66 +203,6 @@ describe('Validate: Overlapping fields can be merged', () => {
191203
`);
192204
});
193205

194-
it('conflicting directives', () => {
195-
expectFailsRule(OverlappingFieldsCanBeMerged, `
196-
fragment conflictingDirectiveArgs on Dog {
197-
name @include(if: true)
198-
name @skip(if: false)
199-
}
200-
`, [
201-
{ message: fieldsConflictMessage(
202-
'name',
203-
'they have differing directives'
204-
),
205-
locations: [ { line: 3, column: 9 }, { line: 4, column: 9 } ] }
206-
]);
207-
});
208-
209-
it('conflicting directive args', () => {
210-
expectFailsRule(OverlappingFieldsCanBeMerged, `
211-
fragment conflictingDirectiveArgs on Dog {
212-
name @include(if: true)
213-
name @include(if: false)
214-
}
215-
`, [
216-
{ message: fieldsConflictMessage(
217-
'name',
218-
'they have differing directives'
219-
),
220-
locations: [ { line: 3, column: 9 }, { line: 4, column: 9 } ] }
221-
]);
222-
});
223-
224-
it('conflicting args with matching directives', () => {
225-
expectFailsRule(OverlappingFieldsCanBeMerged, `
226-
fragment conflictingArgsWithMatchingDirectiveArgs on Dog {
227-
doesKnowCommand(dogCommand: SIT) @include(if: true)
228-
doesKnowCommand(dogCommand: HEEL) @include(if: true)
229-
}
230-
`, [
231-
{ message: fieldsConflictMessage(
232-
'doesKnowCommand',
233-
'they have differing arguments'
234-
),
235-
locations: [ { line: 3, column: 9 }, { line: 4, column: 9 } ] }
236-
]);
237-
});
238-
239-
it('conflicting directives with matching args', () => {
240-
expectFailsRule(OverlappingFieldsCanBeMerged, `
241-
fragment conflictingDirectiveArgsWithMatchingArgs on Dog {
242-
doesKnowCommand(dogCommand: SIT) @include(if: true)
243-
doesKnowCommand(dogCommand: SIT) @skip(if: false)
244-
}
245-
`, [
246-
{ message: fieldsConflictMessage(
247-
'doesKnowCommand',
248-
'they have differing directives'
249-
),
250-
locations: [ { line: 3, column: 9 }, { line: 4, column: 9 } ] }
251-
]);
252-
});
253-
254206
it('encounters conflict in fragments', () => {
255207
expectFailsRule(OverlappingFieldsCanBeMerged, `
256208
{

src/validation/rules/OverlappingFieldsCanBeMerged.js

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import type {
1515
SelectionSet,
1616
Field,
1717
Argument,
18-
Directive
1918
} from '../../language/ast';
2019
import { FIELD, INLINE_FRAGMENT, FRAGMENT_SPREAD } from '../../language/kinds';
2120
import { print } from '../../language/printer';
@@ -136,14 +135,6 @@ export function OverlappingFieldsCanBeMerged(context: ValidationContext): any {
136135
];
137136
}
138137

139-
if (!sameDirectives(ast1.directives || [], ast2.directives || [])) {
140-
return [
141-
[ responseName, 'they have differing directives' ],
142-
[ ast1 ],
143-
[ ast2 ]
144-
];
145-
}
146-
147138
var selectionSet1 = ast1.selectionSet;
148139
var selectionSet2 = ast2.selectionSet;
149140
if (selectionSet1 && selectionSet2) {
@@ -209,28 +200,6 @@ type ConflictReason = [ string, ConflictReasonMessage ];
209200
// Reason is a string, or a nested list of conflicts.
210201
type ConflictReasonMessage = string | Array<ConflictReason>;
211202

212-
function sameDirectives(
213-
directives1: Array<Directive>,
214-
directives2: Array<Directive>
215-
): boolean {
216-
if (directives1.length !== directives2.length) {
217-
return false;
218-
}
219-
return directives1.every(directive1 => {
220-
var directive2 = find(
221-
directives2,
222-
directive => directive.name.value === directive1.name.value
223-
);
224-
if (!directive2) {
225-
return false;
226-
}
227-
return sameArguments(
228-
directive1.arguments || [],
229-
directive2.arguments || []
230-
);
231-
});
232-
}
233-
234203
function sameArguments(
235204
arguments1: Array<Argument>,
236205
arguments2: Array<Argument>

0 commit comments

Comments
 (0)