feat: f**k the __typename :)

This commit is contained in:
Ivan Li 2021-06-26 23:30:09 +08:00
parent dee2c97ba4
commit 120a720be5
2 changed files with 33 additions and 0 deletions

View File

@ -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({

22
src/utils/deep-omit.ts Normal file
View File

@ -0,0 +1,22 @@
import { fromPairs, map, omit, pipe, toPairs, type } from "ramda";
export const deepOmit = <T = any, K = any>(
names: readonly string[],
value: K
): T => {
switch (type(value)) {
case "Array":
return (value as unknown as Array<any>).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;
}
};