graphql-eslint icon indicating copy to clipboard operation
graphql-eslint copied to clipboard

rule to require selecting __typename

Open joshribakoff-sm opened this issue 2 years ago • 3 comments

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.

joshribakoff-sm avatar Sep 15 '22 18:09 joshribakoff-sm

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

dimaMachina avatar Sep 15 '22 18:09 dimaMachina

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)

joshribakoff-sm avatar Sep 15 '22 19:09 joshribakoff-sm

Please take a look yet another time to documentation, fieldName option accept either string or array of strings

dimaMachina avatar Sep 15 '22 19:09 dimaMachina

closing for now, feel free to ask if you still need help

dimaMachina avatar Sep 23 '22 23:09 dimaMachina

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.

alexeyr-ci avatar Jan 05 '24 09:01 alexeyr-ci