graphql-code-generator: Incorrect no-variables type
Describe the bug
If query takes no arguments, its variables generates as {}, which means any non-nullish value
So we can use it like this
useQueryWithoutVariables({variables: "any value"})
- My GraphQL schema:
type Query {
user(id: ID!): User!
}
- My GraphQL operations:
query user {
user(id: 1) {
id
username
email
}
}
- My
codegen.ymlconfig file:
schema: schema.graphql
documents: document.graphql
generates:
types.ts:
plugins:
- typescript
- typescript-operations
Expected behavior
No variables allowed, type should be never
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 1
- Comments: 23 (11 by maintainers)
Fixed in
v1.15.3.@lewebsimple I don’t thing it’s related to this PR, it seems like something else.
No need to run lint rules on generates files. It’s endless, because everyone prefers something else, and we can’t align to all requirements 😃
I recommend you to use
@graphql-codegen/addplugin and do something like that:And btw, if you are not using schema-stitching resolvers, you can turn it off by adding:
@dotansimha I’m not sure this should be disregarded.
: {}means “any non-nullish value”. This is likely NOT what the authors of this package intended. This goes beyond “everyone prefers something else”. I believe@typescript-eslintis exposing a real bug here.I believe you should take a similar approach to before and replace
with
Don’t know if it’s related to this issue, but I’m still getting TS
@typescript-eslint/ban-typeserrors like in the following:Can I safely ignore that ?
@lewebsimple It should be available now (
v1.15.3), sorry for the delay, we had a minor bug in our CI 😄We changed
{}to beExact<{ [key: string]: never; }>, this will make sure that no other fields are being used, and also gives a nice TS error when field isn’t known.https://github.com/dotansimha/graphql-code-generator/pull/4201
@dotansimha wait, wait, wait. It doesn’t work. I’ve tested alpha-version and wrote that
Exactis wrapping only types with some fields. I still have just{}in query without variables. Why did you release that?@dotansimha or like this
We know in advance if there are variables or not
@dotansimha LGTM