GraphQL union and conflicting types

I have a problem with my project and I can not find any solution on the Internet. Here is the situation.

  • I have a Union ( DataResult ) of 2 types ( Teaser and Program )
  • I have a Zone type with data field (array DataResult )
  • Teaser & Program have the same title field with a different type ( String and String! )

Here are parts of the Zone , Teaser and Program diagrams:

 type Program { title: String } type Teaser { title: String! } union DataResult = Teaser | Program type Zone { (...) data: [DataResult!] } 

When I tried to query the zone data as described in the part of the query below, I had an error in GraphQL.

 zones { ...zoneFieldsWithoutData data { ... on Program { ...programFields } ... on Teaser { ...teaserFields } } } 

Here is the error:

 Error: GraphQL error: Fields \"title\" conflict because they return conflicting types String and String!. Use different aliases on the fields to fetch both if this was intentional 

I cannot use an alias because the specs need the same attribute name for all DataResult objects. What can I do?

Moreover, even if I set the same type for title , I will get many warnings about "missing fields" in the console ....

PS: I use Vanilla Apollo as a GraphQL client (server side)

+12
source share
2 answers

Using the interface solved this problem for me, including the “missing field” - errors (although this means that the fields should be of the same type, I think).

Sort of

 interface DataResult { title: String! } type Program implements DataResult { // Not sure if this can be empty? } type Teaser implements DataResult { // Not sure if this can be empty? } type Zone { (...) data: [DataResult!] } 
0
source

Source: https://habr.com/ru/post/1272670/


All Articles