FAST: ApolloMutation
ApolloMutation
inherits from ApolloElement
and implements the ApolloMutationInterface
.
Read the mutation component guides for examples and tips.
Live Demo
import { ApolloMutation, Binding, customElement, html } from '@apollo-elements/fast';
import { AddUserMutation } from './AddUser.mutation.graphql.js';
import { compose, propOr } from './fp-helpers.js';
import '@power-elements/card';
import '@material/mwc-button';
import '@material/mwc-textfield';
const getAddedUser: Binding<AddUser> = x => x.data?.insertUsers?.returning?.[0];
const format = x => { try { return new Date(x).toDateString(); } catch { return ''; } };
@customElement({ name: 'add-user', template: html<AddUser>`
<p-card>
<h2 slot="heading">Add User</h2>
${x => (!x.called || !x.data) ? '' : html<AddUser>`
<dl>
<dt>Name</dt> <dd>${compose(propOr('name', ''), getAddedUser)}</dd>
<dt>Added</dt> <dd>${compose(format, propOr('timestamp', null), getAddedUser)}</dd>
</dl>
`}
<mwc-textfield slot="actions"
label="User Name"
outlined
?disabled="${propOr('loading')}"
@input="${(x, { event }) => x.onInput(event.target.value)}"></mwc-textfield>
<mwc-button slot="actions"
label="Add User"
?disabled="${propOr('loading')}"
@click="${x => x.mutate()}"></mwc-button>
</p-card>
` })
class AddUser extends ApolloMutation<typeof AddUserMutation> {
mutation = AddUserMutation;
onInput(name): boolean {
this.variables = { name }
return true;
}
}
/** right-to-left function composition */
export const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
/** safe property access */
export const propOr = (name, or) => o => o?.[name] ?? or;
import { gql, TypedDocumentNode } from '@apollo/client/core';
interface Data {
insertUsers: {
returning: {
name: string;
id: string;
timestamp: string;
}
}
}
export const AddUserMutation: TypedDocumentNode<Data, { name: string }> = gql`
mutation InsertUser($name: String!) {
insertUsers: insert_users(objects: {name: $name}) {
returning {
name
id
timestamp
}
}
}
`;
<script type="module" src="client.js"></script>
<script type="module" src="AddUser.js"></script>
<add-user></add-user>
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client/core';
window.__APOLLO_CLIENT__ = new ApolloClient({
cache: new InMemoryCache(),
link: new HttpLink({ uri: 'https://api.spacex.land/graphql' }),
});
ApolloMutation
👩🚀 Custom element base class to issue mutations via your Apollo cache.
See ApolloMutationInterface
for more information on events
Properties
variables
Variables<D, V> | null
Mutation variables.
An object that maps from the name of a variable as used in the mutation GraphQL document to that variable's value.
refetchQueries
refetch-queriesRefetchQueriesType<D> | null
As an attribute, can be a string of comma-separated query names
<mutation-element refetch-queries="QueryA, QueryB,QueryC"></mutation-element>
As a property, you can pass any legal refetchQueries
value.
fetchPolicy
fetch-policy'no-cache'
data
Data<D> | null
Latest mutation data.
called
boolean
awaitRefetchQueries
await-refetch-queriesboolean
Exports
import { ApolloMutation } from '@apollo-elements/fast/apollo-mutation';