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

Added support for nested fragments when importing from graphql/loader

Open mzalewski opened this issue 6 years ago • 8 comments

Used test from https://github.com/apollographql/graphql-tag/pull/105

Simplified import of nested fragments - uses the built-in graphql printer to get the full source including any nested fragments

Fixes the following situation:

AuthorDetailsFragment.gql

fragment AuthorDetails on Author {
    firstName
    lastName
}

BookDetailsFragment.gql

#import "./AuthorDetailsFragment.gql"
fragment BooksAuthor on Book {
    author {
        ...AuthorDetails
    }
}

main.js

import gql from 'graphql-tag';
import BookDetails from './BookDetailsFragment.gql';

const result = gql`query { ...BooksAuthor } ${BookDetails}`;

Previously, result would only include 2 definitions: the Query and the BooksAuthor fragment - and ignore the AuthorDetails fragment.

Now result should include all 3

mzalewski avatar Dec 04 '18 22:12 mzalewski

we want this :)

eltonio450 avatar Dec 10 '18 10:12 eltonio450

Oh yes ! we really need this too ! 😍 It's so bad not being able to nest fragments... 😢

Thoum avatar Dec 14 '18 10:12 Thoum

@mzalewski, any updates?

gabsprates avatar Jul 16 '19 14:07 gabsprates

I really need that feature too, hope reviewer merge this pull request

kamaladenalhomsi avatar Sep 11 '19 16:09 kamaladenalhomsi

It's been a while...

IgorSzymanski avatar Nov 13 '19 10:11 IgorSzymanski

Our app just suffered a nasty bug traced to a point where developer tried a nested fragment and commented it out because it doesn't work and the hard coded attributes pasted in its place got out of sync with the original fragment. Please get this merged!

nodabladam avatar Dec 30 '19 23:12 nodabladam

any updates on this?

manfioLP avatar Sep 03 '20 10:09 manfioLP

As a workaround while this isn't merged, for me the following worked.

In the situation described by the OP, put the query in a separate .gql file:

# MyBookQuery.gql
#import "./BookDetailsFragment.gql"
# Above import includes nested AuthorDetailsFragment.

query MyBookQuery {
    book {
         ...BooksAuthor
    }
}
// main.js
const result = require("./MyBookQuery")

I'm using webpack as described here.

As a (positive) side effect, the gql import in main.js can be dropped.

mooori avatar Sep 11 '20 09:09 mooori