graphql-eslint
graphql-eslint copied to clipboard
rule to require selecting __typename
graphql-codegen
generates types that imply __typename
will always be present, but that's only the case if you actually fetch that field. Therefore, it would be nice to have a lint rule that forces all queries to select __typename
on every type.
Hi! Do you tried this rule? https://github.com/B2o5T/graphql-eslint/blob/master/docs/rules/require-id-when-available.md#asarray-array This rule will be renamed in next major version since not only id
field can be required but any field you want
Seems like it should work, but from a quick look it seems like I could only require one field (eg. not both id
and __typename
)
Also, there seems to maybe be a possible feature request for the graphql-codegen
to not assume __typename
will be present, unless it is actually selected. I wasn't sure if I should file a symmetrical issue there on the other repo, I'm assuming the maintainers are the same :) -- but forcing all fields to select __typename
could be overkill (really it's only necessary for union types)
Please take a look yet another time to documentation, fieldName
option accept either string or array of strings
closing for now, feel free to ask if you still need help
It looks like requiring __typename
even on its own doesn't work (at least in 3.20.1). I set
'@graphql-eslint/require-id-when-available': [
'error',
{
fieldName: ['__typename'],
},
]
removed __typename
from a query which had it (so it's definitely available), but the rule didn't report any errors.