Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for rescript 11 and uncurried mode #290

Open
JasoonS opened this issue Aug 3, 2023 · 3 comments
Open

Support for rescript 11 and uncurried mode #290

JasoonS opened this issue Aug 3, 2023 · 3 comments

Comments

@JasoonS
Copy link
Contributor

JasoonS commented Aug 3, 2023

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
Copy link

DZakh commented Nov 30, 2023

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
Copy link

cwstra commented Dec 7, 2023

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants