Open JasoonS opened 1 year 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=...)
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.
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
I was unable to use this library in a new rescript 11 project.
I received the following error:
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: