graphql-code-generator: Unable to use with create-react-app

Describe the bug I generated a react app with create-react-app 3.0. This has a predefined ESLint config which uses the ‘import/first’ rule. This defines that imports must always be a the top of the file. But the generated code that I get from graphql-code-generator contains imports that are in the middle of the file. Therefore the app can not compile.

To Reproduce Steps to reproduce the behavior:

  1. My GraphQL schema:

Not really relevant

  1. My GraphQL operations:

Not really relevant

  1. My codegen.yml config file:
schema: http://localhost:8080/graphql
documents:
  - ./src/**/*.tsx
  - ./src/**/*.ts
overwrite: true
generates:
  ./src/types.d.tsx:
    plugins:
      - typescript-common
      - typescript-client
      - typescript-react-apollo
    config:
      withHooks: true
      withHOC: false
      withComponent: false

Expected behavior I expect the import statements to be at the top of the file.

Environment:

  • OS: MacOs
  • @graphql-codegen/...: 1.1.1
  • NodeJS:

Additional context Part of the generated code that is wrong:

// ====================================================
// Documents
// ====================================================

export namespace Login {
  export type Variables = {
    email: string;
    password: string;
  };

  export type Mutation = {
    __typename?: "Mutation";

    login: Maybe<Login>;
  };

  export type Login = {
    __typename?: "LoginOutput";

    user: Maybe<User>;
  };

  export type User = {
    __typename?: "User";

    id: string;
  };
}

import gql from "graphql-tag"; // <-----------
import * as ReactApolloHooks from "react-apollo-hooks"; // <-----------
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;

export const LoginDocument = gql`
  mutation login($email: String!, $password: String!) {
    login(input: { email: $email, password: $password }) {
      user {
        id
      }
    }
  }
`;

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 21 (5 by maintainers)

Most upvoted comments

In case it is helpful I will describe my workaround for this issue. I use the @graphql-codegen/add plugin to insert an /* eslint-disable */ line in the generated file which causes eslint to skip checking the file. The configuration looks like this:

schema: http://localhost:8080/graphql
documents:
  - ./src/**/*.tsx
  - ./src/**/*.ts
overwrite: true
generates:
  ./src/types.d.tsx:
    plugins:
      - add: '/* eslint-disable */'
      - typescript-common
      - typescript-client
      - typescript-react-apollo
    config:
      withHooks: true
      withHOC: false
      withComponent: false

The relevant added line is - add: '/* eslint-disable */ in the plugins: section.

@hallettj Import in body of module; reorder to top import/first. However, it works for the below config, similar to what you’ve suggested earlier.

schema: http://localhost:4000/graphql
documents: src/graphql/**/*.graphql
overwrite: true
generates:
  src/generated/graphql.tsx:
    plugins:
      - add: '/* eslint-disable */'
      - "typescript"
      - "typescript-operations"
      - "typescript-react-apollo"
    config:
      withComponent: true
      withHooks: true
      withHOC: false

Finally it was a problem of unnamed queries, as soon as I named the query, I got the component generated. Now I’m getting ts errors on the signature of the children function of the provider component, but I think that should be easier to fix. A better documentation would be nice though. If I manage to get it working I may do a PR over the docs to explain how I got it working.

@diegopamio it seems like a different issue, can you please open a new issue with as much information as possible (and if you can, a small repo with a reproduction)? Thanks

@hallettj I agree that this might become useful. I opened an issue for it: https://github.com/dotansimha/graphql-code-generator/issues/1849

Thank you!