Graphql-tag: Nested fragments

Created on 11 May 2017  路  2Comments  路  Source: apollographql/graphql-tag

Hi,

I wanted to nest a fragment in another fragment.
Just like this. https://learngraphql.com/basics/fragments/4
Is this possible with this package? It seems it is not working with my project which is on top of one of the starters I'm using, which depends on this package.
Or maybe a upcoming feature?

I tried to search all the issues here and couldn't find a similar discussion.

Oh I'm using v2.0.0

Most helpful comment

yes - you can nest fragments within fragments using graphql-tag. you'll want to make sure you import both the fragments though:

const fragment = gql`
  fragment Foo on Something {
    someField
  }
`;

const anotherFragment = gql`
  fragment Bar on Something {
    anotherField
    ...Foo
  }
  ${fragment}
`;

is valid. note that we're interpolating the first fragment in the second one.

All 2 comments

yes - you can nest fragments within fragments using graphql-tag. you'll want to make sure you import both the fragments though:

const fragment = gql`
  fragment Foo on Something {
    someField
  }
`;

const anotherFragment = gql`
  fragment Bar on Something {
    anotherField
    ...Foo
  }
  ${fragment}
`;

is valid. note that we're interpolating the first fragment in the second one.

you'll also want to make sure that the nested fragment types are the same! notice in the example that both fragments are on Something

Was this page helpful?
0 / 5 - 0 ratings