FAST: ApolloMutation
ApolloMutation
inherits from ApolloElement
and implements the ApolloMutationInterface
.
Read the mutation component guides for examples and tips.
Exports
import { ApolloMutation } from '@apollo-elements/fast/apollo-mutation';
Properties
awaitRefetchQueries
(optional) inherited from ApolloMutationInterface await-refetch-queriesboolean
Queries refetched as part of refetchQueries
are handled asynchronously, and are not waited on before the mutation is completed (resolved). Setting this to true will make sure refetched queries are completed before the mutation is considered done. false by default.
called
inherited from ApolloMutationInterfaceboolean
Whether the mutation was called
client
inherited from ApolloElementInterfaceApolloClient<NormalizedCacheObject> | null
The Apollo Client instance
Defaults to window.__APOLLO_CLIENT__
, which is set by default when creating an Apollo Client. That means that in most cases, it's not necessary to explicity set the client
property. For more information see Getting Started: Apollo Client.
context
(optional) inherited from ApolloElementInterfaceRecord<string, unknown>
Context passed to the link execution chain.
data
inherited from ApolloElementInterfaceData<D> | null
Latest mutation data.
document
inherited from ApolloElementInterfaceDocumentNode | TypedDocumentNode | null
A GraphQL document containing a single query, mutation, or subscription. You can set it as a JavaScript property or by appending a GraphQL script to the element (light DOM).
documentType
inherited from ApolloElementInterface'mutation'
Hint that this is a mutation component
error
(optional) inherited from ApolloElementInterfaceError | ApolloError | null
Latest error
errorPolicy
(optional) inherited from ApolloElementInterface error-policyErrorPolicy
Specifies the Error Policy to be used for this mutation.
Much like fetchPolicy
, errorPolicy
allows you to control how GraphQL Errors from the server are sent to your UI code. By default, the error policy treats any GraphQL Errors as network errors and ends the request chain. It doesn't save any data in the cache, and renders your UI with the error property set to an ApolloError
. By changing this policy per request, you can adjust how GraphQL Errors are managed by your UI. The possible options for errorPolicy
are:
none
: This is the default policy to match how Apollo Client 1.0 worked. Any GraphQL Errors are treated the same as network errors and any data is ignored from the response.ignore
: Ignore allows you to read any data that is returned alongside GraphQL Errors, but doesn't save the errors or report them to your UI.all
: Using the all policy is the best way to notify your users of potential issues while still showing as much data as possible from your server. It saves both data and errors so your UI can use them.
errors
(optional) inherited from ApolloElementInterfacereadonly GraphQLError[] | null
Latest errors.
fetchPolicy
(optional) inherited from ApolloMutationInterface fetch-policyExtract<FetchPolicy, 'no-cache'>
Specifies the FetchPolicy to be used for this mutation.
ignoreResults
(optional) inherited from ApolloMutationInterfaceboolean
If true, the returned data property will not update with the mutation result.
loading
inherited from ApolloElementInterfaceboolean
Whether a request is in-flight.
mutation
inherited from ApolloMutationInterfaceDocumentNode | TypedDocumentNode
A GraphQL document containing a single mutation. You can set it as a JavaScript property or by appending a GraphQL script to the element (light DOM).
<script type="application/graphql">
mutation { updateLastSeen }
</script>
optimisticResponse
inherited from ApolloMutationInterfaceData<D> | ((vars: Variables<D, V>) => Data<D>)
An object that represents the result of this mutation that will be optimistically stored before the server has actually returned a result.
This is most often used for optimistic UI, where we want to be able to see the result of a mutation immediately, and update the UI later if any errors appear.
element.optimisticResponse = ({ name }: HelloMutationVariables) => ({
__typename: 'Mutation',
hello: {
__typename: 'Greeting',
name,
},
});
refetchQueries
inherited from ApolloMutationInterface refetch-queriesRefetchQueriesType<D> | null
A list of query names which will be refetched once this mutation has returned. This is often used if you have a set of queries which may be affected by a mutation and will have to update. Rather than writing a mutation query reducer (i.e. updateQueries
) for this, you can refetch the queries that will be affected and achieve a consistent store once these queries return.
variables
inherited from ApolloElementInterfaceVariables<D, V> | null
An object that maps from the name of a variable as used in the mutation GraphQL document to that variable's value.
Methods
documentChanged
(optional) inherited from ApolloElementInterfaceLifecycle callback that reacts to changes in the GraphQL document.
Parameters
document
DocumentNode | TypedDocumentNode | null
The GraphQL document.
Returns
void
mutate
inherited from ApolloMutationInterfaceThis resolves a single mutation according to the options specified and returns a Promise which is either resolved with the resulting data or rejected with an error.
Parameters
params
Partial<MutationOptions<Data<D>, Variables<D, V>>>
Optional, partial mutation params
All named arguments to mutate default to the element's corresponding instance property. So you can call element.mutate()
without arguments and it will call using element.mutation
, element.variables
, etc. You can likewise override instance properties per-call by passing them in, e.g.
await element.mutate({
fetchPolicy: 'network-only'
variables: {
...element.variables,
name: 'overridden',
},
});
Property | Type | Description |
---|---|---|
awaitRefetchQueries |
|
See awaitRefetchQueries |
context |
|
See context |
errorPolicy |
|
See errorPolicy |
fetchPolicy |
|
See fetchPolicy |
mutation |
|
See mutation |
optimisticResponse |
|
See optimisticResponse |
refetchQueries |
|
See refetchQueries |
update |
|
See updater |
variables |
|
See variables |
Returns
Promise<FetchResult<Data<D>>>
A FetchResult
object containing the result of the mutation and some metadata.
Property | Type | Description |
---|---|---|
data |
|
The result of a successful execution of the mutation |
errors |
|
included when any errors occurred as a non-empty array |
extensions |
|
Reserved for adding non-standard properties |
context |
|
See context |
onCompleted
(optional) inherited from ApolloMutationInterfaceCallback for when a mutation is completed.
Parameters
data
Data<D>
Returns
void
onError
(optional) inherited from ApolloMutationInterfaceCallback for when an error occurs in mutation.
Parameters
error
Error
Returns
void
updater
(optional) inherited from ApolloMutationInterfaceThis function will be called twice over the lifecycle of a mutation. Once at the very beginning if an optimisticResponse
was provided. The writes created from the optimistic data will be rolled back before the second time this function is called which is when the mutation has succesfully resolved. At that point update will be called with the actual mutation result and those writes will not be rolled back.
Note that since this function is intended to be used to update the store, it cannot be used with a no-cache
fetch policy. If you're interested in performing some action after a mutation has completed, and you don't need to update the store, use the Promise returned from client.mutate
instead.
Parameters
cache
ApolloCache
Use the cache.writeQuery
, cache.writeFragment
, etc. methods to update the cache following the mutation.
result
FetchResult<Data<D>>
See mutate
Property | Type | Description |
---|---|---|
data |
|
The result of a successful execution of the mutation |
errors |
|
included when any errors occurred as a non-empty array |
extensions |
|
Reserved for adding non-standard properties |
context |
|
See context |
Returns
void
variablesChanged
(optional) inherited from ApolloElementInterfaceLifecycle callback that reacts to changes in the operation variables.
Parameters
variables
Variables<D, V> | null
The variables.
Returns
void
Events
Name | Type | Description |
---|---|---|
apollo-element-connected |
|
when the element connects to the DOM |
apollo-element-disconnected |
|
when the element disconnects from the DOM |
apollo-error |
|
when the mutation rejects |
apollo-mutation-result |
|
when the mutation resolves |