Skip to content

Commit

Permalink
Merge pull request #129 from Arnarkari93/mocked-provider
Browse files Browse the repository at this point in the history
Bind mocked provider
  • Loading branch information
jeddeloh authored Dec 1, 2021
2 parents a161774 + 7c3b3fb commit e952560
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/@apollo/client/ApolloClient__Client.res
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ module Errors = ApolloClient__Errors
module Link = ApolloClient__Link
module React = ApolloClient__React
module Utilities = ApolloClient__Utilities
module Testing = ApolloClient__Testing
10 changes: 8 additions & 2 deletions src/@apollo/client/core/ApolloClient__Core_ApolloClient.res
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ module Js_ = {
@send
external clearStore: t => Js.Promise.t<array<Js.Json.t>> = "clearStore"


// extract(optimistic?: boolean): TCacheShape;
@send
external extract: (t, ~optimistic: bool=?, unit) => Js.Json.t = "extract"
Expand Down Expand Up @@ -361,6 +360,9 @@ module Js_ = {
// setLink(newLink: ApolloLink): void;
@send external setLink: (t, ApolloLink.Js_.t) => unit = "setLink"

// stop(): void;
@send external stop: (t, unit) => unit = "stop"

// subscribe<T = any, TVariables = OperationVariables>(options: SubscriptionOptions<TVariables>): Observable<FetchResult<T>>;

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

let extract = (~optimistic=?, ()) => jsClient->Js_.extract(~optimistic=?optimistic, ())
let extract = (~optimistic=?, ()) => jsClient->Js_.extract(~optimistic?, ())

let mutate = (
type data variables jsVariables,
Expand Down Expand Up @@ -761,6 +764,8 @@ let make: (

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

let stop = () => jsClient->Js_.stop()

let subscribe = (
type data variables jsVariables,
~subscription as module(Operation: Operation with
Expand Down Expand Up @@ -907,6 +912,7 @@ let make: (
resetStore: resetStore,
restore: restore,
setLink: setLink,
stop: stop,
subscribe: subscribe,
watchQuery: watchQuery,
writeFragment: writeFragment,
Expand Down
5 changes: 5 additions & 0 deletions src/@apollo/client/testing/ApolloClient__Testing.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module Core = ApolloClient__Testing_Core
module Types = ApolloClient__Testing_Types
module MockedProvider = ApolloClient__Testing_React.MockedProvider

let makeResult = Types.makeResult
39 changes: 39 additions & 0 deletions src/@apollo/client/testing/ApolloClient__Testing_Types.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
module Graphql = ApolloClient__Graphql
module ApolloError = ApolloClient__Errors_ApolloError

type rec request<'jsVariables> = {
query: Graphql.documentNode,
variables: 'jsVariables,
}

type result

type mock<'jsVariables> = {
request: request<'jsVariables>,
result: result,
}

type queryResult = {
data: Js.Nullable.t<Js.Json.t>,
error: Js.Nullable.t<ApolloError.t>,
loading: bool,
}

external mockResult: queryResult => result = "%identity"

let makeResult = (
~data: option<'jsData>=?,
~error: option<ApolloError.t>=?,
~loading=false,
toJson: 'jsData => Js.Json.t,
): result => {
let queryResult = {
data: data->Belt.Option.mapWithDefault(Js.Nullable.null, data =>
data->toJson->Js.Nullable.return
),
error: error->Belt.Option.mapWithDefault(Js.Nullable.null, Js.Nullable.return),
loading: loading,
}

mockResult(queryResult)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Link = ApolloClient__Link_Core_ApolloLink
module Types = ApolloClient__Testing_Types

@new @module("@apollo/client/testing")
external mockLink: (~mocks: array<Types.mock<'jsVariables>>, ~addTypename: bool) => Link.t =
"MockLink"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module MockedProvider = ApolloClient__Testing_React_MockedProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module InMemoryCache = ApolloClient__Cache_InMemory_InMemoryCache
module ApolloClient = ApolloClient__Core_ApolloClient
module ApolloProvider = ApolloClient__React_Context_ApolloProvider
module Core = ApolloClient__Testing_Core
module Types = ApolloClient__Testing_Types

// export interface MockedProviderProps<TSerializedCache = {}> {
// mocks?: ReadonlyArray<MockedResponse>;
// addTypename?: boolean;
// defaultOptions?: DefaultOptions;
// cache?: ApolloCache<TSerializedCache>;
// resolvers?: Resolvers;
// childProps?: object;
// children?: any;
// link?: ApolloLink;
// }

/* Then making an ApolloClient in Rescript, additional wrapper methods (e.g. `rescript_query`)
* are created for the underlying javascript methods. So for the client to be set up correctly,
* we need to do so by calling the rescript method.
*/
@react.component
let make = (
~addTypename=true,
~cache=?,
~childProps=?,
~children,
~defaultOptions=?,
~link=?,
~mocks: array<Types.mock<'jsVariables>>,
~resolvers=?,
) => {
let client = React.useRef(
ApolloClient.make(
~cache=cache->Belt.Option.getWithDefault(InMemoryCache.make(~addTypename, ())),
~defaultOptions?,
~link=link->Belt.Option.getWithDefault(Core.mockLink(~mocks, ~addTypename)),
~resolvers?,
(),
),
)

React.useEffect0(() => {
Some(client.current.stop)
})

<ApolloProvider client=client.current>
{React.cloneElement(
React.Children.only(children),
childProps->Belt.Option.mapWithDefault(Js.Obj.empty(), props =>
Js.Obj.assign(props, Js.Obj.empty())
),
)}
</ApolloProvider>
}
5 changes: 5 additions & 0 deletions src/ReasonMLCommunity__ApolloClient.res
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ module GraphQL_PPX = {
type templateTagReturnType = ApolloClient__Graphql.documentNode
}

module Testing = {
module MockedProvider = ApolloClient__Testing.MockedProvider
let makeResult = ApolloClient__Testing_Types.makeResult
}

// Convenient access to all types and the methods for working with those types
module Types = {
module ApolloError = ApolloClient__Errors_ApolloError
Expand Down

0 comments on commit e952560

Please sign in to comment.