Skip to content

Commit bcc1746

Browse files
authored
[require-id-when-available] fix for inline fragments on interfaces (#139)
* breaking test case * breaking case * fix tests
1 parent d4ffa5d commit bcc1746

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

packages/plugin/src/rules/require-id-when-available.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ const rule: GraphQLESLintRule<RequireIdWhenAvailableRuleConfig, true> = {
4747
}
4848

4949
const typeInfo = node.typeInfo();
50-
5150
if (typeInfo && typeInfo.gqlType) {
5251
const rawType = getBaseType(typeInfo.gqlType);
5352
if (rawType instanceof GraphQLObjectType || rawType instanceof GraphQLInterfaceType) {
@@ -59,7 +58,16 @@ const rule: GraphQLESLintRule<RequireIdWhenAvailableRuleConfig, true> = {
5958
s => s.kind === 'Field' && s.name.value === fieldName
6059
);
6160

62-
if (!hasIdFieldInSelectionSet) {
61+
// check if the parent selection set has the ID field in there
62+
const { parent } = node as any;
63+
const hasIdFieldInInterfaceSelectionSet =
64+
parent &&
65+
parent.kind === 'InlineFragment' &&
66+
parent.parent &&
67+
parent.parent.kind === 'SelectionSet' &&
68+
!!parent.parent.selections.find(s => s.kind === 'Field' && s.name.value === fieldName);
69+
70+
if (!hasIdFieldInSelectionSet && !hasIdFieldInInterfaceSelectionSet) {
6371
context.report({
6472
loc: {
6573
start: {

packages/plugin/tests/require-id-when-available.spec.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,32 @@ const TEST_SCHEMA = /* GraphQL */ `
55
type Query {
66
hasId: HasId!
77
noId: NoId!
8+
vehicles: [Vehicle!]!
9+
flying: [Flying!]!
810
}
911
1012
type NoId {
1113
name: String!
1214
}
1315
16+
interface Vehicle {
17+
id: ID!
18+
}
19+
20+
type Car implements Vehicle {
21+
id: ID!
22+
mileage: Int
23+
}
24+
25+
interface Flying {
26+
hasWings: Boolean!
27+
}
28+
29+
type Bird implements Flying {
30+
id: ID!
31+
hasWings: Boolean!
32+
}
33+
1434
type HasId {
1535
id: ID!
1636
name: String!
@@ -24,6 +44,8 @@ ruleTester.runGraphQLTests('require-id-when-available', rule, {
2444
valid: [
2545
{ ...WITH_SCHEMA, code: `query { noId { name } }` },
2646
{ ...WITH_SCHEMA, code: `query { hasId { id name } }` },
47+
{ ...WITH_SCHEMA, code: `query { vehicles { id ...on Car { mileage } } }` },
48+
{ ...WITH_SCHEMA, code: `query { flying { ...on Bird { id } } }` },
2749
{
2850
...WITH_SCHEMA,
2951
code: `query { hasId { name } }`,

0 commit comments

Comments
 (0)