Skip to content

Commit e952560

Browse files
authored
Merge pull request #129 from Arnarkari93/mocked-provider
Bind mocked provider
2 parents a161774 + 7c3b3fb commit e952560

8 files changed

+120
-2
lines changed

src/@apollo/client/ApolloClient__Client.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,4 @@ module Errors = ApolloClient__Errors
6666
module Link = ApolloClient__Link
6767
module React = ApolloClient__React
6868
module Utilities = ApolloClient__Utilities
69+
module Testing = ApolloClient__Testing

src/@apollo/client/core/ApolloClient__Core_ApolloClient.res

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,6 @@ module Js_ = {
305305
@send
306306
external clearStore: t => Js.Promise.t<array<Js.Json.t>> = "clearStore"
307307

308-
309308
// extract(optimistic?: boolean): TCacheShape;
310309
@send
311310
external extract: (t, ~optimistic: bool=?, unit) => Js.Json.t = "extract"
@@ -361,6 +360,9 @@ module Js_ = {
361360
// setLink(newLink: ApolloLink): void;
362361
@send external setLink: (t, ApolloLink.Js_.t) => unit = "setLink"
363362

363+
// stop(): void;
364+
@send external stop: (t, unit) => unit = "stop"
365+
364366
// subscribe<T = any, TVariables = OperationVariables>(options: SubscriptionOptions<TVariables>): Observable<FetchResult<T>>;
365367

366368
@send
@@ -462,6 +464,7 @@ type t = {
462464
restore: (~serializedState: Js.Json.t) => ApolloCache.t<Js.Json.t>,
463465
@as("rescript_setLink")
464466
setLink: ApolloLink.t => unit,
467+
stop: unit => unit,
465468
@as("rescript_subscribe")
466469
subscribe: 'data 'variables 'jsVariables. (
467470
~subscription: module(Operation with
@@ -583,7 +586,7 @@ let make: (
583586
->Js.Promise.then_(value => Js.Promise.resolve(Ok(value)), _)
584587
->Js.Promise.catch(e => Js.Promise.resolve(Error(Utils.ensureError(Any(e)))), _)
585588

586-
let extract = (~optimistic=?, ()) => jsClient->Js_.extract(~optimistic=?optimistic, ())
589+
let extract = (~optimistic=?, ()) => jsClient->Js_.extract(~optimistic?, ())
587590

588591
let mutate = (
589592
type data variables jsVariables,
@@ -761,6 +764,8 @@ let make: (
761764

762765
let setLink = link => jsClient->Js_.setLink(link)
763766

767+
let stop = () => jsClient->Js_.stop()
768+
764769
let subscribe = (
765770
type data variables jsVariables,
766771
~subscription as module(Operation: Operation with
@@ -907,6 +912,7 @@ let make: (
907912
resetStore: resetStore,
908913
restore: restore,
909914
setLink: setLink,
915+
stop: stop,
910916
subscribe: subscribe,
911917
watchQuery: watchQuery,
912918
writeFragment: writeFragment,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module Core = ApolloClient__Testing_Core
2+
module Types = ApolloClient__Testing_Types
3+
module MockedProvider = ApolloClient__Testing_React.MockedProvider
4+
5+
let makeResult = Types.makeResult
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
module Graphql = ApolloClient__Graphql
2+
module ApolloError = ApolloClient__Errors_ApolloError
3+
4+
type rec request<'jsVariables> = {
5+
query: Graphql.documentNode,
6+
variables: 'jsVariables,
7+
}
8+
9+
type result
10+
11+
type mock<'jsVariables> = {
12+
request: request<'jsVariables>,
13+
result: result,
14+
}
15+
16+
type queryResult = {
17+
data: Js.Nullable.t<Js.Json.t>,
18+
error: Js.Nullable.t<ApolloError.t>,
19+
loading: bool,
20+
}
21+
22+
external mockResult: queryResult => result = "%identity"
23+
24+
let makeResult = (
25+
~data: option<'jsData>=?,
26+
~error: option<ApolloError.t>=?,
27+
~loading=false,
28+
toJson: 'jsData => Js.Json.t,
29+
): result => {
30+
let queryResult = {
31+
data: data->Belt.Option.mapWithDefault(Js.Nullable.null, data =>
32+
data->toJson->Js.Nullable.return
33+
),
34+
error: error->Belt.Option.mapWithDefault(Js.Nullable.null, Js.Nullable.return),
35+
loading: loading,
36+
}
37+
38+
mockResult(queryResult)
39+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module Link = ApolloClient__Link_Core_ApolloLink
2+
module Types = ApolloClient__Testing_Types
3+
4+
@new @module("@apollo/client/testing")
5+
external mockLink: (~mocks: array<Types.mock<'jsVariables>>, ~addTypename: bool) => Link.t =
6+
"MockLink"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module MockedProvider = ApolloClient__Testing_React_MockedProvider
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module InMemoryCache = ApolloClient__Cache_InMemory_InMemoryCache
2+
module ApolloClient = ApolloClient__Core_ApolloClient
3+
module ApolloProvider = ApolloClient__React_Context_ApolloProvider
4+
module Core = ApolloClient__Testing_Core
5+
module Types = ApolloClient__Testing_Types
6+
7+
// export interface MockedProviderProps<TSerializedCache = {}> {
8+
// mocks?: ReadonlyArray<MockedResponse>;
9+
// addTypename?: boolean;
10+
// defaultOptions?: DefaultOptions;
11+
// cache?: ApolloCache<TSerializedCache>;
12+
// resolvers?: Resolvers;
13+
// childProps?: object;
14+
// children?: any;
15+
// link?: ApolloLink;
16+
// }
17+
18+
/* Then making an ApolloClient in Rescript, additional wrapper methods (e.g. `rescript_query`)
19+
* are created for the underlying javascript methods. So for the client to be set up correctly,
20+
* we need to do so by calling the rescript method.
21+
*/
22+
@react.component
23+
let make = (
24+
~addTypename=true,
25+
~cache=?,
26+
~childProps=?,
27+
~children,
28+
~defaultOptions=?,
29+
~link=?,
30+
~mocks: array<Types.mock<'jsVariables>>,
31+
~resolvers=?,
32+
) => {
33+
let client = React.useRef(
34+
ApolloClient.make(
35+
~cache=cache->Belt.Option.getWithDefault(InMemoryCache.make(~addTypename, ())),
36+
~defaultOptions?,
37+
~link=link->Belt.Option.getWithDefault(Core.mockLink(~mocks, ~addTypename)),
38+
~resolvers?,
39+
(),
40+
),
41+
)
42+
43+
React.useEffect0(() => {
44+
Some(client.current.stop)
45+
})
46+
47+
<ApolloProvider client=client.current>
48+
{React.cloneElement(
49+
React.Children.only(children),
50+
childProps->Belt.Option.mapWithDefault(Js.Obj.empty(), props =>
51+
Js.Obj.assign(props, Js.Obj.empty())
52+
),
53+
)}
54+
</ApolloProvider>
55+
}

src/ReasonMLCommunity__ApolloClient.res

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ module GraphQL_PPX = {
6464
type templateTagReturnType = ApolloClient__Graphql.documentNode
6565
}
6666

67+
module Testing = {
68+
module MockedProvider = ApolloClient__Testing.MockedProvider
69+
let makeResult = ApolloClient__Testing_Types.makeResult
70+
}
71+
6772
// Convenient access to all types and the methods for working with those types
6873
module Types = {
6974
module ApolloError = ApolloClient__Errors_ApolloError

0 commit comments

Comments
 (0)