Skip to content

Latest commit

 

History

History
279 lines (168 loc) · 4.96 KB

mutation-options.mdx

File metadata and controls

279 lines (168 loc) · 4.96 KB
Name /
Type
Description

Operation options

mutation

DocumentNode

A GraphQL query string parsed into an AST with the gql template literal.

Optional for the useMutation hook, because the mutation can also be provided as the first parameter to the hook.

Required for the Mutation component.

variables

{ [key: string]: any }

An object containing all of the GraphQL variables your mutation requires to execute.

Each key in the object corresponds to a variable name, and that key's value corresponds to the variable value.

errorPolicy

ErrorPolicy

Specifies how the mutation handles a response that returns both GraphQL errors and partial results.

For details, see GraphQL error policies.

The default value is none, meaning that the mutation result includes error details but not partial results.

onCompleted

(data: TData | {}) => void

A callback function that's called when your mutation successfully completes with zero errors (or if errorPolicy is ignore and partial data is returned).

This function is passed the mutation's result data.

onError

(error: ApolloError) => void

A callback function that's called when the mutation encounters one or more errors (unless errorPolicy is ignore).

This function is passed an ApolloError object that contains either a networkError object or a graphQLErrors array, depending on the error(s) that occurred.

onQueryUpdated

(observableQuery: ObservableQuery, diff: Cache.DiffResult, lastDiff: Cache.DiffResult | undefined) => boolean | TResult

Optional callback for intercepting queries whose cache data has been updated by the mutation, as well as any queries specified in the refetchQueries: [...] list passed to client.mutate.

Returning a Promise from onQueryUpdated will cause the final mutation Promise to await the returned Promise. Returning false causes the query to be ignored.

refetchQueries

Array<string | { query: DocumentNode, variables?: TVariables}> | ((mutationResult: FetchResult) => Array<string | { query: DocumentNode, variables?: TVariables}>)

An array (or a function that returns an array) that specifies which queries you want to refetch after the mutation occurs.

Each array value can be either:

  • An object containing the query to execute, along with any variables
  • A string indicating the operation name of the query to refetch
awaitRefetchQueries

boolean

If true, makes sure all queries included in refetchQueries are completed before the mutation is considered complete.

The default value is false (queries are refetched asynchronously).

ignoreResults

boolean

If true, the mutation's data property is not updated with the mutation's result.

The default value is false.

Networking options

notifyOnNetworkStatusChange

boolean

If true, the in-progress mutation's associated component re-renders whenever the network status changes or a network error occurs.

The default value is false.

client

ApolloClient

The instance of ApolloClient to use to execute the mutation.

By default, the instance that's passed down via context is used, but you can provide a different instance here.

context

Record<string, any>

If you're using Apollo Link, this object is the initial value of the context object that's passed along your link chain.

Caching options

update

(cache: ApolloCache, mutationResult: FetchResult) => void

A function used to update the Apollo Client cache after the mutation completes.

For more information, see Updating the cache after a mutation.

optimisticResponse

Object

If provided, Apollo Client caches this temporary (and potentially incorrect) response until the mutation completes, enabling more responsive UI updates.

For more information, see Optimistic mutation results.