Skip to content

Commit

Permalink
refactor: convert @obj and @deriving(abstract) to use optional record…
Browse files Browse the repository at this point in the history
… fields
  • Loading branch information
illusionalsagacity committed Jan 17, 2024
1 parent f13c180 commit 6e92f9b
Show file tree
Hide file tree
Showing 5 changed files with 248 additions and 312 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,16 @@ module InMemoryCacheConfig = {
// possibleTypes?: PossibleTypesMap;
// typePolicies?: TypePolicies;
// }
// NOTE: Using deriving abstract here because passing properties that are undefined has effects
@deriving(abstract)
type t = {
@optional
resultCaching: bool,
@optional
possibleTypes: PossibleTypesMap.Js_.t,
@optional
typePolicies: TypePolicies.Js_.t,
resultCaching?: bool,
possibleTypes?: PossibleTypesMap.Js_.t,
typePolicies?: TypePolicies.Js_.t,
// ...extends ApolloReducerConfig
@optional
dataIdFromObject: KeyFieldsFunction.Js_.t,
@optional
addTypename: bool,
dataIdFromObject?: KeyFieldsFunction.Js_.t,
addTypename?: bool,
}
}
type t = Js_.t
let make = Js_.t
}

module Js_ = {
Expand Down Expand Up @@ -88,13 +80,10 @@ let make: (
~typePolicies=?,
(),
) =>
Js_.make(
InMemoryCacheConfig.make(
~addTypename?,
~dataIdFromObject?,
~possibleTypes?,
~resultCaching?,
~typePolicies=?typePolicies->Belt.Option.map(TypePolicies.toJs),
(),
),
)->ApolloCache.fromJs
Js_.make({
?addTypename,
?dataIdFromObject,
?possibleTypes,
?resultCaching,
typePolicies: ?typePolicies->Belt.Option.map(TypePolicies.toJs),
})->ApolloCache.fromJs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,10 @@ module SelectionSetNode = ApolloClient__Graphql.Language.Ast.SelectionSetNode

module KeyFieldsContext = {
type t = {
typename: option<string>,
selectionSet: option<SelectionSetNode.t>,
fragment: option<FragmentMap.t>,
keyObject: option<Js.Json.t>,
}
module Js_ = {
// declare type KeyFieldsContext = {
// typename?: string;
// selectionSet?: SelectionSetNode;
// fragmentMap?: FragmentMap;
// keyObject?: Record<string, any>;
// };
type t = t = {
typename: option<string>,
selectionSet: option<SelectionSetNode.t>,
fragment: option<FragmentMap.Js_.t>,
keyObject: option<Js.Json.t>,
}
typename?: string,
selectionSet?: SelectionSetNode.t,
fragment?: FragmentMap.t,
keyObject?: Js.Json.t,
}
}

Expand Down Expand Up @@ -126,11 +112,11 @@ module TypePolicy = {
~subscriptionType: bool=?,
unit,
) => t = (~fields=?, ~keyFields=?, ~mutationType=?, ~queryType=?, ~subscriptionType=?, ()) => {
fields: fields,
keyFields: keyFields,
mutationType: mutationType,
queryType: queryType,
subscriptionType: subscriptionType,
fields,
keyFields,
mutationType,
queryType,
subscriptionType,
}
}

