Hi guys, can UDG use Union type in the schema as the result of a query?
I’m using REST datasources here… so no additional resolver logic can be added
If im using GQL datasource can UDG resolve this?
type Query {
getUser(id:Int):UserUnion
}
union UserUnion = User | CustomError
type CustomError{
code: String
message: String
}
type User{
id:Int
name:String
}
so I did a few simple checks on my local Tyk instance and I was able to get union to work. And it should work for you as well in theory.
It might be a matter of connecting the datasources right. In your schema - where are the datasources connected exactly? You definitely need to have one connected to getUser root field, right? Did you connect anything else anywhere?
And one more question to make sure I can fully replicate your use case - the data source that you connect to getUser root field returns what exactly? I mean what’s the response body for status code 200 OK and what’s the response body in case of an error?
Also tried these response , and still does not work
200 ok
/data (afterFetchHook.OnData): {“id”:1,“name”:“Bob”,“status”:“PENDING”,“accessories”:[“A”,“B”,“C”]}
Thanks for sending back the details. What I checked is not relevant in this situation - I used GQL as a data source, which worked fine, but it’s a different thing. I now replicated your exact example and I see the same issue as you do. I’ll create a ticket in our backlog and we’ll take a look at it - it looks like it’s something with the GQL engine, it gets all the responses from the upstream data sources but somehow “gets confused” on where to put the parsed information.
Before we fix this, you could try to use a workaround. Instead of defining response as a union, just move the error into User type like this:
type Query {
getUser(id:Int): User
}
type CustomError{
code: String
message: String
}
type User{
id:Int
name:String
error: CustomError
}
Then your getUser query root would get the data source as: http://data.com/users/{{.arguments.id}} and you’d have to connect a data source to error field on type User as well as http://data.com/users/{{.object.id}}.
@agata-wit
I don’t know if this is another bug or the same , but I also find inconsistent behavior on union when I tried TYK UDG with GraphQL datasource,