graphql-zeus icon indicating copy to clipboard operation
graphql-zeus copied to clipboard

Bug: implemented Interface not working nor properly typed

Open Oyelowo opened this issue 2 years ago • 8 comments

Problem

  • When a union of types which implement an interface is returned, it should be possible to get the concrete types' values using inline fragment with the interface name instead of the concrete types themselves. This is very useful when doing error handling and you don't want to specifically have to handle each case or manually update new error types on the client side. e.g https://blog.logrocket.com/handling-graphql-errors-like-a-champ-with-unions-and-interfaces/

Example issue:

Below should allow interface Node inline framgenet to get the common field - Id i.e ...on Node { id true} Screenshot 2022-12-01 at 2 57 10

Example schema:

interface Node {
  id: ID!
}

type User implements Node {
  id: ID!
  login: String!
}

type Post implements Node {
  id: ID!
  title: String!
  body: String!
}

union UserOrPost = User | Post

type Query {
  entity(id: ID!): UserOrPost
}

Oyelowo avatar Dec 01 '22 01:12 Oyelowo

I agree, I also need this functionality. And I think it should work also if i not use "...on Node" at all. I use following scenario (return type Node instead of UserOrPost):

type Query { 
   entity(id: ID!): Node 
}

If I query for this fragment:

{ 
  entity: [
    { id: "" }, 
    { 
      __typeName: true, 
      id: true
    } 
  ]
}

the return typescript type should work even without using ...on Node. But now it returns never.

widiak avatar Dec 13 '22 10:12 widiak

should work in 5.2.2

aexol avatar Dec 13 '22 15:12 aexol

Hi @aexol, thanks for your hard work on this! I just tried using the API, but it didn't seem to work for me. Could you provide some guidance on how to use it correctly, or let me know if there's something I might be missing? Thanks in advance for your help!

Screenshot 2022-12-13 at 18 40 38

PS: I was thinking of this: https://graphql.org/learn/schema/#union-types, where it states that:

"Also, in this case, since Human and Droid share a common interface (Character), you can query their common fields in one place rather than having to repeat the same fields across multiple types: "

Oyelowo avatar Dec 13 '22 16:12 Oyelowo

could you provide me with your Zeus generated files? I can debug

aexol avatar Dec 13 '22 22:12 aexol

I see your output is correct right now.

aexol avatar Dec 13 '22 22:12 aexol

I see now you meant unions sharing an interface

aexol avatar Dec 13 '22 22:12 aexol

I can think about it of course. Meanwhile, it would help if you typed fields for each union member. Now, this feature works only if you return the interface from the field. I can think of applying the exact mechanism to unions.

aexol avatar Dec 13 '22 22:12 aexol

that's right, when you return an interface, it works as expected. However, when some or all members of a union implement the same interface, it should be possible to use the interface and get a union of the concrete types with only the fields defined in the interface and the __typename of the respective concrete types 🤔

Oyelowo avatar Dec 13 '22 23:12 Oyelowo