Skip to content

Instantly share code, notes, and snippets.

@stephensmitchell
Forked from praveenweb/Articles.svelte
Created September 12, 2020 11:29
Show Gist options
  • Save stephensmitchell/ed30d37370098944b93d0512363e6650 to your computer and use it in GitHub Desktop.
Save stephensmitchell/ed30d37370098944b93d0512363e6650 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