Docs
Launch GraphOS Studio
Since 3.8.0

Remove Typename Link

Automatically remove __typename fields from variables.


Overview

When reusing data from a as an to another , __typename can cause errors. To avoid this, you can use the removeTypenameFromVariables link to automatically remove __typename from in .

Remove __typename from all variables

As an example, take the following . automatically adds __typename 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 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 .

Usage

You can import and instantiate it like so:

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

Include removeTypeNameLink anywhere in your link chain before your terminating link to remove __typename from for all .

import { from } from '@apollo/client';
const link = from([removeTypenameLink, httpLink]);
const client = new ApolloClient({
link,
// ... other options
});

If you're using directional composition, for example, to send a subscription to a websocket connection, place removeTypenameLink before splitLink to remove __typename from for all .

import { from, split } from '@apollo/client';
import { removeTypenameFromVariables } from '@apollo/client/link/remove-typename';
const removeTypenameLink = removeTypenameFromVariables();
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
wsLink,
httpLink,
);
const link = from([removeTypenameLink, splitLink]);
const client = new ApolloClient({
link,
// ... other options
});

Keep __typename in JSON scalars

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

While the GraphQL type validation spec disallows input 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 .

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

For example, suppose your schema 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 for any declared as a JSON type. ( types are inferred from the .)

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 moves through the removeTypenameFromVariables link, the dashboardConfig will be detected as a JSON type and all __typename are kept intact.

Nested JSON scalar fields in input variables

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

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

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

This nesting can be as deep as needed and include as many 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 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 .

Previous
Persisted Queries
Next
REST
Edit on GitHubEditForumsDiscord

© 2024 Apollo Graph Inc.

Privacy Policy

Company