mhallin / graphql_ppx

GraphQL PPX rewriter for Bucklescript/ReasonML
BSD 3-Clause "New" or "Revised" License
320 stars 42 forks source link

signature of parse #57

Open Coobaha opened 5 years ago

Coobaha commented 5 years ago

Hi @mhallin

I encountered problem trying to update Apollo cache with readQuery/writeQuery. I need to know shape of Js.Json.t which is passed to parse (Js.Json.t => t). Parse guarantees that JSON shape is valid in the runtime but it should be possible to have it as a static type based on gql query string?

Cache holds Js.Json.t. It's type is unknown currently and it will be incorrect to use t if @bsRecordis used.

module MyQuery: {
  type t;
  let make: unit => Js.t({ . parse: Js.Json.t => t, query: string, variables: Js.Json.t });
};

Would it be possible to have alternative version that trusts that data comes from graphql endpoint? In other words less runtime type save (which should not be the case if data comes from graphql server) but more exact and performant. So it will parse json only when needed, for instance for @bsRecord.

module MyQuery: {
  type jsT = <JSON type parsed from graphql query string>
  type t;
  let make: unit => Js.t({ . parse: jsT => t, query: string, variables: Js.Json.t });
};

Or at least some raw jsT could be introduced to use with apollo cache

mhallin commented 5 years ago

Interesting idea, I'm not sure how easy it would be to tack this on to the current implementation. Maybe this would be another "output mode" that you could specify as an argument to the ppx.

Another thing; it's not just @bsRecord that would require parsing, but unions/interfaces and enums too.