Skip to content

Instantly share code, notes, and snippets.

@praveenweb
Created May 23, 2019 11:58
Show Gist options
  • Save praveenweb/0d94fecb4ee563b1b9367f895e3b3df2 to your computer and use it in GitHub Desktop.
Save praveenweb/0d94fecb4ee563b1b9367f895e3b3df2 to your computer and use it in GitHub Desktop.
Fetch Articles using GraphQL in Svelte
<script context="module">
import gql from 'graphql-tag';
import { client } from './apollo';
const ARTICLES = gql`
{
article {
id
title
author {
id
}
}
}
`;
export async function preload() {
return {
cache: await client.query({ query: ARTICLES })
};
}
</script>
<script>
import { restore, query } from 'svelte-apollo';
export let cache;
restore(client, ARTICLES, cache.data);
const articles = query(client, { query: ARTICLES });
</script>
<ul>
{#await $articles}
<li>Loading...</li>
{:then result}
{#each result.data.article as article (article.id)}
<li>{article.title}</li>
{:else}
<li>No articles found</li>
{/each}
{:catch error}
<li>Error loading articles: {error}</li>
{/await}
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment