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

Persisted document hashes do not include fragments #326

Closed felamaslen closed 1 month ago

felamaslen commented 1 month ago

When calling graphql.persisted, the hash needs to change whenever the query changes, so that you don't end up with conflicts between your remotely stored persisted operations and what you have defined in the client. This works for the most part, but doesn't seem to include fragments imported into the query.

Steps to reproduce:

  1. Create the following code
// MyComponent.ts
import { graphql } from 'gql.tada';

export const MyFragmentDocument = graphql(`
  fragment MyFragment on MyType {
    id
  }
`);

// MyView.ts
import { graphql } from 'gql.tada';

import { MyFragmentDocument } from './MyComponent';

export const MyQueryDocument = graphql.persisted(
  'sha256:abc123',
  graphql(`
    query MyData {
      myType {
        ...MyFragment
      }
    }
  `, [MyFragmentDocument]),
);
  1. Run the graphqlsp code action on MyView.ts to insert the correct hash

  2. Update the fragment - e.g. change id to myProp

Expected behaviour

There is a new hash suggested by graphqlsp through the code action, since the imported fragment changed, changing the content of the document.

Actual behaviour

There is no new hash suggested by graphqlsp, suggesting that the fragment is not taken into account.

felamaslen commented 1 month ago

I'd like to reopen this issue - the PR I merged fixed the inclusion of fragments one level deep, but this still does not work:

const Frag1Document = graphql(`
  fragment Frag1 on MyType {
    ...Frag2
  }
`)
const Frag2Document = graphql(`
  fragment Frag2 on MyType {
    prop
  }
`)
const MyDocument = graphql(`
  query My {
    myType {
      ...Frag1
    }
  }
`);

The hash is of a document including MyDocument and Frag1, but not Frag2.