Graphql-code-generator: Unable to prepend multi-line comments via `add` plugin

Created on 11 Jul 2019  路  3Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug

I noticed a few bizarre behaviors in the add plugin while upgrading from v1.1.3 to v1.3.1.

  • When outputting as a GraphQL file, a multi-line comment output i.e. """ sometimes does not get emitted.
  • When outputting as a TypeScript file, a multi-line comment somehow gets wrapped up with the import that's needed from the typescript plugin i.e. import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';

To Reproduce the GraphQL output issue

Steps to reproduce the behavior: Use either of these codegen.yml config files.

overwrite: true
schema: 'schemas/*.graphql'
generates:
  ./foo-schema.graphql:
    plugins:
      - add: '"""'
      - add: 'NOTE: THIS IS AN AUTO-GENERATED FILE. DO NOT MODIFY IT DIRECTLY.'
      - add: 'Blah blah blah, more documentation.'
      - add: '"""'
overwrite: true
schema: 'schemas/*.graphql'
generates:
  ./foo-schema.graphql:
    plugins:
      - add:
          content:
            - '"""'
            - 'NOTE: THIS IS AN AUTO-GENERATED FILE. DO NOT MODIFY IT DIRECTLY.'
            - 'Blah blah blah, more documentation.'
            - '"""'

Expected behavior of the GraphQL output issue


Expected output:

"""
NOTE: THIS IS AN AUTO-GENERATED FILE. DO NOT MODIFY IT DIRECTLY.
Blah blah blah, more documentation.
"""

Actual output:

"""
NOTE: THIS IS AN AUTO-GENERATED FILE. DO NOT MODIFY IT DIRECTLY.
Blah blah blah, more documentation.

Note that the 2nd """ gets dropped here.

To Reproduce the TypeScript output issue

Steps to reproduce the behavior: Use the below codegen.yml config file.

overwrite: true
schema: 'schemas/*.graphql'
generates:
  ./graphql-schema.ts:
    plugins:
      - add:
          content:
            - '/**'
            - ' * NOTE: THIS IS AN AUTO-GENERATED FILE. DO NOT MODIFY DIRECTLY.'
            - ' */'
      - typescript:
          scalars:
            DateTime: Date
      - typescript-resolvers

Expected behavior of the TypeScript output issue

Expected output:

/**
 * NOTE: THIS IS AN AUTO-GENERATED FILE. DO NOT MODIFY DIRECTLY.
 */
import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';

...

Actual output:

/**
import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';
 * NOTE: THIS IS AN AUTO-GENERATED FILE. DO NOT MODIFY DIRECTLY.
 */

...
bug core waiting-for-release

Most helpful comment

For those waiting for the fix to be released, multiline string yaml is working fine:

definitions:
  add: &top-comment
    content: >
      /**
       * NOTE: THIS IS AN AUTO-GENERATED FILE. DO NOT MODIFY IT DIRECTLY.
       */

      /* eslint-disable */

generates:
  types/introspection-result.ts:
    plugins:
      - add: *top-comment
      - 'fragment-matcher'
  types/graphql.ts:
    plugins:
      - add: *top-comment
      - 'typescript'

All 3 comments

For those waiting for the fix to be released, multiline string yaml is working fine:

definitions:
  add: &top-comment
    content: >
      /**
       * NOTE: THIS IS AN AUTO-GENERATED FILE. DO NOT MODIFY IT DIRECTLY.
       */

      /* eslint-disable */

generates:
  types/introspection-result.ts:
    plugins:
      - add: *top-comment
      - 'fragment-matcher'
  types/graphql.ts:
    plugins:
      - add: *top-comment
      - 'typescript'

Fixed in 1.5.0 馃帀

Was this page helpful?
0 / 5 - 0 ratings