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"})
  1. My GraphQL schema:
type Query {
    user(id: ID!): User!
}
  1. My GraphQL operations:
query user {
    user(id: 1) {
        id
        username
        email
    }
}
  1. My codegen.yml config 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)

Most upvoted comments

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/add plugin and do something like that:

generates:
  my-types.ts:
    plugins:
      - add: '/* eslint-disable */'
      - typescript
      - typescript-resolvers

And btw, if you are not using schema-stitching resolvers, you can turn it off by adding:

generates:
  my-types.ts:
    config:
      noSchemaStitching: true
    plugins:
      - add: '/* eslint-disable */'
      - typescript
      - typescript-resolvers

@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-eslint is exposing a real bug here.

I believe you should take a similar approach to before and replace

export type Resolver<TResult, TParent = {}, TContext = {}, TArgs = {}> =
  | ResolverFn<TResult, TParent, TContext, TArgs>
  | StitchingResolver<TResult, TParent, TContext, TArgs>;

with

type Empty = Exact<{ [key: string]: never; }>
export type Resolver<TResult, TParent = Empty, TContext = Empty, TArgs = Empty> =
  | ResolverFn<TResult, TParent, TContext, TArgs>
  | StitchingResolver<TResult, TParent, TContext, TArgs>;

Don’t know if it’s related to this issue, but I’m still getting TS @typescript-eslint/ban-types errors like in the following:

export type Resolver<TResult, TParent = {}, TContext = {}, TArgs = {}> =
  | ResolverFn<TResult, TParent, TContext, TArgs>
  | StitchingResolver<TResult, TParent, TContext, TArgs>;

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 be Exact<{ [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 Exact is 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