How to merge graphQL fragments for request with apollo client

3.4k Views Asked by At

I have the following types and:

union U = B|C|D

type A = { childs: U }

that I use with this fragment which works fine and apollo client is able to resolve child objects correctly

fragment A on A {
    childs {
        ...B
        ...C
        ...D
    }
}

when I convert this into

fragment A on A {
    childs {
        U
    }
}

fragment U on U {
    ...B
    ...C
    ...D
}

the apollo client is not able to resolve the childs anymore, all objects are empty without any fields. Is there any way to merge the fragments?

1

There are 1 best solutions below

5
On BEST ANSWER

First I think there might be a typo in your conversion? The U has missing ...

fragment A on A {
    childs {
        ...U # here
    }
}

I think you should also try to use conditional fragments

fragment U on U {
    ... on B {
        ...B
    }
    ... on C {
        ...C
    }
    ... on D {
        ...D
    }
}