Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix options creation for @apollo/client >= 3.5 #134

Merged
Merged
Changes from 2 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
264 changes: 125 additions & 139 deletions src/@apollo/client/react/types/ApolloClient__React_Types.res
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,32 @@ module QueryHookOptions = {
// export interface QueryHookOptions<TData = any, TVariables = OperationVariables> extends QueryFunctionOptions<TData, TVariables> {
// query?: DocumentNode;
// }
type t<'jsData, 'jsVariables> = {
query: option<Graphql.documentNode>,
type t<'jsData, 'jsVariables>

@obj
illusionalsagacity marked this conversation as resolved.
Show resolved Hide resolved
external make: (
~query: Graphql.documentNode=?,
// ...extends QueryFunctionOptions
displayName: option<string>,
skip: option<bool>,
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<ApolloClient.t>,
context: option<Js.Json.t>, // ACTUAL: Record<string, any>
errorPolicy: option<string>,
fetchPolicy: option<string>,
nextFetchPolicy: option<string>,
notifyOnNetworkStatusChange: option<bool>,
partialRefetch: option<bool>,
pollInterval: option<int>,
~client: ApolloClient.t=?,
~context: Js.Json.t=?, // ACTUAL: Record<string, any=?>
~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<bool>,
// ~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> = {
Expand Down Expand Up @@ -73,70 +77,61 @@ 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 = {
module Js_ = {
// export interface LazyQueryHookOptions<TData = any, TVariables = OperationVariables> extends Omit<QueryFunctionOptions<TData, TVariables>, '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<string, any>
@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<string, any>,
~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),
@optional
ssr: bool,
@optional
variables: 'jsVariables,
}
let make = t
// ~returnPartialData:bool=?,
~ssr: bool=?,
~variables: 'jsVariables=?,
unit,
) => t<'jsData, 'jsVariables> = ""
}

type t<'data, 'variables> = {
Expand Down Expand Up @@ -730,44 +725,27 @@ module MutationHookOptions = {
// export interface MutationHookOptions<TData = any, TVariables = OperationVariables> extends BaseMutationOptions<TData, TVariables> {
// 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
")
illusionalsagacity marked this conversation as resolved.
Show resolved Hide resolved
@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> = {
Expand Down Expand Up @@ -813,8 +791,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
),
Expand Down Expand Up @@ -1203,18 +1181,22 @@ module SubscriptionHookOptions = {
// export interface SubscriptionHookOptions<TData = any, TVariables = OperationVariables> extends BaseSubscriptionOptions<TData, TVariables> {
// subscription?: DocumentNode;
// }
type t<'jsData, 'jsVariables> = {
subscription: option<Graphql.documentNode>,
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<FetchPolicy.t>,
shouldResubscribe: option<(. BaseSubscriptionOptions.Js_.t<'jsData, 'jsVariables>) => bool>,
client: option<ApolloClient.t>,
skip: option<bool>,
onSubscriptionData: option<(. OnSubscriptionDataOptions.Js_.t<'jsData>) => unit>,
onSubscriptionComplete: option<unit => 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> = {
Expand All @@ -1233,22 +1215,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,
(),
)
}