graphql-tag icon indicating copy to clipboard operation
graphql-tag copied to clipboard

When including a document node with sub fragments in gql tag include all fragments

Open k opened this issue 7 years ago • 8 comments

Say I have a fragment A which has two fragments B and C

fragmentA.gql:

#import "fragmentB"
#import "fragmentC"

fragment A {
  ...B
  items {
      ...C
   }
}

which I import like so:

import fragmentA from './fragmentA.gql'

if I use fragment A in query D like so:

export const queryD = gql`
query getSomeAlphabet {
      id
      ...A
}
${fragmentA}`

The document node queryD only has 2 definitions, the getSomeAlphabet query and A but not the definitions for B and C.

Is this a bug, or am I doing something wrong here?

k avatar Sep 28 '17 00:09 k

Apologies for brevity, on my phone.

You need to interpolate the fragment B and C in your initial definition of fragment A, too. The document needs the interpolated value of all the fragments you're using. On Wed, Sep 27, 2017 at 5:04 PM Kenneth Bambridge [email protected] wrote:

Say I have a fragment A which has two fragments B and C

fragmentA.gql:

fragment A { ...B items { ...C } }

which I import like so:

import fragmentA from './fragmentA.gql'

if I use fragment A in query D like so:

export const queryD = gqlquery getSomeAlphabet { id ...A}${fragmentA}

The document node queryD only has 2 definitions, the getSomeAlphabet query and A but not the definitions for B and C.

Is this a bug, or am I doing something wrong here?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/apollographql/graphql-tag/issues/125, or mute the thread https://github.com/notifications/unsubscribe-auth/ABERRhypusNUGveC3R67NLYe6h_GTqTHks5smuKPgaJpZM4Pmjkf .

jnwng avatar Sep 28 '17 00:09 jnwng

Thanks @jnwng for the quick response. I forgot to include in the original fragment, but there are two #import statements pulling in the other fragments that are used. Is that what you were referring to?

k avatar Sep 28 '17 00:09 k

Ah, yes. If you have the webpack loader set up properly I'd expect the #import statements to include the definitions for those fragments into the document. I'd double check that that is actually happening - maybe check what's in fragment A when you're defining query D. Might be something up with the imports. On Wed, Sep 27, 2017 at 5:12 PM Kenneth Bambridge [email protected] wrote:

Thanks @jnwng https://github.com/jnwng for the quick response. I forgot to include in the original fragment, but there are two #import statements pulling in the other fragments that are used. Is that what you were referring to?

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/apollographql/graphql-tag/issues/125#issuecomment-332690547, or mute the thread https://github.com/notifications/unsubscribe-auth/ABERRqskowosCCLbe4t_ezA2HKHsOo2Iks5smuRSgaJpZM4Pmjkf .

jnwng avatar Sep 28 '17 00:09 jnwng

I logged the equivalent of fragmentA and i double checked it had all the definitions, but when putting it into queryD it only had to the top level fragment from fragmentA unfortunately.

k avatar Sep 28 '17 01:09 k

The way I got around this was to make a separate .gql file for queryD and #import fragmentA

k avatar Sep 28 '17 01:09 k

Ah, yes now I recall that there's an issue interpolating fragments that are loaded via the webpack loader. Glad you figured it out! On Wed, Sep 27, 2017 at 6:12 PM Kenneth Bambridge [email protected] wrote:

The way I got around this was to make a separate .gql file for queryD and #import fragmentA

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/apollographql/graphql-tag/issues/125#issuecomment-332698772, or mute the thread https://github.com/notifications/unsubscribe-auth/ABERRpsDkyr9gL3dx8xxnBzxPG1YIMtsks5smvKJgaJpZM4Pmjkf .

jnwng avatar Sep 28 '17 11:09 jnwng

@jnwng Is that being tracked elsewhere? Sometimes its more convenient to use the gql tag because its the only way I know of to allow multiple import/exports (until #102 is completed)

k avatar Sep 28 '17 15:09 k

i believe this is resolved by #105 and has to do with how we serialize stuff using the webpack loader.

jnwng avatar May 28 '18 21:05 jnwng