Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: implemented Interface not working nor properly typed #351

Open
Oyelowo opened this issue Dec 1, 2022 · 8 comments
Open

Bug: implemented Interface not working nor properly typed #351

Oyelowo opened this issue Dec 1, 2022 · 8 comments

Comments

@Oyelowo
Copy link

Oyelowo commented Dec 1, 2022

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
}
@widiak
Copy link

widiak commented Dec 13, 2022

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.

@aexol
Copy link
Collaborator

aexol commented Dec 13, 2022

should work in 5.2.2

@Oyelowo
Copy link
Author

Oyelowo commented Dec 13, 2022

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:
"

@aexol
Copy link
Collaborator

aexol commented Dec 13, 2022

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

@aexol
Copy link
Collaborator

aexol commented Dec 13, 2022

I see your output is correct right now.

@aexol
Copy link
Collaborator

aexol commented Dec 13, 2022

I see now you meant unions sharing an interface

@aexol
Copy link
Collaborator

aexol commented Dec 13, 2022

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.

@Oyelowo
Copy link
Author

Oyelowo commented Dec 13, 2022

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 🤔

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants