-
-
Save stephensmitchell/ed30d37370098944b93d0512363e6650 to your computer and use it in GitHub Desktop.
Fetch Articles using GraphQL in Svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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