Haunted: useQuery
Apollo useQuery
hook for web components.
Read the query component guides for examples and tips.
import { gql } from '@apollo/client/core';
export const LaunchesQuery = gql`
query LaunchesQuery($limit: Int) {
launchesPast(limit: $limit) {
mission_name
links { mission_patch_small }
}
}
`;
<script type="module" src="client.js"></script>
<script type="module" src="launches.js"></script>
<spacex-launches></spacex-launches>
import { useQuery, component, html } from '@apollo-elements/haunted';
import { LaunchesQuery } from './Launches.query.graphql.js';
function Launches({ document = null, variables = null }) {
const { data } = useQuery(LaunchesQuery, { 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));
:host {
--image-size: 40px;
}
li img {
height: var(--image-size);
width: auto;
}
li article {
height: var(--image-size);
display: flex;
justify-content: space-between;
}
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' }),
});
useQuery
Parameters
query
ComponentDocument<D>
options
ApolloQueryControllerOptions<D, V>
All properties are optional
Property | Type | Description |
---|---|---|
fetchPolicy | WatchQueryFetchPolicy |
The fetchPolicy for the query. |
variables | Variables<D, V> |
Variables for the query. |
noAutoSubscribe | boolean |
If true, the element will not begin querying data until you manually call subscribe |
shouldSubscribe | (op?: Partial<Operation<D, V>) => boolean |
If true, the element will not begin querying data until you manually call subscribe |
onData | (data: Data<D>) => void |
Optional callback for when a query resolves. |
onError | (error: Error) => void |
Optional callback for when an error occurs. |
Returns
ApolloQueryController<D, V>
Exports
import { useQuery } from '@apollo-elements/haunted/useQuery';