Skip to content

GraphQL query fragments #25

@JacobMountain

Description

@JacobMountain

Currently interface selections are inlined, this can lead to overly verbose/complex queries. fragments can be used to reduce the size of the query.
e.g.

query Hero($episode: Episode) { 
    hero(episode: $episode) { 
        id 
        name 
        friends { 
            id 
            name 
            ... on Human { 
                homePlanet 
                height 
                mass 
                __typename 
            } 
            ... on Droid { 
                primaryFunction 
                __typename 
            } 
            __typename 
        } 
        appearsIn
        ... on Human { 
            homePlanet 
            height 
            mass
            appearsIn 
            starships { 
                id 
                name 
                length 
                coordinates 
                __typename 
            } 
            __typename 
        } 
        ...Droid { 
            appearsIn 
            primaryFunction 
            __typename 
        } 
        __typename 
    } 
}

could be reduced to

query Hero($episode: Episode) { 
    hero(episode: $episode) { 
        id 
        name 
        friends { 
            id 
            name 
            ...Human
            ...Droid
            __typename 
        } 
        appearsIn
        ... Human
        ... on Droid
        __typename 
    } 
}
fragment Human on Human {
    homePlanet 
    height 
    mass 
    __typename 
}
fragment Droid on Droid {
    appearsIn 
    primaryFunction 
    __typename
}

Although at the final depth it wouldn't be able to use the fragment as it would add unnecessary depth

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions