graphql / graphiql

GraphiQL & the GraphQL LSP Reference Ecosystem for building browser & IDE tools.
MIT License
16.03k stars 1.72k forks source link

[lsp-server] 🐞 The language server throws "could not parse the TypeScript file" error for every TS file when `documents` field is used #3415

Open HosseinAgha opened 1 year ago

HosseinAgha commented 1 year ago

Is there an existing issue for this?

Current Behavior

The language server throws Could not parse the TypeScript file. SyntaxError: Unexpected token, expected "," errors when I use documents section in GraphQL config.
The Config:

projects:
  apps:
    schema: https://localhost:8080/graphql
    documents:
      - ./app/**/*.{ts,tsx}
      - '!(app/operations/generated/*)'
      - '!(app/operations/gqlApolloHooks.ts)'

The Other Config That I tested:

projects:
  apps:
    schema: https://localhost:8080/graphql
    documents:
      - ./app/**/*.{ts,tsx}
      - '!./app/operations/generated/*.{ts,tsx}'
      - '!./app/operations/gqlApolloHooks.ts'

And here is a trick config that works:

projects:
  apps:
    schema: https://localhost:8080/graphql
    include:
      - ./app/**/*.{ts,tsx}
    exclude:
      - './app/operations/generated/*.{ts,tsx}'
      - './app/operations/gqlApolloHooks.ts'

Expected Behavior

It should not throw TypeScript errors and just work.

Steps To Reproduce

No response

Environment

Anything else?

No response

HosseinAgha commented 1 year ago

Looks like my GraphQL schema did not load correctly, I switched back to using documents because fragments did not work with include.
The LSP is still throwing TypeScript errors but everything magically works now.
I'll keep this issue open as I still get the errors. I'll change the issue title.

TheMaru commented 1 year ago

I'm having the same error message in a monorepo setup with projects in the configuration. Every graphQL file throws the same "Could not parse the TypeScript file" error and after some time the GraphQL server crashes too often so it won't automatically restart

Akkuma commented 1 year ago

I'm seeing the same problem

Could not parse the TypeScript file at [path to .tsx] to extract the graphql tags:
SyntaxError: Unexpected token, expected "," (1:33)
acao commented 1 year ago

i believed the fix for this is released as v0.8.19 for vscode-graphql, 2.11.5 for the LSP, let me know if that helps!

HosseinAgha commented 1 year ago

Nope still getting SyntaxError: Unexpected token, expected "," errors in the graphql language server output everytime I reload the VSCode. (All my queries are defined using gql-tag inside ts files) The autocomplete works fine though.

Tadimsky commented 11 months ago

I'm getting the same thing:

[Error - 11:22:11 PM] Could not parse the TypeScript file at file:///Users/jonno/Source/dorado/components/generic/CarrierAsyncSelect.tsx to extract the graphql tags:
[Error - 11:22:11 PM] SyntaxError: Unexpected token, expected "," (1:48)
acao commented 11 months ago

ok, i think this is related to using graphql config to parse files again, which we reintroduced a few months ago. graphql config throws errors when a file is not perfectly parsable, whereas autocomplete and hover and other features use our fault tolerant parser. are any features besides validation not working because of these errors?

HosseinAgha commented 11 months ago

No, AFAIK.

lensbart commented 9 months ago

Same issue here with the following config:

// graphql.config.ts
import type { IGraphQLConfig } from 'graphql-config'

const schema = 'apps/api/src/schema/**/*.graphql'

export default {
  projects: {
    api: {
      schema,
    },
    web: {
      documents: 'apps/web/src/**/*.{ts,tsx}',
      schema,
    },
  },
} satisfies IGraphQLConfig

and the following output:

[Error - 02:54:29] Could not parse the TypeScript file at file:///some/file/path/etc.ts to extract the graphql tags:
[Error - 02:54:29] SyntaxError: Unexpected token, expected "," (1:28)
Daniel3711997 commented 6 months ago

Still happening

acao commented 6 months ago

most of these issues will be fixed in the next minor release soon! the errors do not point to the exact cause. documents config will be required and a warning will be logged if it isn't present. I plan to release this this weekend after adding more integration specs and manual testing.

and then another major release will occur - this is another major rewrite which ensures that graphql-config includes and excludes are honored. this 2nd refactor actually just uses @graphql-tools/load to properly do everything with graphql config pointers, instead of manually unwrapping the pointers like we do now. we have been planning this with the guild for a long time! and so far it is much faster.

keep an eye out for the pre-release channel, and v0.10.1 or so

addiebarron commented 5 months ago

Still having this issue on pre-release

acao commented 5 months ago

unfortunately the issue will continue happening until the 2nd stage rewrite, as it comes from graphql tools loadTypeDefs() via graphql config loadConfig(). it doesn't mean anything though because it falls back to another parser and keeps going

in the next release, where we overhaul logging levels, these errors will be buried because they are not relevant

luin commented 4 months ago

I encountered a similar issue with the latest version (v0.11.0) where all my fragments are reported as "Unknown". Downgrading the extension to v0.9.3 fixes the issue.

However, both versions show the following errors for every TypeScript files in my repo:

[Error - 10:51:46 AM] Could not parse the TypeScript file at file:///xxx.tsx to extract the graphql tags:
[Error - 10:51:46 AM] SyntaxError: Unexpected token, expected "," (1:24)

Not sure why v0.9.3 works correctly and go to definition for fragments also works.

lucas-garrido commented 4 months ago

same issue

acao commented 4 months ago

@luin these errors are unrelated. in the latest graphql config version it seems you must use arrays if you want full paths for documents, otherwise use globs for documents config

bensoutendijk commented 3 months ago

I encountered a similar issue with the latest version (v0.11.0) where all my fragments are reported as "Unknown". Downgrading the extension to v0.9.3 fixes the issue.

However, both versions show the following errors for every TypeScript files in my repo:

[Error - 10:51:46 AM] Could not parse the TypeScript file at file:///xxx.tsx to extract the graphql tags:
[Error - 10:51:46 AM] SyntaxError: Unexpected token, expected "," (1:24)

Not sure why v0.9.3 works correctly and go to definition for fragments also works.

Yes downgrading works for me as well. I am having this issue with all of my .vue files in my project.

Tadimsky commented 2 months ago

@acao is there any update on this? It's still happening on the latest version. Downgrading is the only way to resolve this.

acao commented 2 months ago

@Tadimsky I suspect this is due to changes where we let graphql config do more of the parsing. Looking into it still!

5o50 commented 1 week ago

Same issue on v0.12.0 only way is downgrading to v0.9.3