FAST: ApolloMutationBehavior
ApolloMutationBehavior
extends ApolloMutationController
and implements the Behavior
interface.
Read the mutation component guides for examples and tips.
Live Demo
import type { Binding, ViewTemplate } from '@microsoft/fast-element';
import { FASTElement, customElement, html, when } from '@microsoft/fast-element';
import { ApolloMutationBehavior } from '@apollo-elements/fast';
import { AddUserMutation } from './AddUser.mutation.graphql.js';
import { client } from './client.js';
import { styles } from './add-user.css.js';
const format = x => { try { return new Date(x).toDateString(); } catch { return ''; } };
const name = 'add-user';
const getAddedUser: Binding<AddUser> = x => x.addUser.data?.insertUsers?.returning?.[0];
const getLoading: Binding<AddUser> = x => x.addUser.loading;
const getTimestamp: Binding<AddUser> = x => format(getAddedUser(x)?.timestamp);
const getUserName: Binding<AddUser> = x => getAddedUser(x)?.name ?? '';
const hasData: Binding<AddUser> = x => x.addUser.called || !!x.addUser.data;
const onClick: Binding<AddUser> = x => x.addUser.mutate();
const onInput: Binding<AddUser> = (x, { event }) => x.onInput(event);
const dataTemplate: ViewTemplate<AddUser> = html`
<dl>
<dt>Name</dt> <dd>${getUserName}</dd>
<dt>Added</dt> <dd>${getTimestamp}</dd>
</dl>`;
const template: ViewTemplate<AddUser> = html`
<fast-card>
<h2>Add User</h2>
${when(hasData, dataTemplate)}
<fast-text-field ?disabled="${getLoading}" @input="${onInput}">
User Name
</fast-text-field>
<fast-button ?disabled="${getLoading}" @click="${onClick}">
Add User
</fast-button>
</fast-card>
`;
@customElement({ name, template, styles })
class AddUser extends FASTElement {
addUser = new ApolloMutationBehavior(this, AddUserMutation, { client });
onInput(event: Event & { target: HTMLInputElement }): boolean {
const name = event.target.value
this.addUser.variables = { name }
return true;
}
}
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="https://unpkg.com/@microsoft/fast-components"></script>
<script type="module" src="AddUser.js"></script>
<fast-design-system-provider use-defaults>
<add-user></add-user>
</fast-design-system-provider>
body {
display: grid;
background-color: #111;
color: white;
font-family: "Open Sans", Arial, Helvetica, sans-serif;
place-items: center center;
height: 100vh;
}
fast-design-system-provider {
height: 100%;
width: 100%;
}
import { css} from '@microsoft/fast-element';
export const styles = css`
fast-card {
display: grid;
padding: 10px;
gap: 10px;
align-content: start;
}
fast-text-field {
width: auto;
}
`;
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client/core';
export const client = new ApolloClient({
cache: new InMemoryCache(),
link: new HttpLink({ uri: 'https://api.spacex.land/graphql' }),
});
ApolloMutationBehavior
🚀 FAST Behavior that connects to your Apollo cache.
Properties
variables
Variables<D, V> | null
loading
boolean
errors
array
error
null
data
Data<D> | null
Latest query data.
Methods
unbind
Parameters
_source
FASTElement
Returns
void
bind
Parameters
_source
FASTElement & HTMLElement
_context
ExecutionContext
Returns
void
Exports
import { ApolloMutationBehavior } from '@apollo-elements/fast/apollo-mutation-behavior';