Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Concatenate document definitions #283

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

nhodges
Copy link

@nhodges nhodges commented Jan 22, 2020

Issue Fixed #159

Proposed Changes

  • Concatenate document definitions while filtering for duplicates

Based on PR #105 by @evanrs, updated for current version of graphql-tag

Document fragments parsed by graphql-tag/loader lose their definitions when used with a gql tagged template literal. This is fixed by concatenating the document definitions—filtering for duplicates.

Without this, the following would fail:

import catFaceFragment from './catFaceFragment.gql';

const query = gql`
  query Kitten($name: String) {
    kitten(name: $name) {
      ... CatFace
    }
  }
  ${catFaceFragment}
`
#import "./whiskers.gql"

fragment CatFace on Cat {
  ... Whiskers
}
fragment Whiskers on Cat {
  whiskers {
    quantity
    length
    areWiderThanBody
  }  
}
Unknown fragment "Whiskers".

Why?

Currently, to make an example like the above work, you would have to do something like:

import { parse, print } from 'graphql'
import catFaceFragment from './catFaceFragment.gql';

const query = parse`
  query Kitten($name: String) {
    kitten(name: $name) {
      ... CatFace
    }
  }
  ${print(catFaceFragment)}
`

This has an outsized impact on bundle size due to having to pull in both parse/print instead of having graphql-tag do the lifting

@apollo-cla
Copy link

@nhodges: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@nhodges nhodges requested review from jnwng and abernix January 22, 2020 18:55
@dimikot
Copy link

dimikot commented Jun 26, 2020

There seems to be a bug in the current PR BTW - it's not enough to just add one if in gql() function, one needs to track ALL pieces of each GraphQL fragment and re-glue them over and over doing de-duplication. Otherwise it won't work for rhombus-like structure.

For future googlers, I wrote a small extension which wraps gql and just does this - removes duplicates of fragments. To use, just replace import gql from "graphql-tag" to import gql from "gql-dedup", no other changes are required.

https://www.npmjs.com/package/gql-dedup

@abernix abernix removed request for abernix and jnwng July 26, 2023 15:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fragment importing another fragment does not work in interpolation
3 participants