-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(rule): Add rule to check that
id
is only used for Relay IDs
In Relay, `id` is [treated as a reserved word and must be a globally unique value](facebook/relay#1682 (comment)). That means when an `id` field is present on a type, it must be the type that Relay expects (a string type). This linter assumes that an `ID` scalar is present in the schema, and checks that if an `id` field is present, it is using that type.
- Loading branch information
Showing
3 changed files
with
117 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { | ||
ASTVisitor, | ||
FieldDefinitionNode, | ||
ValidationContext, | ||
} from 'graphql'; | ||
|
||
import { ValidationError } from 'graphql-schema-linter'; | ||
import { isNamedTypeNode, isNonNullTypeNode } from '../utils'; | ||
|
||
function isProperRelayIdField(field: FieldDefinitionNode): boolean { | ||
return ( | ||
isNonNullTypeNode(field.type) && | ||
isNamedTypeNode(field.type.type) && | ||
field.type.type.name.value == 'ID' | ||
); | ||
} | ||
|
||
export function RelayIdFieldType(context: ValidationContext): ASTVisitor { | ||
return { | ||
ObjectTypeDefinition(node) { | ||
const idField = node.fields?.find( | ||
(field: FieldDefinitionNode) => field.name.value === 'id', | ||
); | ||
if (idField && !isProperRelayIdField(idField)) { | ||
context.reportError( | ||
new ValidationError( | ||
'relay-id-field-type', | ||
`The "id" field on \`${node.name.value}\` must be a proper Relay ID field type, or renamed.`, | ||
[node], | ||
), | ||
); | ||
} | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { RelayIdFieldType } from '../../src/rules/relayIdFieldType'; | ||
|
||
import { gql } from '../utils'; | ||
|
||
import { expectPassesRule, expectFailsRule } from '../assertions'; | ||
|
||
describe('RelayIdFieldType rule', () => { | ||
it('allows types that have no id field', () => { | ||
expectPassesRule( | ||
RelayIdFieldType, | ||
gql` | ||
type Entity { | ||
entityId: String! | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('allows types with a correct id field', () => { | ||
expectPassesRule( | ||
RelayIdFieldType, | ||
gql` | ||
type Entity { | ||
id: ID! | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('allows types to use ID on other fields', () => { | ||
expectPassesRule( | ||
RelayIdFieldType, | ||
gql` | ||
type Entity { | ||
myId: ID! | ||
} | ||
`, | ||
); | ||
}); | ||
|
||
it('disallows types with a wrong id field type', () => { | ||
expectFailsRule( | ||
RelayIdFieldType, | ||
gql` | ||
type Entity { | ||
id: String! | ||
} | ||
`, | ||
[ | ||
{ | ||
message: | ||
'The "id" field on `Entity` must be a proper Relay ID field type, or renamed.', | ||
locations: [{ line: 2, column: 9 }], | ||
}, | ||
], | ||
); | ||
}); | ||
|
||
it('disallows types with a non-nullable id field type', () => { | ||
expectFailsRule( | ||
RelayIdFieldType, | ||
gql` | ||
type Entity { | ||
id: ID | ||
} | ||
`, | ||
[ | ||
{ | ||
message: | ||
'The "id" field on `Entity` must be a proper Relay ID field type, or renamed.', | ||
locations: [{ line: 2, column: 9 }], | ||
}, | ||
], | ||
); | ||
}); | ||
}); |