graphiql: [graphql-lsp] Template string fragment get `Unknown fragment` error

When inserting a fragment inside a template string, the fragment does not get recognized when it gets called in the rest of the gql string

2023-03-01 11 27 00

IDE: VSCode 1.75.1

Extensions:

  • GraphQL: Language Feature Support v0.8.5
  • GraphQL: Syntax Highlighting v1.0.6

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 14
  • Comments: 28 (10 by maintainers)

Most upvoted comments

I’m still having this issue: 2023-09-28 22 10 07

fragment file:

import { gql } from '@sb/webapp-api-client/graphql';

export const processTestItemListQuery = gql(/* GraphQL */ `
  query processTestItemListQuery {
    allProcessTestItems(first: 100) {
      edges {
        node {
          ...processTestItemListItemFragment
        }
      }
    }
  }
`);

export const processTestItemListItemDeleteMutation = gql(/* GraphQL */ `
  mutation processTestItemListItemDeleteMutation($input: DeleteProcessTestItemMutationInput!) {
    deleteProcessTestItem(input: $input) {
      deletedIds
    }
  }
`);

export const processTestItemListItemFragment = gql(/* GraphQL */ `
  fragment processTestItemListItemFragment on ProcessTestItemType {
    id
    name
    price
  }
`);

.graphqlconfig:

{
  "name": "Untitled GraphQL Schema",
  "projects": {
    "api": {
      "name": "GraphQL API",
      "schema": "packages/webapp-libs/webapp-api-client/graphql/schema/api.graphql"
    },
    "contentful": {
      "name": "Contentful API",
      "schema": "packages/webapp-libs/webapp-contentful/graphql/schema/contentful.graphql"
    }
  },
  "documents": "**/*.ts"
}

@yamatsum yes it was one of the goals of the last refactor, but the test cases I resolved for don’t seem to cover some of these cases. if someone could provide a test case for MessageProcessor.spec.ts that re-creates this reliably it would be awesome!

@acao if it helps — i’ve noticed since that pretty consistently the error disappears when i:

  • save the file that has the schema in question (even without any changes)
  • save the file in which the error occurs (also without changes)

after that, it seems like pretty much 100% of the time, the error disappears for all the fragments defined in the 1st file.

A college (MAC) has the same issue, but I don’t (Linux) for the same codebase. Interestingly, some fragments are recognised, some not, so not a consistent problem. What’s more interesting is that one particular fragment is recognised in one file, but not in another - Strange!

We have all the fragments in different files. Only one of them has the issue where all have similar structures.

I couldn’t notice a pattern how it happens. There were no issue until some point (see downgrade not below). Then some files previously working fine started displaying red marks without any modifications.

  • Downgrading “GraphQL: Language Feature Support” from 0.11.0 to 0.9.3 resolves the issue.
  • Reinstalling 0.11.0 revives the issue on the same fragment.
  • Saving with or without changes does not help.

i have a theory this bug has resurfaced because of another schema cacheing bug I’ve been working on - thus the fragments are “Unknown” because their base type is missing

hey! having the same issue as @sachingaikwad123

@acao I switched to the pre-release and now the option extensions.languageService.enableValidation: false is working 🎊

also, the go to fragment definition is working partially, it’s opening the file where the fragment but the cursos do not go the the definition line.

I’ll close it, it should have been fixed in https://github.com/graphql/graphiql/pull/3861

@sachingaikwad123 can you see if the pre-release in #3521 fixes the issue? we have tests that I think should confirm this issue is resolved.

what I discovered was we were updating the fragment cache for onUpdate and onWatchedFilesUpdate, but not on onOpenOrAdd, and some other issues with fragment cache keys that piled up along the way. now we have an integration test suite for the server that confirms a happy path for validating fragments and fragments on queries across changes to all files. i will double check to make sure we are still confirming validation is empty where it should be on every mutative event

Adding “documents” to my config file worked, thanks!

@VinceBT-BG music to my ears! another happy user. I will add something to the FAQ section for users using legacy schemaPath

awesome gif tool by the way, i like the keyboard icons, maybe I will use this!

I was indeed using schemaPath instead of schema in the configuration! I tried on the example and it worked, I also tried in my project and I don’t have the errors anymore, thanks !

2023-06-26 17 18 01

EDIT:

Indeed when using schemaPath, in order for the fragments to work, the files need to be specified in an includes field, whereas if schema is present, it will work if placed in documents instead.