Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prettier throws error for graphql files with only imports/comments #6145

Open
inlightmedia opened this issue May 23, 2019 · 1 comment
Open
Labels
lang:graphql Issues affecting GraphQL status:needs discussion Issues needing discussion and a decision to be made before action can be taken

Comments

@inlightmedia
Copy link

inlightmedia commented May 23, 2019

Prettier 1.17.1
Playground link

--parser graphql

Input:

# import Query.* from "user.graphql"
# import Query.* from "accountRequest.graphql"
# import Query.* from "providerCompany.graphql"
# import Query.* from "service.graphql"
# import Query.* from "activeServicePeriod.graphql"
# import Query.* from "supportTicket.graphql"
# import Query.* from "booking.graphql"
# import Query.* from "purchasedService.graphql"
# import Query.* from "messages.graphql"
# import Query.* from "notification.graphql"
# import Query.* from "emailCronJobs.graphql"
# import Query.* from "provider.graphql"
# import Query.* from "bookingCronJobs.graphql"

Output:

SyntaxError: Syntax Error: Unexpected <EOF> (13:48)
  11 | # import Query.* from "emailCronJobs.graphql"
  12 | # import Query.* from "provider.graphql"
> 13 | # import Query.* from "bookingCronJobs.graphql"
     |                                                ^

Expected behavior:
I use imports in graphql files for my index.graphql files to import all my named type definiton files. This is valid syntax.

Actual behavior:

If the file only has imports and no defined queries or type definitions like below:

# import Query.* from "example.graphql"
# import Query.* from "other.graphql"
# import Query.* from "bar.graphql"

Prettier throws an error: SyntaxError: Syntax Error: Unexpected <EOF>

I just added all my index.graphql files to my prettier ignore to allow my app to run (it was breaking my CI/CD) but it's sad that I lose all the formatting benefits for those files.

@lydell lydell changed the title Prettier throws error for graphql files with only imports Prettier throws error for graphql files with only imports/comments May 23, 2019
@lydell lydell added lang:graphql Issues affecting GraphQL status:needs discussion Issues needing discussion and a decision to be made before action can be taken labels May 23, 2019
@bmalehorn
Copy link
Contributor

It turns out empty files are not valid graphQL, just like empty files are not valid JSON. And # import statements are a language extension - they're parsed as comments. To graphQL, you're just defining a big useless file full of comments, and why would you want to do that?

Anyway, there are a few solutions:

  1. check if graphQL files are comment-only, if so, do nothing
  2. change the graphql package to accept an { allowEmpty: true } flag

1 won't format comment-only files (collapse multiple empty lines, remove trailing whitespace, etc.), and will still fail on empty files containing """descriptions""". But it will stop the crashes, which is the main problem here.

2 is the "right way" to fix this, but it might be a tough sell to add a special parsing mode for an unofficial language extension. It also requires coordination across a couple repos.

My vote is for 1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lang:graphql Issues affecting GraphQL status:needs discussion Issues needing discussion and a decision to be made before action can be taken
Projects
None yet
Development

No branches or pull requests

3 participants