data
data copied to clipboard
GraphQL issues with __typename and interfaces/unions
I'm trying to use this library to mock Apollo and am running into a few issues when using the generics (.query<Query, Variables>):
__typenameis required and expects a predetermined value.Stringis a superset and won't work.- Workaround:
__typename: () => "User" as const,
- Workaround:
- Interfaces (and possibly unions).. are a bit odd.
- Some of these fields are only present in some implementations.
.createhas no awareness of this, but I do expect to perform.findon one collection to find any of the types, since agetAnimalquery might return any type of animal. - I also end up defining defaults for all fields, even if they're not relevant to that type.
- The
__typenameends up determining what objects are compatible. I can no longer enforce a union here (e.g. Animal interface potentially has"Cat" | "Dog" | "Elephant") since the__typenamedetermines how the type is matched and what fields must be present. __typenamecan technically beundefined, butnullableonly adds| null. I don't really want a default here.- Workaround:
__typename: () => "Cat" as any,(orundefined as anywill work too)
- Some of these fields are only present in some implementations.
At this point it's a little easier to use .query<any, Variables>, but the types were otherwise helpful for developer experience and catching missing fields.