A lightweight and simple GraphQL client for Svelte 🔥
Just discovered someone already uses the name svql 😢
This started as a attempt to port the graphql-hooks library to svelte. A lot of ideas and code borrowed from that library but simplified.
<script context="module">
import { createClient, executeQuery } from "svelte-gql";
const client = createClient({
url: process.env.GRAPHQL_ENDPOINT,
headers: {
"x-hasura-admin-secret": process.env.HASURA_ACCESS_KEY
}
});
const user = executeQuery();
export async function preload() {
return { user };
}
</script>
<script>
import { setContext } from "svelte";
setContext("client", client);
const user_query = /* GraphQL */ `
query {
user {
name
}
}
`
user.query(client, { query: user_query });
</script>
<div>
{#if $user.fetching}
<div>loading...</div>
{/if}
{#if $user.data}
{#each $user.data.user as user}
<h1>{user.name}</h1>
{/each}
{/if}
</div>
query
function