Expand Down
79 changes: 39 additions & 40 deletions src/@apollo/client/core/ApolloClient__Core_WatchQueryOptions.res
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module PureQueryOptions = ApolloClient__Core_Types.PureQueryOptions
module ErrorPolicy = {
module Js_ = {
// export declare type ErrorPolicy = 'none' | 'ignore' | 'all';
type t = string
type t = [#none | #ignore | #all]
}

type t =
Expand All @@ -18,16 +18,16 @@ module ErrorPolicy = {

let toJs = x =>
switch x {
| None => "none"
| Ignore => "ignore"
| All => "all"
| None => #none
| Ignore => #ignore
| All => #all
}
}

module FetchPolicy = {
module Js_ = {
// export declare type FetchPolicy = 'cache-first' | 'network-only' | 'cache-only' | 'no-cache' | 'standby';
type t = string
type t = [#"cache-first" | #"network-only" | #"cache-only" | #"no-cache" | #standby]
}

type t =
Expand All @@ -37,13 +37,13 @@ module FetchPolicy = {
| NoCache
| Standby

let toJs = x =>
let toJs = (x): Js_.t =>
switch x {
| CacheFirst => "cache-first"
| CacheOnly => "cache-only"
| NetworkOnly => "network-only"
| NoCache => "no-cache"
| Standby => "standby"
| CacheFirst => #"cache-first"
| CacheOnly => #"cache-only"
| NetworkOnly => #"network-only"
| NoCache => #"no-cache"
| Standby => #standby
}
}

Expand All @@ -63,7 +63,7 @@ module FetchPolicy__noCacheExtracted = {
module WatchQueryFetchPolicy = {
module Js_ = {
// export declare type WatchQueryFetchPolicy = FetchPolicy | 'cache-and-network';
type t = string
type t = [FetchPolicy.Js_.t | #"cache-and-network"]
}

type t =
Expand All @@ -74,14 +74,14 @@ module WatchQueryFetchPolicy = {
| NoCache
| Standby

let toJs = x =>
let toJs = (x): Js_.t =>
switch x {
| CacheAndNetwork => "cache-and-network"
| CacheFirst => "cache-first"
| CacheOnly => "cache-only"
| NetworkOnly => "network-only"
| NoCache => "no-cache"
| Standby => "standby"
| CacheAndNetwork => #"cache-and-network"
| CacheFirst => #"cache-first"
| CacheOnly => #"cache-only"
| NetworkOnly => #"network-only"
| NoCache => #"no-cache"
| Standby => #standby
}
}

Expand Down Expand Up @@ -198,24 +198,23 @@ module UpdateQueryFn = {
~querySafeParse,
~querySerialize,
~subscriptionSafeParse,
. jsQueryData,
{subscriptionData: {data}},
) =>
switch (jsQueryData->querySafeParse, data->subscriptionSafeParse) {
| (Ok(queryData), Ok(subscriptionData)) =>
t(
queryData,
{
subscriptionData: {
data: subscriptionData,
(. jsQueryData, {subscriptionData: {data}}) =>
switch (jsQueryData->querySafeParse, data->subscriptionSafeParse) {
| (Ok(queryData), Ok(subscriptionData)) =>
t(
queryData,
{
subscriptionData: {
data: subscriptionData,
},
},
},
)->querySerialize
| (Error(parseError), _)
| (_, Error(parseError)) =>
onParseError(parseError)
jsQueryData
}
)->querySerialize
| (Error(parseError), _)
| (_, Error(parseError)) =>
onParseError(parseError)
jsQueryData
}
}

module SubscribeToMoreOptions = {
Expand Down Expand Up @@ -326,9 +325,9 @@ module MutationUpdaterFn = {
let toJs: (t<'data>, ~safeParse: Types.safeParse<'data, 'jsData>) => Js_.t<'jsData> = (
mutationUpdaterFn,
~safeParse,
. cache,
jsFetchResult,
) => mutationUpdaterFn(cache, jsFetchResult->FetchResult.fromJs(~safeParse))
) =>
(. cache, jsFetchResult) =>
mutationUpdaterFn(cache, jsFetchResult->FetchResult.fromJs(~safeParse))
}

module RefetchQueryDescription = {
Expand Down Expand Up @@ -418,8 +417,8 @@ module MutationOptions = {
errorPolicy: t.errorPolicy->Belt.Option.map(ErrorPolicy.toJs),
fetchPolicy: t.fetchPolicy->Belt.Option.map(FetchPolicy__noCacheExtracted.toJs),
mutation: t.mutation,
optimisticResponse: t.optimisticResponse->Belt.Option.map((optimisticResponse, . variables) =>
optimisticResponse(variables)->serialize
optimisticResponse: t.optimisticResponse->Belt.Option.map(optimisticResponse =>
(. variables) => optimisticResponse(variables)->serialize
),
refetchQueries: t.refetchQueries->Belt.Option.map(RefetchQueryDescription.toJs),
update: t.update->Belt.Option.map(MutationUpdaterFn.toJs(~safeParse)),
Expand Down
10 changes: 5 additions & 5 deletions src/@apollo/client/errors/ApolloClient__Errors_ApolloError.res
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ module Js_ = {
} else if (error && typeof error.message === "string" && error.extensions) {
return makeApolloError({graphQLErrors: [error]});
} else {
return makeApolloError({networkError: ensureError(error)})
return makeApolloError({networkError: ensureError(error)})
}
}
`)(error, make, ensureError)
Expand Down Expand Up @@ -161,11 +161,11 @@ let make: (
unit,
) => t = (~graphQLErrors=?, ~networkError=?, ~errorMessage=?, ~extraInfo=?, ()) => {
let errorWithoutNetworkError = Js_.make({
graphQLErrors: graphQLErrors,
graphQLErrors,
networkError: Js.Nullable.undefined,
errorMessage: errorMessage,
extraInfo: extraInfo,
errorMessage,
extraInfo,
})->fromJs

{...errorWithoutNetworkError, networkError: networkError}
{...errorWithoutNetworkError, networkError}
}
Loading

0 comments on commit 6e92f9b

Please sign in to comment.