Skip to content

rule to require selecting __typename #1159

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
joshribakoff-sm opened this issue Sep 15, 2022 · 5 comments
Closed

rule to require selecting __typename #1159

joshribakoff-sm opened this issue Sep 15, 2022 · 5 comments

Comments

@joshribakoff-sm
Copy link

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.

@dimaMachina
Copy link
Contributor

dimaMachina commented Sep 15, 2022

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

@joshribakoff-sm
Copy link
Author

joshribakoff-sm commented Sep 15, 2022

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)

@dimaMachina
Copy link
Contributor

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

@dimaMachina
Copy link
Contributor

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

@alexeyr-ci
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants