apollo-hooks-codegen icon indicating copy to clipboard operation
apollo-hooks-codegen copied to clipboard

Union Return Type Support

Open naderhen opened this issue 6 years ago • 2 comments

Thanks so much for this library, it's been incredibly helpful for the project I'm building. I was wondering if the following was supported:

schema:

type Mutation {
  unionMethod(body: String): UnionType
}


type FieldError {
  field: String
}

type Errors {
  fieldErrors: [FieldError]
}

type MutationResponse {
  ok: Boolean
}

union UnionType = Errors | MutationResponse

documents:

mutation unionMethod {
  unionMethod(body: "test") {
    ...on Errors {
      fieldErrors {
        field
      }
    }

    ...on MutationResponse {
      ok
    }
  }
}

Currently, this throws an exception TypeError: Cannot create property 'source' on string 'unhandled GraphQLOutputType in unwrap' which appears to be coming from an unhandled case in https://github.com/bearbytes/apollo-hooks-codegen/blob/master/src/transform.ts#L381 .

Are there plans to support this use case, or suggestions for the best way to handle it moving forward? Happy to provide a PR if you have a sense of what we might need.

Thanks!

naderhen avatar Mar 18 '19 18:03 naderhen

This is definitely something I overlooked.

Please check version 1.2.0 if this works for you. I added support for Unions and also attached the __typename property to the named types, so you can check which type was returned.

pothos-dev avatar Mar 21 '19 19:03 pothos-dev

Thanks for the quick fix! I'm about to hop on a plane so it might be a few days before I can test this out against my codebase, but seeing the diff + new tests definitely looks great! Will update here once I run it locally. Thanks again!

naderhen avatar Mar 21 '19 20:03 naderhen