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

Unions are not supported for mutation return query type? #74

Open
elnygren opened this issue Mar 21, 2019 · 4 comments
Open

Unions are not supported for mutation return query type? #74

elnygren opened this issue Mar 21, 2019 · 4 comments

Comments

@elnygren
Copy link

elnygren commented Mar 21, 2019

I'm getting some errors in a case where my schema has a mutation that returns a union (either the created type or validation errors).

Basically I think it would be a nice pattern to make input validation errors a part of my schema for additional type safety :)

Here's some pseudocode example (unfortunately no time to make exact reprod / can't share code from my own project)

EDIT: I added ...some fields here... below as it was causing confusion, I'm indeed not using empty GraphQL types!
schema

type Foo { ...some fields here... }
type FooValidationErrors { ...some fields here... }
union FooResult = Foo | FooValidationErrors
input FooInput {...some fields here...}

type Mutation {
  createFoo(input: FooInput): FooResult
}

query

mutation createFoo {
  createFoo($input: FooInput) {
    ...on Foo {...some fields here...}
    ...on FooValidationErrors {...some fields here...}
  }
}

error:
Schema.Invalid_type("Type FooResult doesn't have any fields")

Note: I'm not having any problems using this schema with Apollo server and Apollo client. Just with graphql ppx.

Edit: I decided to alter my schema a bit and went with https://github.com/mhallin/graphql_ppx#non-union-variant-conversion

@baransu
Copy link
Contributor

baransu commented Apr 3, 2019

I think the problem is your schema which is not valid. As far as I know, you cannot have empty objects or scalar unions. You have no fields in Foo or FooValidationErrors thus you're not querying any fields in mutation response.

The following example which is almost the same as yours would be valid.

Schema:

type Foo {
  id: String!
  name: String!
}

enum UserValidationError {
  SomeError
}

type UserValidationErrors {
  errors: [SomeError!]!
}

union FooResult = Foo | FooValidationErrors

input FooInput {
  name: String
}

type Mutation {
  createFoo(input: FooInput): FooResult
}

Query:

mutation createFoo($input:  FooInput) {
  createFoo(input: $input) {
    ...on Foo {
       id # at least one field required
    }
    ...on FooValidationErrors {
      errors
    }
  }
}

@baransu
Copy link
Contributor

baransu commented Apr 3, 2019

@elnygren As you mentioned, the best approach would be to use @bsVariants described in https://github.com/mhallin/graphql_ppx#non-union-variant-conversion

Can this issue be closed?

@elnygren
Copy link
Author

elnygren commented Apr 4, 2019

@baransu ah my example was a bit short...

You have no fields in Foo or FooValidationErrors thus you're not querying any fields in mutation response.

This is incorrect, I was just lazy and left them out... (I've fixed this now in the original post).

So in other words: the error happens with a valid schema as I described.

@baransu
Copy link
Contributor

baransu commented Apr 5, 2019

Could you provide example? Error message says: FooResult doesn’t have any fields so I’m curious 🤔

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