Graphql-code-generator: "prettier --write" hook is not working

Created on 5 Nov 2019  路  9Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug

I am using the 'near-operation-file' preset to generate types next to the files including GraphQL operations. I have also added an afterAllFileWrite hook to run prettier on the generated files but prettier seems to be run only on one of the two files that get generated.

I have also tried using an afterOneFileWrite hook instead, but the behavior is the same.

To Reproduce
Steps to reproduce the behavior:

  • Use the following config.
  • Create two .ts files each one including a GraphQL query.
  • Add some prettier rules to be run on the generated types.
  • Generate the types using @graphql-codegen.

My codegen.yml config file:

overwrite: true
schema: 'http://localhost:24000/graphql'
documents:
  - './pages/**/*.{ts,tsx}'
  - './components/**/*.{ts,tsx}'
generates:
  ./generated/graphql/types.ts:
    plugins:
      - 'typescript'
  ./:
    preset: 'near-operation-file'
    presetConfig:
      extension: '.gql.types.tsx'
      baseTypesPath: 'generated/graphql/types.ts'
    plugins:
      - 'typescript-operations'
      - 'typescript-react-apollo'
    config:
      withHooks: true
hooks:
  afterAllFileWrite:
    - prettier --write

Expected behavior

The lifecycle hook should be run for all generated files, not just for one of them.

Environment:

  • OS: macOS 10.14.5
  • @graphql-codegen: 1.8.3
  • Node: 11.10.1

Additional context

N/A

bug core waiting-for-answer

Most helpful comment

@dotansimha I just run into this issue. You're correct, it only works if the hook in the root-level position. Is that intended by design?

All 9 comments

Thanks @vasilak !
We'll check that.

For me prettier does not work with two outputs too. It works if i remove all text after - prettier --write

overwrite: true
schema: "http://localhost:4000"
documents: null
generates:
  src/types/graphql.ts:
    config:
      interfacePrefix: "I"
      typesPrefix: "I"
      skipTypename: true
      declarationKind: "interface"
      noNamespaces: true
      withHooks: true
    plugins:
      - add: '/* tslint:disable:jsdoc-format */'
      - typescript
      - typescript-operations
      - typescript-compatibility
    hooks:
      afterOneFileWrite:
        - prettier --write
  ./graphql.schema.json:
    plugins:
      - introspection

Just tested it with our examples in this repo, and it seems like it works.
I changed some of the prettier rules and ran in again, it seems like new rules are being applied:

image

I think it's related to the level/position of your hooks, can you please try putting it in your root-level of your file?

Closing for now.
@vasilak feel free to contact us if it's still relevant :)

@dotansimha I just run into this issue. You're correct, it only works if the hook in the root-level position. Is that intended by design?

Actually, it doesn't work for me on root level...

overwrite: true
schema: 'http://localhost:8000/graphql'
documents: 'src/**/*.gql'
generates:
  src/__generated__/graphql.ts:
    plugins:
      - 'typescript'
      - 'typescript-operations'
      - 'typescript-react-apollo'
      - add:
          content: '/* eslint-disable */'
    config:
      reactApolloVersion: 2
  ./graphql.schema.json:
    plugins:
      - 'introspection'

hooks:
  afterAllFileWrite:
    - npx prettier --write

Tried both with and without npx, same result. In another project where I use yarn, it worked like a charm.

Interestingly, if I move the hook to the file level (because I only need to format the .ts file), change it to afterOneFileWrite, it doesn't throw any error if the command is prettier --write, even though my prettier isn't global. Obviously, it does nothing. If I make it npx prettier --write, it throws an error and does nothing

@JaffParker please make sure prettier is not ignoring those files... if it's still relevant, can you please report this in a new issue with a reproduction?

Had the same problem but the reason was quite arbitrary - when using the typescript-resolvers plugin, I was using the config: context field with a bad path value (always forget it's relative to the generated file!).

鉃★笍 Once this was fixed with the correct path to context, prettier and eslint started working again :)

Was this page helpful?
0 / 5 - 0 ratings