gql-dart / ferry

Stream-based strongly typed GraphQL client for Dart
https://ferrygraphql.com/
MIT License
593 stars 113 forks source link

Error generating classes for mutation that returns a union #570

Open humphrey opened 6 months ago

humphrey commented 6 months ago

Does Ferry support mutations that return Union types such as the example snippet below?

union UpdatePrefsResponse = MyPrefs | FieldErrors | SimpleMutationError

type Mutation {
  updatePrefs(data: UpdatePrefsInput!): UpdatePrefsResponse!
}

Whenever I try to generate classes for a mutation that returns a union such as this (my graph is full of them) I get the following error:

Exception: UpdatePrefsResponse is not an ObjectTypeDefinitionNode or InterfaceTypeDefinitionNode

Or is there something else going on? My apologies if this is a noob question - this is my first attempt to use ferry for a project (I'm attempting to port my existing graphql_codegen project to ferry)

knaeckeKami commented 6 months ago

Hi!

Can you share the mutation that leads to that error?

humphrey commented 6 months ago

Hi!

Can you share the mutation that leads to that error?

Sure thing! Here is the mutation.

mutation UpdatePrefs($data: UpdatePrefsInput!) {
  updatePrefs(data: $data) { 
    ...MyPrefs
    ... on InputError { message }
    ... on FieldError {
      message
      fieldErrors { field, messages }
    }
  }
}

fragment MyPrefs on MyPrefs { dateFormat, firstDayOfWeek }

And here is a full cut down schema needed to reproduce. FYI, this is a simple reproducable adaption from my real schema and mutation (I've deleted most of my code and renamed a number of things for this example). I copied this to a new project with just a schema.graphql and a .graphql file for my mutation, and I get the same error.

union UpdatePrefsResponse = MyPrefs | FieldErrors | SimpleMutationError

type Mutation {
  updatePrefs(data: UpdatePrefsInput!): UpdatePrefsResponse!
}

input UpdatePrefsInput {
  dateFormat: String!
  firstDayOfWeek: Int!
}

type MyPrefs {
  dateFormat: String!
  firstDayOfWeek: Int!
}

interface InputError {
  message: String!
}

type FieldErrors implements InputError {
  message: String!
  fieldErrors: [FieldErrorsField!]!
}

type FieldErrorsField {
  field: String!
  messages: [String!]!
}

type SimpleMutationError implements InputError {
  message: String!
}
knaeckeKami commented 6 months ago

Thanks!

This might be an issue with fragments spreads on a specific case of a union. will take a look

comk commented 2 weeks ago

I have the same problem, too