0
votes

I would like to use graphql fragments in my nestjs project but i cant find any documentation on the official site. I have tried to add a fragment to my *.graphql file:

fragment fragmenName on MyType {
    attribute
}

on server start the file is transpiled(?) to the qraphql.ts with all types/interfaces and queries except the fragment. The appolo server does not find the fragment also...

Do i miss something?

1

1 Answers

1
votes

the response of @Daniel forced me to think a little more and i came to the following conclusion which ill try to explain. For example i have the following type:

type Car{
   brand:String
   power:String
   doors:Int
   color:String
}

and a query:

type Query {
    car: Car
}

So if i, on the client-side want to ask for the car. I can do it this way:

fetch('http://url:port', {
    methode: 'post',
    headers: {...headers}
    body: JSON.stringify({ query: `
        car{
            brand
            color
            power
            doors
        }
    `})
}).then(res => res.json()).then(res=>console.log(res.data));

this will result in all the attributes of my car. So now if i like to use the fragments. I will do:

fetch('http://url:port', {
    methode: 'post',
    headers: {...headers}
    body: JSON.stringify({ query: `
        car{
            ...wholeCar
        }
        fragment wholeCar on Car{
            brand
            color
            power
            doors
        }
    `})
}).then(res => res.json()).then(res=>console.log(res.data));

The key thing to understand was that i need to define the fragment within the query, on the client and not on the server.