0no-co / GraphQLSP

TypeScript LSP plugin that finds GraphQL documents in your code and provides diagnostics, auto-complete and hover-information.
https://gql-tada.0no.co
MIT License
332 stars 13 forks source link

Unknown fragment error when the fragment is defined as an object field #321

Closed mdoury-oef closed 1 month ago

mdoury-oef commented 1 month ago

Describe the bug

Attaching fragments to utils and components is quite a common composition pattern. In some cases, grouping fragments in a map could also be useful. It seems when a fragment is defined as an object field, an "Unknown fragment" error is raised.

Example

// getCountryEmoji.ts
export function getCountryEmoji(
  country: FragmentOf<typeof CountryEmojiFragment>,
) {
  const data = readFragment(CountryEmojiFragment, country);
  return data.emoji;
}

getCountryEmoji.fragment = CountryEmojiFragment;
// When the fragment is defined as an object field, we get the following error:
// Unknown fragment "CountryEmoji".
const brokenQuery = graphql(
  `
    query getCountry($code: ID!) {
      country(code: $code) {
        ...CountryEmoji
      }
    }
  `,
  [getCountryEmoji.fragment],
);

The issue can be reproduced in this codesandbox

gql.tada version

@0no-co/graphqlsp: ^1.9.1 gql.tada: ^1.4.3

Validations