Docs
Launch GraphOS Studio
Since 3.8.0

Remove Typename Link

Automatically remove __typename fields from variables.


Overview

The removeTypenameFromVariables link automatically removes __typename s from s in s. This is useful when reusing data from a query as an to another GraphQL .

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

As an example, let's take the following query. Apollo Client will automatically add __typename s to the query for each 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 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

While the GraphQL spec disallows input s that begin with the characters "__" (two underscores), this restriction is not applied when the input is a JSON (i.e. a type that accepts raw JSON as input). When your server relies on __typename within certain JSON s, you can configure the removeTypenameFromVariables link to retain __typename.

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

Provide an except option to map all types that should maintain __typename. This is done using the KEEP sentinel. Each key in the except option should correspond to an input type in your GraphQL .

Here we tell the removeTypenameFromVariables link to keep all __typename s for any that is declared as a JSON type. types are inferred from the GraphQL query. Let's take the following GraphQL :

mutation ConfigureDashboardMutation($dashboardConfig: JSON) {
configureDashboard(config: $dashboardConfig) {
id
}
}
import { removeTypenameFromVariables, KEEP } from '@apollo/client/link/remove-typename';
const removeTypenameLink = removeTypenameFromVariables({
except: {
JSON: KEEP
}
});

Here we've declared a dashboardConfig as type 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 may be found on a nested . The except option allows you to configured nested s within these types to keep __typename intact for these s.

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 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