Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/plugin/src/rules/require-id-when-available.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ const rule: GraphQLESLintRule<RequireIdWhenAvailableRuleConfig, true> = {
}

const typeInfo = node.typeInfo();

if (typeInfo && typeInfo.gqlType) {
const rawType = getBaseType(typeInfo.gqlType);
if (rawType instanceof GraphQLObjectType || rawType instanceof GraphQLInterfaceType) {
Expand All @@ -59,7 +58,16 @@ const rule: GraphQLESLintRule<RequireIdWhenAvailableRuleConfig, true> = {
s => s.kind === 'Field' && s.name.value === fieldName
);

if (!hasIdFieldInSelectionSet) {
// check if the parent selection set has the ID field in there
const { parent } = node as any;
const hasIdFieldInInterfaceSelectionSet =
parent &&
parent.kind === 'InlineFragment' &&
parent.parent &&
parent.parent.kind === 'SelectionSet' &&
!!parent.parent.selections.find(s => s.kind === 'Field' && s.name.value === fieldName);

if (!hasIdFieldInSelectionSet && !hasIdFieldInInterfaceSelectionSet) {
context.report({
loc: {
start: {
Expand Down
22 changes: 22 additions & 0 deletions packages/plugin/tests/require-id-when-available.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,32 @@ const TEST_SCHEMA = /* GraphQL */ `
type Query {
hasId: HasId!
noId: NoId!
vehicles: [Vehicle!]!
flying: [Flying!]!
}

type NoId {
name: String!
}

interface Vehicle {
id: ID!
}

type Car implements Vehicle {
id: ID!
mileage: Int
}

interface Flying {
hasWings: Boolean!
}

type Bird implements Flying {
id: ID!
hasWings: Boolean!
}

type HasId {
id: ID!
name: String!
Expand All @@ -24,6 +44,8 @@ ruleTester.runGraphQLTests('require-id-when-available', rule, {
valid: [
{ ...WITH_SCHEMA, code: `query { noId { name } }` },
{ ...WITH_SCHEMA, code: `query { hasId { id name } }` },
{ ...WITH_SCHEMA, code: `query { vehicles { id ...on Car { mileage } } }` },
{ ...WITH_SCHEMA, code: `query { flying { ...on Bird { id } } }` },
{
...WITH_SCHEMA,
code: `query { hasId { name } }`,
Expand Down