-
Notifications
You must be signed in to change notification settings - Fork 28
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
Add functor API for hooks. #129
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,3 +95,63 @@ let useSubscription = | |
(handler, args, parse), | ||
); | ||
}; | ||
|
||
/** | ||
* The functor implementation of useQuery. An alternative to the function API. | ||
* | ||
* Accepts the following arguments: | ||
* | ||
* Query - a graphql_ppx or graphql_ppx_re module containing the | ||
* type t of the GraphQL query, the query string of the GraphQL query, | ||
* and a parse function for decoding the JSON response. | ||
*/ | ||
module type SubscriptionConfig = { | ||
type t; | ||
let query: string; | ||
let parse: Js.Json.t => t; | ||
}; | ||
|
||
module type MakeSubscriptionType = { | ||
type resp; | ||
|
||
let useSubscription: | ||
(~variables: Js.Json.t=?, ~handler: handler('acc, resp, 'ret)) => | ||
UrqlTypes.hookResponse('ret); | ||
}; | ||
|
||
module MakeSubscription = | ||
(Subscription: SubscriptionConfig) | ||
: (MakeSubscriptionType with type resp = Subscription.t) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one through me for a bit of a loop. I had to learn a bit about sharing contraints in OCaml – see more in the Destructive Substitution section here: https://v1.realworldocaml.org/v1/en/html/functors.html Basically, we need to be able to pass the type |
||
type resp = Subscription.t; | ||
|
||
let useSubscription = | ||
(type acc, type ret, ~variables=?, ~handler: handler(acc, resp, ret)) | ||
: UrqlTypes.hookResponse(ret) => { | ||
let args = | ||
useSubscriptionArgs(~query=Subscription.query, ~variables?, ()); | ||
|
||
React.useMemo3( | ||
() => { | ||
let response: UrqlTypes.hookResponse(ret) = | ||
switch (handler) { | ||
| Handler(handlerFn) => | ||
useSubscriptionJs( | ||
args, | ||
Some( | ||
(acc, data) => handlerFn(acc, Subscription.parse(data)), | ||
), | ||
)[0] | ||
|> useSubscriptionResponseToRecord(x => x) | ||
| NoHandler => | ||
useSubscriptionJs(args, None)[0] | ||
|> useSubscriptionResponseToRecord(Subscription.parse) | ||
}; | ||
|
||
response; | ||
}, | ||
(handler, args, Subscription.parse), | ||
); | ||
}; | ||
|
||
useSubscription; | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seemed like this should be in the dependency array, since it's a function returned from a hook and its identity is not guaranteed.