teamwalnut / graphql-ppx

GraphQL language primitives for ReScript/ReasonML written in ReasonML
https://graphql-ppx.com
MIT License
257 stars 53 forks source link

Can't use ppxCustom on 2nd level of graphQL #265

Closed pzshine closed 2 years ago

pzshine commented 3 years ago

As I have tried to fetch the data using `use subscription as this code below

type aggregate_t = {count: option<int>}

type transactions_aggregate_t = {aggregate: option<aggregate_t>}

type val_t = {
  consensusAddress: string,
  operatorAddress: Address.t,
  moniker: string,
  identity: string,
}

type internal_t = {
  timestamp: MomentRe.Moment.t,
  hash: Hash.t,
  inflation: float,
  validator: val_t,
  transactions_aggregate: transactions_aggregate_t,
}

module MultiConfig = %graphql(`
  subscription Blocks($limit: Int!, $offset: Int!) {
    blocks(limit: $limit, offset: $offset, order_by: [{height: desc}]) @ppxAs(type: "internal_t") {
      timestamp @ppxCustom(module: "GraphQLParserModule.Date")
      hash @ppxCustom(module: "GraphQLParserModule.Hash")
      inflation @ppxCustom(module: "GraphQLParserModule.FloatString")
      validator @ppxAs(type: "val_t"){
        consensusAddress: consensus_address
        operatorAddress: operator_address @ppxCustom(module: "GraphQLParserModule.Address")
        moniker
        identity
      }
      transactions_aggregate @ppxAs(type: "transactions_aggregate_t"){
        aggregate @ppxAs(type: "aggregate_t"){
          count
        }
      }
    }
  }
`)

The example module that I've used on @ppxAs

module Date = {
  type t = MomentRe.Moment.t
  let parse = json => json->GraphQLParser.timestamp

  let serialize = date => date->MomentRe.Moment.toJSON->Belt.Option.getExn->Js.Json.string
}

My graphql config in bs-config

"graphql": {
    "apolloMode": false,
    "extendMutation": "ApolloClient.GraphQL_PPX.ExtendMutation",
    "extendQuery": "ApolloClient.GraphQL_PPX.ExtendQuery",
    "extendSubscription": "ApolloClient.GraphQL_PPX.ExtendSubscription",
    "templateTagReturnType": "ApolloClient.GraphQL_PPX.templateTagReturnType",
    "templateTagImport": "gql",
    "templateTagLocation": "@apollo/client"
  },

The problem is I couldn't use @ppxCustom on 2nd level graphQL, so it's in val_t scope

operatorAddress: operator_address @ppxCustom(module: "GraphQLParserModule.Address")

as you have seen on the code above. I'm not sure it's whether a bug or not?

Thank you

jfrolich commented 3 years ago

This should work, there are snapshot tests in this repo, where the generated code is snapshotted. Can you try to reproduce it there?

pzshine commented 2 years ago

This should work, there are snapshot tests in this repo, where the generated code is snapshotted. Can you try to reproduce it there?

I tried to reproduce and found that I wrote the serialize in the wrong way. the raw is Json.t not string at all.

Now the issue is solved, I will close this issue.

Thanks for the clarification.

jfrolich commented 2 years ago

Thanks @pzshine!