Created
May 23, 2019 11:58
-
-
Save praveenweb/0d94fecb4ee563b1b9367f895e3b3df2 to your computer and use it in GitHub Desktop.
Fetch Articles using GraphQL in Svelte
This file contains 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