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

Properly decode partialOperationContext in all hooks and components. Update api.md to reflect latest changes to the reason-urql API. #142

Merged
merged 1 commit into from
Jan 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
178 changes: 96 additions & 82 deletions docs/api.md

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion examples/2-query/src/index.re
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ let flattenPokemon = pokemons =>

ReactDOMRe.renderToElementWithId(
<Provider value=client>
<Query request>
<Query
request
context={Client.ClientTypes.partialOperationContext(
~partialOpRequestPolicy=`CacheFirst,
(),
)}>
...{({response}) =>
switch (response) {
| Data(data) =>
Expand Down
15 changes: 14 additions & 1 deletion examples/3-mutation/src/Dog.re
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,20 @@ let make =
emoji={j|✋|j}
count={string_of_int(pats)}
hex="db4d3f"
onClick={_ => executePatMutation(~key=id, ()) |> ignore}
onClick={_ =>
executePatMutation(
~key=id,
~context=
Some(
Client.ClientTypes.partialOperationContext(
~partialOpRequestPolicy=`NetworkOnly,
(),
),
),
(),
)
|> ignore
}
/>
<EmojiButton
emoji={j|🍖|j}
Expand Down
9 changes: 5 additions & 4 deletions src/components/UrqlQuery.re
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type queryRenderPropsJs('extensions) = {
data: Js.Nullable.t(Js.Json.t),
error: Js.Nullable.t(UrqlCombinedError.combinedErrorJs),
executeQuery:
option(UrqlClient.ClientTypes.partialOperationContext) => unit,
option(UrqlClient.ClientTypes.partialOperationContextJs) => unit,
extensions: Js.Nullable.t('extensions),
};

Expand All @@ -26,7 +26,7 @@ module QueryJs = {
~variables: Js.Json.t,
~requestPolicy: string,
~pause: bool=?,
~context: UrqlClient.ClientTypes.partialOperationContext=?,
~context: UrqlClient.ClientTypes.partialOperationContextJs=?,
~children: queryRenderPropsJs('extensions) => React.element
) =>
React.element =
Expand All @@ -43,7 +43,8 @@ let urqlQueryResponseToReason =
->Js.Nullable.toOption
->Belt.Option.map(UrqlCombinedError.combinedErrorToRecord);
let fetching = result->fetchingGet;
let executeQuery = (~context=?, ()) => result->executeQueryGet(context);
let executeQuery = (~context=?, ()) =>
result->executeQueryGet(context->UrqlClient.partialOpCtxToPartialOpCtxJs);
let extensions = result->extensionsGet->Js.Nullable.toOption;

let response =
Expand Down Expand Up @@ -74,7 +75,7 @@ let make =
variables
requestPolicy={UrqlTypes.requestPolicyToJs(requestPolicy)}
?pause
?context>
context=?{UrqlClient.partialOpCtxToPartialOpCtxJs(context)}>
{result => result |> urqlQueryResponseToReason(parse) |> children}
</QueryJs>;
};
16 changes: 9 additions & 7 deletions src/hooks/UrqlUseMutation.re
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
type executeMutationJs =
(
option(Js.Json.t),
option(UrqlClient.ClientTypes.partialOperationContext)
option(UrqlClient.ClientTypes.partialOperationContextJs)
) =>
Js.Promise.t(UrqlClient.ClientTypes.operationResult);

Expand Down Expand Up @@ -63,8 +63,10 @@ let useMutation = (~request) => {

let executeMutation =
React.useMemo2(
((), ~context=?, ()) =>
executeMutationJs(Some(request##variables), context),
((), ~context=?, ()) => {
let ctxJs = UrqlClient.partialOpCtxToPartialOpCtxJs(context);
executeMutationJs(Some(request##variables), ctxJs);
},
(executeMutationJs, request##variables),
);

Expand All @@ -83,10 +85,10 @@ let useDynamicMutation = definition => {

let executeMutation =
React.useMemo2(
((), ~context=None) =>
composeVariables(request =>
executeMutationJs(Some(request), context)
),
((), ~context=None) => {
let ctxJs = UrqlClient.partialOpCtxToPartialOpCtxJs(context);
composeVariables(request => executeMutationJs(Some(request), ctxJs));
},
(executeMutationJs, composeVariables),
);

Expand Down
20 changes: 16 additions & 4 deletions src/hooks/UrqlUseQuery.re
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,24 @@ type useQueryArgs = {
[@bs.optional]
pause: bool,
[@bs.optional]
context: UrqlClient.ClientTypes.partialOperationContext,
context: UrqlClient.ClientTypes.partialOperationContextJs,
};

/**
* The type of executeQuery – a function for re-executing
* the query passed to useQuery. Accepts an optional partial
* operation context for modifying query execution.
*/
type executeQueryJs =
option(UrqlClient.ClientTypes.partialOperationContextJs) => unit;

type executeQuery =
(~context: UrqlClient.ClientTypes.partialOperationContext=?, unit) => unit;

/* The response to useQuery on the JavaScript side. */
type useQueryResponseJs('extensions) = (
UrqlTypes.jsResponse(Js.Json.t, 'extensions),
executeQuery,
executeQueryJs,
);

/**
Expand Down Expand Up @@ -91,17 +94,26 @@ let useQuery = (~request, ~requestPolicy=?, ~pause=?, ~context=?, ()) => {
~requestPolicy=?
requestPolicy->Belt.Option.map(UrqlTypes.requestPolicyToJs),
~pause?,
~context?,
~context=?UrqlClient.partialOpCtxToPartialOpCtxJs(context),
(),
);

let (responseJs, executeQuery) = useQueryJs(args);
let (responseJs, executeQueryJs) = useQueryJs(args);

let response =
React.useMemo2(
() => responseJs |> urqlResponseToReason(request##parse),
(responseJs, request##parse),
);

let executeQuery =
React.useMemo1(
((), ~context=?, ()) => {
let ctxJs = UrqlClient.partialOpCtxToPartialOpCtxJs(context);
executeQueryJs(ctxJs);
},
[|executeQueryJs|],
);

(response, executeQuery);
};