From 120a720be5f07797fb063d891c1a4aa3d9d02baa Mon Sep 17 00:00:00 2001 From: Ivan Li Date: Sat, 26 Jun 2021 23:30:09 +0800 Subject: [PATCH] feat: f**k the __typename :) --- src/commons/graphql/client.tsx | 11 +++++++++++ src/utils/deep-omit.ts | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 src/utils/deep-omit.ts diff --git a/src/commons/graphql/client.tsx b/src/commons/graphql/client.tsx index 8693e50..4707adb 100644 --- a/src/commons/graphql/client.tsx +++ b/src/commons/graphql/client.tsx @@ -15,6 +15,7 @@ import { onError } from "@apollo/client/link/error"; import { WebSocketLink } from "@apollo/client/link/ws"; import { getMainDefinition } from "@apollo/client/utilities"; import { useSnackbar } from "notistack"; +import { deepOmit } from "../../utils/deep-omit"; const schema = buildClientSchema( introspectionResult as unknown as IntrospectionQuery @@ -24,6 +25,15 @@ const typesMap = { DateTime: DateTimeResolver, }; +const cleanTypeName = new ApolloLink((operation, forward) => { + if (operation.variables) { + operation.variables = deepOmit(["__typename"], operation.variables); + } + return forward(operation).map((data) => { + return data; + }); +}); + export const FennecApolloClientProvider: FC = ({ children }) => { const { enqueueSnackbar } = useSnackbar(); const errorLink = onError(({ graphQLErrors, networkError }) => { @@ -71,6 +81,7 @@ export const FennecApolloClientProvider: FC = ({ children }) => { const link = ApolloLink.from([ errorLink, withScalars({ schema, typesMap }) as unknown as ApolloLink, + cleanTypeName, splitLink, ]); const client = new ApolloClient({ diff --git a/src/utils/deep-omit.ts b/src/utils/deep-omit.ts new file mode 100644 index 0000000..e1a416e --- /dev/null +++ b/src/utils/deep-omit.ts @@ -0,0 +1,22 @@ +import { fromPairs, map, omit, pipe, toPairs, type } from "ramda"; + +export const deepOmit = ( + names: readonly string[], + value: K +): T => { + switch (type(value)) { + case "Array": + return (value as unknown as Array).map((item: any) => + deepOmit(names, item) + ) as unknown as T; + case "Object": + return pipe( + omit(names), + toPairs, + map(([key, val]) => [key, deepOmit(names, val)] as any), + fromPairs + )(value) as unknown as T; + default: + return value as unknown as T; + } +};