mhallin/graphql_ppx

add __typename for apollo-client

Gregoirevda opened this issue · 6 comments

Per apollo-codegen docs for TypeScript and Flow:

https://github.com/apollographql/apollo-codegen

If you're using a client like apollo-client that does this automatically for your GraphQL operations, pass in the --add-typename option to apollo-codegen to make sure the generated Typescript and Flow types have the __typename field as well. This is required to ensure proper type generation support for GraphQLUnionType and GraphQLInterfaceType fields.

It would be good to add this in graphql_ppx too. I could have more reusable types in reason-apollo

I'm not sure what you want. The ppx already adds __typename on unions for its own purposes, does Apollo need more than that?

Apollo Client adds a __typename fields on every GraphQL OBJECT when the user makes a query. So the response is always with __typename's

I see. I don't want to pollute all queries with __typename, but maybe I could add an "Apollo mode" that turns on this feature and enables AST output. Would that be useful?

That would be great! I understand it shouldn't be default, but specific to Apollo usage

@mhallin How this could be achieved? I would like to send a PR

If you only need this in the query and not in the parse function, I would add it to the GraphQL printer at https://github.com/mhallin/graphql_ppx/blob/master/src/graphql_printer.ml. There's a starting point for global configuration in https://github.com/mhallin/graphql_ppx/blob/master/src/dirty_checker.ml#L1-L3 (that need to move to its own module) where you could add an "apollo mode".