ApolloMutationController
modifies data on your GraphQL server. Pass it a GraphQL mutation document, and any options you choose, and when you call its mutate()
method, it will issue the mutation. It then updates its host when it’s state (e.g. data
, error
, or loading
) changes.
The (optional) third argument to the constructor is an ApolloMutationControllerOptions
object, which is the same as the MutationOptions
parameter to mutate()
, as well as onComplete
/onError
callbacks to run your side-effects, if you choose.
Apollo Elements controllers are not limited to Lit. Use them with any object that implements the ReactiveControllerHost
interface. See ControllerHostMixin
for an example.
import '@apollo-elements/components/apollo-client';
import { ApolloMutationController } from '@apollo-elements/core';
import { customElement, state, query } from 'lit/decorators.js';
import { css, html, LitElement } from 'lit';
import { AddUserMutation } from './AddUser.mutation.graphql.js';
import '@material/mwc-button';
import '@material/mwc-textfield';
@customElement('add-user')
class AddUser extends LitElement {
addUser = new ApolloMutationController(this, AddUserMutation);
render() {
return html`
<mwc-textfield label="Add User" value=${this.addUser.data?.addUser?.name}></mwc-textfield>
<mwc-button label="Add" @click="${this.mutate}"></mwc-button>
<p ?hidden="${!this.addUser.data}">${this.addUser.data?.addUser?.name} added!</p>
`;
}
mutate(event) {
const name = this.shadowRoot.querySelector('mwc-textfield').value;
this.addUser.mutate({ variables: { name } });
}
}
Properties
mutation
The GraphQL mutation documentmostRecentMutationId
number
called
boolean
data
Data<D> | null
null
.error
Error | null
null
.errors
readonly GraphQLError[]
[]
.loading
boolean
options
ApolloControllerOptions<D, V>
Option | Type | Description |
---|---|---|
client | ApolloClient<NormalizedCacheObject> |
The ApolloClient instance for the controller. |
variables | Variables<D, V> |
Variables for the operation. |
context | any |
Context passed to the link execution chain. |
errorPolicy | ErrorPolicy |
the error policy for the operation |
hostElement | HTMLElement |
When provided, the controller will fall back to this element to fire events |
client
ApolloClient | null
ApolloClient
instance for this controller.document
ComponentDocument<D, V> | null
variables
Variables<D, V> | null
Methods
mutate
asyncFires a mutation This 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
generateMutationId
Increments and returns the most recent mutation id.isMostRecentMutation
Returns true when an ID matches the most recent mutation id.Parameters
mutationId
onCompletedMutation
Callback for when a mutation is completed.Parameters
mutationId
response
onMutationError
Callback for when a mutation fails.Parameters
mutationId
apolloError
hostConnected
Returns
void
hostDisconnected
Returns
void
Events
apollo-controller-connected
ApolloControllerConnectedEvent
The controller’s host connected to the DOM.apollo-controller-disconnected
ApolloControllerDisconnectedEvent
The controller’s host disconnected from the DOM.Private Properties
#options
privateApolloControllerOptions<D, V>
Option | Type | Description |
---|---|---|
client | ApolloClient<NormalizedCacheObject> |
The ApolloClient instance for the controller. |
variables | Variables<D, V> |
Variables for the operation. |
context | any |
Context passed to the link execution chain. |
errorPolicy | ErrorPolicy |
the error policy for the operation |
hostElement | HTMLElement |
When provided, the controller will fall back to this element to fire events |
#client
privateApolloClient | null
#document
privateComponentDocument<D, V> | null
emitter
protectedEventTarget
Private Methods
notify
protectedrequests an update on the host with the provided properties.Parameters
properties
Record<string, unknown>
Returns
void
documentChanged
protectedcallback for when the GraphQL document changes.Parameters
document
ComponentDocument<D, V> | null
Returns
void
variablesChanged
protectedcallback for when the GraphQL variables change.Parameters
variables
Variables<D, V> | null
Returns
void
clientChanged
protectedcallback for when the Apollo client changes.Parameters
client
ApolloClient | null
Returns
void
optionsChanged
protectedcallback for when the options change.Parameters
options
ApolloControllerOptions<D, V>
Option | Type | Description |
---|---|---|
client | ApolloClient<NormalizedCacheObject> |
The ApolloClient instance for the controller. |
variables | Variables<D, V> |
Variables for the operation. |
context | any |
Context passed to the link execution chain. |
errorPolicy | ErrorPolicy |
the error policy for the operation |
hostElement | HTMLElement |
When provided, the controller will fall back to this element to fire events |
Returns
void
init
protectedAssigns the controller’s variables and GraphQL document.Parameters
document
ComponentDocument<D, V> | null
Returns
void
Exports
js ApolloMutationController
from apollo-mutation-controller.js