Apollo Elements Apollo Elements Guides API Blog Toggle darkmode

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;
  }
}

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-queries
RefetchQueriesType<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-queries
boolean

Exports

import { ApolloMutation } from '@apollo-elements/fast/apollo-mutation';