Join us for GraphQL Summit, October 10-12 in San Diego. Use promo code ODYSSEY for $400 off your pass.
Docs
Launch GraphOS Studio
Since 3.8.0

Remove Typename Link

Automatically remove __typename fields from variables.


Overview

When reusing data from a query as an to another GraphQL , __typename s can cause errors. To avoid this, you can use the removeTypenameFromVariables link to automatically remove __typename s from s in s. You can import and instantiate it like so:

import { removeTypenameFromVariables } from '@apollo/client/link/remove-typename';
const removeTypenameLink = removeTypenameFromVariables();

Remove __typename from all variables

As an example, take the following query. Apollo Client automatically adds __typename s for each field selection set.

const query = gql`
query DashboardQuery($id: ID!) {
dashboard(id: $id) {
id
name
}
}
`;
const { data } = await client.query({ query, variables: { id: 1 }});
// {
// "dashboard": {
// "__typename": "Dashboard",
// "id": 1,
// "name": "My Dashboard"
// }
// }

Now let's update this dashboard by sending a to our server. We'll use the dashboard returned from the previous query as input to our .

const mutation = gql`
mutation UpdateDashboardMutation($dashboard: DashboardInput!) {
updateDashboard(dashboard: $dashboard) {
id
name
}
}
`;
await client.mutate({
mutation,
variables: {
dashboard: { ...data.dashboard, name: 'My Updated Dashboard' }
}
});

Without the use of the removeTypenameFromVariables link, the server will return an error because data.dashboard still contains the __typename .

Keep __typename in JSON scalars

Sometimes, you may need to retain the __typename from a query's response—for example, in the case of JSON scalar input s.

While the GraphQL type validation spec disallows input s that begin with two underscores (__), this restriction doesn't apply when the input is a JSON scalar. (A JSON type accepts raw JSON as input.) You can configure the removeTypenameFromVariables link to retain __typename for certain JSON s.

To do so, provide an except option when instantiating removeTypenameFromVariables and use the KEEP sentinel to denote which s types should keep __typename. Each key in the except option should correspond to an input type in your GraphQL .

For example, suppose your includes a ConfigureDashboardMutation that takes a JSON type named $dashboardConfig:

mutation ConfigureDashboardMutation($dashboardConfig: JSON) {
configureDashboard(config: $dashboardConfig) {
id
}
}

You can tell the removeTypenameFromVariables link to keep all __typename s for any declared as a JSON type. ( types are inferred from the GraphQL query.)

import { removeTypenameFromVariables, KEEP } from '@apollo/client/link/remove-typename';
const removeTypenameLink = removeTypenameFromVariables({
except: {
JSON: KEEP
}
});

Note: the JSON type does not need to be literally named JSON to be considered a JSON .

When the query moves through the removeTypenameFromVariables link, the dashboardConfig will be detected as a JSON type and all __typename s are kept intact.

Nested JSON scalar fields in input variables

Not all top-level s may map to a JSON type. For more complex input s, the JSON scalar may be found on a nested . The except option lets you configure nested s within these types to keep __typename intact.

import { removeTypenameFromVariables, KEEP } from '@apollo/client/link/remove-typename';
const removeTypenameLink = removeTypenameFromVariables({
except: {
DashboardInput: {
config: KEEP
}
}
});

s declared as type DashboardInput will have any top-level __typename s removed, but keep __typename for the config .

This nesting can be as deep as needed and include as many s as necessary. Use the KEEP sentinel to determine where __typename should be kept.

import { removeTypenameFromVariables, KEEP } from '@apollo/client/link/remove-typename';
const removeTypenameLink = removeTypenameFromVariables({
except: {
// Keep __typename for `bar` and `baz` fields on any variable
// declared as a `FooInput` type
FooInput: {
bar: KEEP,
baz: KEEP,
},
// Keep __typename for the `baz.qux` field on any variable
// declared as a `BarInput` type
BarInput: {
baz: {
qux: KEEP
}
},
// Keep __typename on `bar.baz` and `bar.qux.foo` fields for any
// variable declared as a `BazInput` type
BazInput: {
bar: {
baz: KEEP,
qux: {
foo: KEEP
}
}
},
}
});

To keep __typename for nested s in arrays, use the same object notation as if the field were an .

import { removeTypenameFromVariables, KEEP } from '@apollo/client/link/remove-typename';
const removeTypenameLink = removeTypenameFromVariables({
except: {
// Keep __typename on the `config` field for each widget
// in the `widgets` array for variables declared as
// a `DashboardInput` type
DashboardInput: {
widgets: {
config: KEEP
}
}
}
});

Options

Name /
Type
Description
except

KeepTypenameConfig

Determines which input types should retain __typename. This maps the input type to the config, which is either the KEEP sentinel or a nested config of s.

Previous
Persisted Queries
Next
REST
Edit on GitHubEditForumsDiscord