FAST: ApolloQueryBehavior
ApolloQueryBehavior
extends ApolloQueryController
and implements the Behavior
interface.
Demo
import type { Binding, ViewTemplate } from '@microsoft/fast-element';
import { FASTElement, customElement, html, repeat } from '@microsoft/fast-element';
import { ApolloQueryBehavior } from '@apollo-elements/fast';
import { styles } from './launches.css.js';
import { LaunchesQuery, Launch } from './Launches.query.graphql.js';
import '@apollo-elements/components/apollo-client';
const name = 'spacex-launches';
const getLaunches: Binding<Launches> = x => Array.from(x.launches.data?.launchesPast ?? []);
const getMissionName: Binding<Launch> = x => x.mission_name ?? ''
const getPatch: Binding<Launches> = x => x.links?.mission_patch_small;
const onLimitChange: Binding<Launches> = (x, { event }) => x.onLimitChange(event);
const template: ViewTemplate<Launches> = html`
<fast-card>
<h2>Launches</h2>
<fast-number-field min=1 max=50 value=3 @change=${onLimitChange}}>
Limit
</fast-number-field>
<fast-divider></fast-divider>
<ol>${repeat(getLaunches, html`
<li>
<article>
<span>${getMissionName}</span>
<img :src="${getPatch}" alt="Badge" role="presentation"/>
</article>
</li>` as ViewTemplate<Launch>)}
</ol>
</fast-card>
`;
@customElement({ name, styles, template })
class Launches extends FASTElement {
launches = new ApolloQueryBehavior(this, LaunchesQuery, {
variables: { limit: 3 }
});
onLimitChange(event) {
this.launches.variables = {
limit: parseInt(event.target.value)
};
}
}
<script type="module" src="https://unpkg.com/@microsoft/fast-components"></script>
<fast-design-system-provider use-defaults>
<script type="module" src="launches.js"></script>
<apollo-client uri="https://api.spacex.land/graphql">
<spacex-launches></spacex-launches>
</apollo-client>
</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;
}
apollo-client,
fast-design-system-provider {
height: 100%;
}
import { css } from '@microsoft/fast-element';
export const styles = css`
:host {
--image-size: 40px;
}
li img {
height: var(--image-size);
width: auto;
}
li article {
height: var(--image-size);
display: flex;
justify-content: space-between;
}
fast-card {
display: grid;
align-content: start;
gap: 10px;
padding: 10px;
}
h2 {
margin: 0;
}
`;
import { gql, TypedDocumentNode } from '@apollo/client/core';
interface Launch {
mission_name: string;
links: {
mission_patch_small: string;
};
}
interface Data {
launchesPast: Launch[];
}
interface Variables {
limit: number;
}
export const LaunchesQuery: TypedDocumentNode<Data, Variables> = gql`
query LaunchesQuery($limit: Int) {
launchesPast(limit: $limit) {
id
mission_name
links { mission_patch_small }
}
}
`;
;
Read the query component guides for more examples and tips.
ApolloQueryBehavior
🚀 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 { ApolloQueryBehavior } from '@apollo-elements/fast/apollo-query-behavior';