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

Feat/optimistic response #69

Merged
merged 2 commits into from
Dec 17, 2019
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
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 = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a comment that this is necessary because the types graphql_ppx_re generates are not 1:1 to the JS representation and probably this won't be necessary as soon as teamwalnut/graphql-ppx#20 gets merged?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't have time to check back in before now. Would you still like me to add it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be appreciated! I haven't published yet

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On it.

.
"__typename": string,
"updatePerson": {
.
"__typename": string,
"age": int,
"id": string,
"name": string,
},
};
Comment on lines +15 to +25
Copy link
Collaborator Author

@mbirkegaard mbirkegaard Dec 15, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it it's is now, this should be compatible with all versions of bucklescript, but can be changed to only work with bucklescript >= 7 if that's preferred.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright!


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