From dc8d025822dbf14ef22e5068a9ce5869a9e45009 Mon Sep 17 00:00:00 2001 From: Max Ehrlicher-Schmidt Date: Wed, 23 Sep 2020 17:00:47 +0200 Subject: [PATCH] Add __typename removal for mutations --- src/app/graphql.module.ts | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/src/app/graphql.module.ts b/src/app/graphql.module.ts index be2adee..badafbb 100644 --- a/src/app/graphql.module.ts +++ b/src/app/graphql.module.ts @@ -1,13 +1,33 @@ -import {NgModule} from '@angular/core'; -import {APOLLO_OPTIONS} from 'apollo-angular'; -import {ApolloClientOptions, ApolloLink, InMemoryCache} from '@apollo/client/core'; -import {HttpLink} from 'apollo-angular/http'; +import { NgModule } from '@angular/core'; +import { APOLLO_OPTIONS } from 'apollo-angular'; +import { + ApolloClientOptions, + ApolloLink, + concat, + InMemoryCache, +} from '@apollo/client/core'; +import { HttpLink } from 'apollo-angular/http'; import { environment } from '../environments/environment'; const uri = environment.apiUrl + '/graphql'; // <-- add the URL of the GraphQL server here + +const cleanTypeName = new ApolloLink((operation, forward) => { + if (operation.variables) { + const omitTypename = (key: string, value: any) => + key === '__typename' ? undefined : value; + operation.variables = JSON.parse( + JSON.stringify(operation.variables), + omitTypename + ); + } + return forward(operation).map((data) => { + return data; + }); +}); + export function createApollo(httpLink: HttpLink): ApolloClientOptions { return { - link: httpLink.create({uri}), + link: concat(cleanTypeName, httpLink.create({ uri })), cache: new InMemoryCache({}), }; } @@ -21,5 +41,4 @@ export function createApollo(httpLink: HttpLink): ApolloClientOptions { }, ], }) -export class GraphQLModule { -} \ No newline at end of file +export class GraphQLModule {}