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

Union types gateway support #59

Open
Davide-Gheri opened this issue Feb 8, 2023 · 1 comment
Open

Union types gateway support #59

Davide-Gheri opened this issue Feb 8, 2023 · 1 comment

Comments

@Davide-Gheri
Copy link

As per this code comment https://github.com/mercurius-js/mercurius-gateway/blob/main/lib/gateway/make-resolver.js#L652 it seems that it is not possible to define a Query that returns a Union type with one of the included object types defined in another service.

E.g.:

With these two service schemas

# Post schema
type Post @key(fields: "id") {
    id: ID!
    title: String!
    authorId: Int!
}
    
extend type Query {
    findPost(id: ID!): Post
}
# Recipe schema
type Recipe @key(fields: "id") {
  id: ID!
  name: String!
  postId: Int!
}

extend type Post @key(fields: "id") {
  id: ID! @external
  recipes: [Recipe!]!
}

union SearchUnion = Recipe | Post

extend type Query {
  findRecipe(id: ID!): Recipe
  search: [SearchUnion!]!
}

and a dummy search resolver like this:

search: () => ([
  { __typename: 'Post', id: 1 },
  { __typename: 'Recipe', id: 1, name: 'recipe1', postId: 1 },
]);

The query throws the error Cannot read properties of undefined (reading 'has')
I found out the error is thrown here.

Strangely if I change the returned array order (returning a Recipe before a Post) the error changes to Cannot return null for non-nullable field Post.title. (it works if I only ask for Post.id since it doesn't have to resolve fields from another service)

@mcollina
Copy link
Contributor

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

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

2 participants