Graphql-code-generator: schema-ast produces empty GraphQL docblock comments

Created on 19 Jul 2020  路  15Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug

For some of my types, the ast-schema plugin generates docblocks comments that are empty:
image

Some types that do not have a description do not have those empty comments.

That wouldn't be much annoying if they didn't make the parser of the JS GraphQL WebStorm plugin fail the parsing (to workaround, I am now using commentDescriptions: true and everything is fine).

  1. My codegen.yml config file:
schema: "http://localhost:3001/graphql"

generates:
  schema.graphql:
    plugins:
      - schema-ast

Expected behavior

I guess the generator does not put a comment block if it receives null for the comment from the introspection query.
It should also do the same for empty strings.

Environment:

  • @graphql-codegen/...: everything in version 1.17.0
waiting-for-answer

Most helpful comment

It should also do the same for empty strings.

If the introspection query is returning an empty string instead of null for a description, then the issue is with the schema itself and not with codegen or graphql-tools. An empty string is not the same as "no description" and the expected behavior when printing a schema with such a description would be to print the provided value.

Example

All 15 comments

@ardatan I think maybe it's related to graphql-tools?

I couldn't reproduce it. Could you reproduce rhe issue using CodeSandbox? @toverux

It should also do the same for empty strings.

If the introspection query is returning an empty string instead of null for a description, then the issue is with the schema itself and not with codegen or graphql-tools. An empty string is not the same as "no description" and the expected behavior when printing a schema with such a description would be to print the provided value.

Example

@danielrearden is right. @toverux can you please share a reproduction of the issue? How do you fetch your schema? can you please share more details about your GraphQL server implementation?

I can see there is difference between my code generated with "graphql": "^14.7.0", and "graphql": "^15.3.0",. Since I've upgraded my project, generated GQL Schema is completely reshuffled and comments are suddenly not null, but "".

When I downgrade back to "graphql": "^14.7.0", the generated schema is fixed again :man_shrugging:

So my suspicion is that the problem is in graphql itself.

I'm seeing the same thing as @Eddman described. I've tested this with two different GraphQL-implementations. One is based on https://github.com/99designs/gqlgen and one on https://graphene-python.org/.

Both produce the same thing when using [email protected], but it looks normal when [email protected] is used.

Edit:

I created a workaround for this issue with a hook on MacOS:

  schema.graphql:
    plugins:
      - schema-ast
    config:
      commentDescriptions: true
      includeDirectives: true
    hooks:
      afterOneFileWrite:
        # Todo: remove this hook once https://git.io/JJod4 has been resolved.
        - perl -i -pe 's/\s*""""""\n//g'

You could also use sed with something like: sed -i '' -e ':a' -e 'N' -e '$!ba' -e 's/[ ]*""""""\n//g', but I think perl is more suitable for the job.

@Eddman and @Zn4rK are right. I think it's coming from graphql itself. Note that one of the changes related to it is comments implementation. In v14 it was done with # and then it was changed to " and the old method is not deprecated, but still can be turned on (for printing only) using commentDescriptions.

I just pushed a fix for graphql-tools to support commentDescriptions also while parsing, to make sure comments are being converted into descriptions during the parse flow. Not sure if it will effect your issue, but it worth a try.

@Zn4rK how does it work without commentDescriptions: true ?

I managed to reproduce it 馃帀

I think introspection JSON or GraphQL servers running v14 of graphql, will dump the schema with "" as descriptions. Then, for graphql-tools is looks like a complete valid value, and it passes it to the graphql printer for printing, which results in empty comment.
You are right that this comes from graphql. I can apply a fix in our loaders to eliminate empty descriptions.

@Eddman @Zn4rK can you please check your introspection file? which source are you using for schema? does it return "" in description field? or it's set to undefined/null?

Closing. If someone is still having this issue with graphql@15, please let me know.

I'm having this problem with graphql@15, here's my package.json dependencies:

{
    "@graphql-cli/codegen": "^1.17.10",
    "@graphql-codegen/schema-ast": "^1.17.8",
    "graphql": "^15.3.0",
    "graphql-cli": "^4.0.0"
  }

Brand new project, no other dependencies, any ideas?

@dan-j please share a reproduction

@dotansimha I'm wondering why is this one closed when there was no new graphql released, nor any fix introduced in graphql-code-generator?

BTW with graphql@14 I have "description": null, with graphql@15 it is "description": "",

Direct introspection query execution against our endpoint returns "description": "", so "" is replaced with null in @14.

@dotansimha Any news here?

@Eddman Could you create a new issue with a reproduction?

@ardatan #4920

Was this page helpful?
0 / 5 - 0 ratings