apollographql / graphql-tag

A JavaScript template literal tag that parses GraphQL queries
MIT License
2.32k stars 176 forks source link

Code formatting causing fragment to be perceived as not unique. #751

Open RamonBalthazar opened 1 year ago

RamonBalthazar commented 1 year ago

The problem

Given a fragment that looks like this:

const PersonFragment = gql`
  fragment PersonFragment on Person {
    id
    name
    notes(where: { deleted_at: { _is_null: true } }) {
      id
      text
    }
  }
`

When I use this fragment in two places in my app I get this warning on the console:

Warning: fragment with name PersonFragment already exists. graphql-tag enforces all fragment names across your application to be unique; read more about this in the docs: http://dev.apollodata.com/core/fragments.html#unique-names

The cause

After some investigation I found out that the issue is being caused by code formatting.

If I change this line

    notes(where: { deleted_at: { _is_null: true } }) {

to this, without some of the spaces

    notes(where: {deleted_at: {_is_null: true}}) {

the warning goes away!

I worked it out after printing PersonFragment and noticing that the print function gets rid of those spaces.

This is a bug because the fragment equality algorithm is formatting the code itself when parsing the fragment for the first time, but not formatting the code again when comparing with what it's got stored.


This doesn't work also, which is extra annoying:

    # prettier-ignore
    notes(where: {deleted_at: {_is_null: true}}) {