Skip to content
This repository has been archived by the owner on Nov 20, 2020. It is now read-only.

Commit

Permalink
Merge pull request #69 from mbirkegaard/feat/optimisticResponse
Browse files Browse the repository at this point in the history
Feat/optimistic response
  • Loading branch information
fakenickels authored Dec 17, 2019
2 parents e39dbdb + 370ceec commit 20103bb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
30 changes: 30 additions & 0 deletions examples/persons/src/EditPerson.re
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,34 @@ module EditPersonMutation = [%graphql
|}
];

module OptimisticResponse = {
type t = {
.
"__typename": string,
"updatePerson": {
.
"__typename": string,
"age": int,
"id": string,
"name": string,
},
};

external cast: t => Js.Json.t = "%identity";

let make = (~id, ~name, ~age) =>
{
"__typename": "Mutation",
"updatePerson": {
"__typename": "Person",
"id": id,
"name": name,
"age": age,
},
}
->cast;
};

type state = {
id: string,
age: option(int),
Expand Down Expand Up @@ -72,6 +100,8 @@ let make = () => {
~name=state.name,
(),
),
~optimisticResponse=
OptimisticResponse.make(~id=state.id, ~name=state.name, ~age),
(),
)
|> ignore
Expand Down
8 changes: 8 additions & 0 deletions src/ApolloHooksMutation.re
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type options('a) = {
awaitRefetchQueries: bool,
[@bs.optional]
update: (ApolloClient.generatedApolloClient, mutationResult('a)) => unit,
[@bs.optional]
optimisticResponse: Js.Json.t,
};

type jsResult = {
Expand All @@ -65,6 +67,7 @@ type mutation('a) =
~client: ApolloClient.generatedApolloClient=?,
~refetchQueries: refetchQueries=?,
~awaitRefetchQueries: bool=?,
~optimisticResponse: Js.Json.t=?,
unit
) =>
Js.Promise.t(controlledVariantResult('a));
Expand All @@ -85,6 +88,7 @@ let useMutation:
~update: (ApolloClient.generatedApolloClient, mutationResult('data)) =>
unit
=?,
~optimisticResponse: Js.Json.t=?,
ApolloHooksTypes.graphqlDefinition('data, _, _)
) =>
(
Expand All @@ -98,6 +102,7 @@ let useMutation:
~refetchQueries=?,
~awaitRefetchQueries=?,
~update=?,
~optimisticResponse=?,
(parse, query, _),
) => {
let (jsMutate, jsResult) =
Expand All @@ -109,6 +114,7 @@ let useMutation:
~refetchQueries?,
~awaitRefetchQueries?,
~update?,
~optimisticResponse?,
(),
),
);
Expand All @@ -121,6 +127,7 @@ let useMutation:
~client=?,
~refetchQueries=?,
~awaitRefetchQueries=?,
~optimisticResponse=?,
(),
) =>
jsMutate(.
Expand All @@ -129,6 +136,7 @@ let useMutation:
~client?,
~refetchQueries?,
~awaitRefetchQueries?,
~optimisticResponse?,
(),
),
)
Expand Down

0 comments on commit 20103bb

Please sign in to comment.