teamwalnut / graphql-ppx

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

Support for rescript 11 and uncurried mode #290

Open JasoonS opened 1 year ago

JasoonS commented 1 year ago

I was unable to use this library in a new rescript 11 project.

I received the following error:

  This function is a curried function where an uncurried function is expected

I didn't get the chance to dig deeper into the issue so I can't say what the root cause is, but it happened on this query:

let result = %graphql(`
  query UserQuery {
  indexers {
    created_time
    indexer_id
  }
}
`)
DZakh commented 11 months ago

I managed to make it work using @@uncurried.swap

// Api.res

type gqlResponse = {
  data: option<Json.t>,
  errors: option<Json.t>,
}

%%private(
  @module("./api/fetchBackoffice.ts")
  external fetch: (~query: string, ~variables: 'variables) => Promise.t<gqlResponse> = "default"
)

@@uncurried.swap

module MakeRequest = (
  Query: {
    module Raw: {
      type t
    }
    type t
    type t_variables
    let query: string
    let unsafe_fromJson: (. Json.t) => Raw.t
  },
) => {
  let toTask = (. ~variables: Query.t_variables): Task.t<Query.Raw.t, string> => {
    Task.make((~resolve, ~reject) => {
      fetch(~query=Query.query, ~variables)
      ->Promise.thenResolve(response => {
        switch response {
        | {errors: Some(errors)} => reject(errors->Json.stringifyAny->Option.getExn)
        | {errors: None, data: None} => reject("Response data is empty.")
        | {errors: None, data: Some(data)} => resolve(data->Query.unsafe_fromJson)
        }
      })
      ->Promise.catch(exn => {
        exn->Exn.asJsExn->Option.flatMap(Exn.message)->Option.getExn->reject
        Promise.resolve()
      })
      ->ignore
      None
    })
  }
}
// Foo.res
%graphql(`
   query Query($registrationNumber: String!){
      carInfo(registrationNumber: $registrationNumber) {
        engineType
        modelYear
        vehicleMake
        model
      }
    }
  `)

module Request = Api.MakeRequest(Query)

Request.toTask(~variables=...)
cwstra commented 11 months ago

I think the ppx still fails with a "This function is a curried function where an uncurried function is expected" error whenever a fragment shows up, which is harder to fix with a wrapper.

LeoLeBras commented 9 months ago

A rescript-forum note from the main maintainer 🙏

We at walnut are going to migrate to ReScript 11 as well, and are maintaining graphql-ppx bear with us while we ship an update. But it’s in progress :slight_smile: — https://forum.rescript-lang.org/t/anyone-using-graphql-ppx-and-rescript-apollo-client-with-11/4995/4?u=leolebras