Apollo useQuery
hook for web components.
Demo
import { useQuery, component, html } from '@apollo-elements/haunted';
import { LaunchesQuery } from './Launches.query.graphql.js';
import '@apollo-elements/components/apollo-client';
function Launches(hostElement) {
const { data } = useQuery(LaunchesQuery, {
hostElement,
variables: { limit: 3 }
});
const launches = data?.launchesPast ?? [];
return html`
<link rel="stylesheet" href="launches.css"/>
<ol>${launches.map(x => html`
<li>
<article>
<span>${x.mission_name}</span>
<img .src="${x.links.mission_patch_small}" alt="Badge" role="presentation"/>
</article>
</li>`)}
</ol>
`;
}
customElements.define('spacex-launches', component(Launches));
import { useQuery, component, html } from '@apollo-elements/haunted';
import { LaunchesQuery } from './Launches.query.graphql.js';
import '@apollo-elements/components/apollo-client';
function Launches(hostElement) {
const { data } = useQuery(LaunchesQuery, {
hostElement,
variables: { limit: 3 }
});
const launches = data?.launchesPast ?? [];
return html`
<link rel="stylesheet" href="launches.css"/>
<ol>${launches.map(x => html`
<li>
<article>
<span>${x.mission_name}</span>
<img .src="${x.links.mission_patch_small}" alt="Badge" role="presentation"/>
</article>
</li>`)}
</ol>
`;
}
customElements.define('spacex-launches', component(Launches));
<script type="module" src="launches.js"></script>
<apollo-client uri="https://main--spacex-l4uc6p.apollographos.net/graphql">
<spacex-launches></spacex-launches>
</apollo-client>
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 }
}
}
`;
:host {
--image-size: 40px;
}
li img {
height: var(--image-size);
width: auto;
}
li article {
height: var(--image-size);
display: flex;
justify-content: space-between;
}
Read the query component guides for examples and tips.
Exports
js useQuery
from useQuery.js