From 2e5fa3d3f6a97b53b2e67883d5053bea9494e442 Mon Sep 17 00:00:00 2001 From: rob Date: Mon, 20 Dec 2021 19:23:26 -0500 Subject: [PATCH 1/3] fix: compatibility issue with @apollo/client >= 3.5 --- .../react/types/ApolloClient__React_Types.res | 263 +++++++++--------- 1 file changed, 124 insertions(+), 139 deletions(-) diff --git a/src/@apollo/client/react/types/ApolloClient__React_Types.res b/src/@apollo/client/react/types/ApolloClient__React_Types.res index d03b57f..48a2e41 100644 --- a/src/@apollo/client/react/types/ApolloClient__React_Types.res +++ b/src/@apollo/client/react/types/ApolloClient__React_Types.res @@ -22,28 +22,32 @@ module QueryHookOptions = { // export interface QueryHookOptions extends QueryFunctionOptions { // query?: DocumentNode; // } - type t<'jsData, 'jsVariables> = { - query: option, + type t<'jsData, 'jsVariables> + + @obj + external make: ( + ~query: Graphql.documentNode=?, // ...extends QueryFunctionOptions - displayName: option, - skip: option, - onCompleted: option<'jsData => unit>, - onError: option<(. ApolloError.Js_.t) => unit>, + ~displayName: string=?, + ~skip: bool=?, + ~onCompleted: 'jsData => unit=?, + ~onError: (. ApolloError.Js_.t) => unit=?, // ..extends BaseQueryOptions - client: option, - context: option, // ACTUAL: Record - errorPolicy: option, - fetchPolicy: option, - nextFetchPolicy: option, - notifyOnNetworkStatusChange: option, - partialRefetch: option, - pollInterval: option, + ~client: ApolloClient.t=?, + ~context: Js.Json.t=?, // ACTUAL: Record + ~errorPolicy: string=?, + ~fetchPolicy: string=?, + ~nextFetchPolicy: string=?, + ~notifyOnNetworkStatusChange: bool=?, + ~partialRefetch: bool=?, + ~pollInterval: int=?, // INTENTIONALLY IGNORED (but now with safeParse and result unwrapping, maybe it shouldn't be?) - // returnPartialData: option(bool), - ssr: option, + // ~returnPartialData: bool=?, + ~ssr: bool=?, // We don't allow optional variables because it's not typesafe - variables: 'jsVariables, - } + ~variables: 'jsVariables, + unit, + ) => t<'jsData, 'jsVariables> = "" } type t<'data, 'variables> = { @@ -73,27 +77,29 @@ module QueryHookOptions = { ~mapJsVariables: 'jsVariables => 'jsVariables, ~safeParse: Types.safeParse<'data, 'jsData>, ~serializeVariables: 'variables => 'jsVariables, - ): Js_.t<'jsData, 'jsVariables> => { - client: t.client, - context: t.context, - displayName: t.displayName, - errorPolicy: t.errorPolicy->Belt.Option.map(ErrorPolicy.toJs), - onCompleted: t.onCompleted->Belt.Option.map((onCompleted, jsData) => - onCompleted(jsData->safeParse) - ), - onError: t.onError->Belt.Option.map((onError, . jsApolloError) => - onError(ApolloError.fromJs(jsApolloError)) - ), - fetchPolicy: t.fetchPolicy->Belt.Option.map(WatchQueryFetchPolicy.toJs), - nextFetchPolicy: t.nextFetchPolicy->Belt.Option.map(WatchQueryFetchPolicy.toJs), - notifyOnNetworkStatusChange: t.notifyOnNetworkStatusChange, - query: t.query, - pollInterval: t.pollInterval, - partialRefetch: t.partialRefetch, - skip: t.skip, - ssr: t.ssr, - variables: t.variables->serializeVariables->mapJsVariables, - } + ): Js_.t<'jsData, 'jsVariables> => + Js_.make( + ~client=?t.client, + ~context=?t.context, + ~displayName=?t.displayName, + ~errorPolicy=?t.errorPolicy->Belt.Option.map(ErrorPolicy.toJs), + ~onCompleted=?t.onCompleted->Belt.Option.map((onCompleted, jsData) => + onCompleted(jsData->safeParse) + ), + ~onError=?t.onError->Belt.Option.map((onError, . jsApolloError) => + onError(ApolloError.fromJs(jsApolloError)) + ), + ~fetchPolicy=?t.fetchPolicy->Belt.Option.map(WatchQueryFetchPolicy.toJs), + ~nextFetchPolicy=?t.nextFetchPolicy->Belt.Option.map(WatchQueryFetchPolicy.toJs), + ~notifyOnNetworkStatusChange=?t.notifyOnNetworkStatusChange, + ~query=?t.query, + ~pollInterval=?t.pollInterval, + ~partialRefetch=?t.partialRefetch, + ~skip=?t.skip, + ~ssr=?t.ssr, + ~variables=t.variables->serializeVariables->mapJsVariables, + (), + ) } module LazyQueryHookOptions = { @@ -101,42 +107,31 @@ module LazyQueryHookOptions = { // export interface LazyQueryHookOptions extends Omit, 'skip'> { // query?: DocumentNode; // } - @deriving(abstract) - type t<'jsData, 'jsVariables> = { - @optional - query: Graphql.documentNode, + + type t<'jsData, 'jsVariables> + + @obj + external make: ( + ~query: Graphql.documentNode=?, // ...extends QueryFunctionOptions - @optional - displayName: string, - @optional - onCompleted: 'jsData => unit, - @optional - onError: (. ApolloError.Js_.t) => unit, + ~displayName: string=?, + ~onCompleted: 'jsData => unit=?, + ~onError: (. ApolloError.Js_.t) => unit=?, // ..extends BaseQueryOptions - @optional - client: ApolloClient.t, - @optional - context: Js.Json.t, // ACTUAL: Record - @optional - errorPolicy: string, - @optional - fetchPolicy: string, - @optional - nextFetchPolicy: string, - @optional - notifyOnNetworkStatusChange: bool, - @optional - partialRefetch: bool, - @optional - pollInterval: int, + ~client: ApolloClient.t=?, + ~context: Js.Json.t=?, // ACTUAL: Record t<'jsData, 'jsVariables> = "" } type t<'data, 'variables> = { @@ -730,44 +725,26 @@ module MutationHookOptions = { // export interface MutationHookOptions extends BaseMutationOptions { // mutation?: DocumentNode; // } - @ocaml.doc(" - * We use deriving abstract here because option('jsVariables) is not typesafe, - * but still needs to be optional since a user has the option of providing - * the variables to the results of the hook rather than the hook itself - ") - @deriving(abstract) - type t<'jsData, 'jsVariables> = { - @optional - mutation: Graphql.documentNode, + type t<'jsData, 'jsVariables> + @obj + external make: ( + ~mutation: Graphql.documentNode=?, // ...extends BaseMutationOptions - @optional - awaitRefetchQueries: bool, - @optional - client: ApolloClient.t, // Non-Js_ client is appropriate here - @optional - context: Js.Json.t, // actual: option(Context) - @optional - errorPolicy: ErrorPolicy.Js_.t, - @optional - fetchPolicy: FetchPolicy__noCacheExtracted.Js_.t, - @optional - ignoreResults: bool, - @optional - notifyOnNetworkStatusChange: bool, - @optional - onError: (. ApolloError.Js_.t) => unit, - @optional - onCompleted: 'jsData => unit, - @optional - optimisticResponse: 'jsVariables => 'jsData, - @optional - refetchQueries: RefetchQueryDescription.Js_.t, - @optional - update: MutationUpdaterFn.Js_.t<'jsData>, - @optional - variables: 'jsVariables, - } - let make = t + ~awaitRefetchQueries: bool=?, + ~client: ApolloClient.t=?, // Non-Js_ client is appropriate here + ~context: Js.Json.t=?, // actual: option(Context) + ~errorPolicy: ErrorPolicy.Js_.t=?, + ~fetchPolicy: FetchPolicy__noCacheExtracted.Js_.t=?, + ~ignoreResults: bool=?, + ~notifyOnNetworkStatusChange: bool=?, + ~onError: (. ApolloError.Js_.t) => unit=?, + ~onCompleted: 'jsData => unit=?, + ~optimisticResponse: 'jsVariables => 'jsData=?, + ~refetchQueries: RefetchQueryDescription.Js_.t=?, + ~update: MutationUpdaterFn.Js_.t<'jsData>=?, + ~variables: 'jsVariables=?, + unit, + ) => t<'jsData, 'jsVariables> = "" } type t<'data, 'variables, 'jsVariables> = { @@ -813,8 +790,8 @@ module MutationHookOptions = { onError(ApolloError.fromJs(jsApolloError)) ), ~onCompleted=?t.onCompleted->Belt.Option.map((onCompleted, jsData) => - onCompleted(jsData->safeParse) - ), + onCompleted(jsData->safeParse) + ), ~optimisticResponse=?t.optimisticResponse->Belt.Option.map((optimisticResponse, variables) => optimisticResponse(variables)->serialize ), @@ -1203,18 +1180,22 @@ module SubscriptionHookOptions = { // export interface SubscriptionHookOptions extends BaseSubscriptionOptions { // subscription?: DocumentNode; // } - type t<'jsData, 'jsVariables> = { - subscription: option, + type t<'jsData, 'jsVariables> + + @obj + external make: ( + ~subscription: Graphql.documentNode=?, // ...extends BaseSubscriptionOptions // Intentionally restricted to not be non-optional. `option(unit)` does not compile cleanly to `undefined` - variables: 'jsVariables, - fetchPolicy: option, - shouldResubscribe: option<(. BaseSubscriptionOptions.Js_.t<'jsData, 'jsVariables>) => bool>, - client: option, - skip: option, - onSubscriptionData: option<(. OnSubscriptionDataOptions.Js_.t<'jsData>) => unit>, - onSubscriptionComplete: option unit>, - } + ~variables: 'jsVariables, + ~fetchPolicy: FetchPolicy.t=?, + ~shouldResubscribe: (. BaseSubscriptionOptions.Js_.t<'jsData, 'jsVariables>) => bool=?, + ~client: ApolloClient.t=?, + ~skip: bool=?, + ~onSubscriptionData: (. OnSubscriptionDataOptions.Js_.t<'jsData>) => unit=?, + ~onSubscriptionComplete: unit => unit=?, + unit, + ) => t<'jsData, 'jsVariables> = "" } type t<'data, 'variables, 'jsVariables> = { @@ -1233,22 +1214,26 @@ module SubscriptionHookOptions = { ~mapJsVariables: 'jsVariables => 'jsVariables, ~safeParse: Types.safeParse<'data, 'jsData>, ~serializeVariables: 'variables => 'jsVariables, - ) => Js_.t<'jsData, 'jsVariables> = (t, ~mapJsVariables, ~safeParse, ~serializeVariables) => { - subscription: t.subscription, - variables: t.variables->serializeVariables->mapJsVariables, - fetchPolicy: t.fetchPolicy, - shouldResubscribe: t.shouldResubscribe->Belt.Option.map(( - shouldResubscribe, - . jsBaseSubscriptionOptions, - ) => shouldResubscribe(jsBaseSubscriptionOptions->BaseSubscriptionOptions.fromJs)), - client: t.client, - skip: t.skip, - onSubscriptionData: t.onSubscriptionData->Belt.Option.map(( - onSubscriptionData, - . jsOnSubscriptionDataOptions, - ) => - onSubscriptionData(jsOnSubscriptionDataOptions->OnSubscriptionDataOptions.fromJs(~safeParse)) - ), - onSubscriptionComplete: t.onSubscriptionComplete, - } + ) => Js_.t<'jsData, 'jsVariables> = (t, ~mapJsVariables, ~safeParse, ~serializeVariables) => + Js_.make( + ~subscription=?t.subscription, + ~variables=t.variables->serializeVariables->mapJsVariables, + ~fetchPolicy=?t.fetchPolicy, + ~shouldResubscribe=?t.shouldResubscribe->Belt.Option.map(( + shouldResubscribe, + . jsBaseSubscriptionOptions, + ) => shouldResubscribe(jsBaseSubscriptionOptions->BaseSubscriptionOptions.fromJs)), + ~client=?t.client, + ~skip=?t.skip, + ~onSubscriptionData=?t.onSubscriptionData->Belt.Option.map(( + onSubscriptionData, + . jsOnSubscriptionDataOptions, + ) => + onSubscriptionData( + jsOnSubscriptionDataOptions->OnSubscriptionDataOptions.fromJs(~safeParse), + ) + ), + ~onSubscriptionComplete=?t.onSubscriptionComplete, + (), + ) } From a06d0db5c5048282aa67d36c6eb6b52fdf2efd3d Mon Sep 17 00:00:00 2001 From: rob Date: Mon, 20 Dec 2021 19:37:24 -0500 Subject: [PATCH 2/3] chore: formatting --- src/@apollo/client/react/types/ApolloClient__React_Types.res | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/@apollo/client/react/types/ApolloClient__React_Types.res b/src/@apollo/client/react/types/ApolloClient__React_Types.res index 48a2e41..74c7c4a 100644 --- a/src/@apollo/client/react/types/ApolloClient__React_Types.res +++ b/src/@apollo/client/react/types/ApolloClient__React_Types.res @@ -119,7 +119,7 @@ module LazyQueryHookOptions = { ~onError: (. ApolloError.Js_.t) => unit=?, // ..extends BaseQueryOptions ~client: ApolloClient.t=?, - ~context: Js.Json.t=?, // ACTUAL: Record, ~errorPolicy: string=?, ~fetchPolicy: string=?, ~nextFetchPolicy: string=?, @@ -726,6 +726,7 @@ module MutationHookOptions = { // mutation?: DocumentNode; // } type t<'jsData, 'jsVariables> + @obj external make: ( ~mutation: Graphql.documentNode=?, From 490e68a6fd07ccba6b86d82998d1f73d8009336d Mon Sep 17 00:00:00 2001 From: rob Date: Tue, 21 Dec 2021 16:11:25 -0500 Subject: [PATCH 3/3] docs: add note to hook options --- .../client/react/types/ApolloClient__React_Types.res | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/@apollo/client/react/types/ApolloClient__React_Types.res b/src/@apollo/client/react/types/ApolloClient__React_Types.res index 74c7c4a..749e15f 100644 --- a/src/@apollo/client/react/types/ApolloClient__React_Types.res +++ b/src/@apollo/client/react/types/ApolloClient__React_Types.res @@ -24,6 +24,7 @@ module QueryHookOptions = { // } type t<'jsData, 'jsVariables> + // NOTE: We are using @obj here because passing properties that are defined with a value of undefined has effects @obj external make: ( ~query: Graphql.documentNode=?, @@ -110,6 +111,7 @@ module LazyQueryHookOptions = { type t<'jsData, 'jsVariables> + // NOTE: We are using @obj here because passing properties that are defined with a value of undefined has effects @obj external make: ( ~query: Graphql.documentNode=?, @@ -727,6 +729,7 @@ module MutationHookOptions = { // } type t<'jsData, 'jsVariables> + // NOTE: We are using @obj here because passing properties that are defined with a value of undefined has effects @obj external make: ( ~mutation: Graphql.documentNode=?, @@ -859,10 +862,6 @@ module MutationFunctionOptions = { // context?: Context; // fetchPolicy?: WatchQueryFetchPolicy; // } - @ocaml.doc(" - * We use deriving abstract here because this is used in a context where passing in explicit - * properties could override one already passed in - ") type t<'jsData, 'jsVariables> = { // We don't allow optional variables because it's not typesafe variables: 'jsVariables, @@ -1183,6 +1182,7 @@ module SubscriptionHookOptions = { // } type t<'jsData, 'jsVariables> + // NOTE: We are using @obj here because passing properties that are defined with a value of undefined has effects @obj external make: ( ~subscription: Graphql.documentNode=?,