Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/kesne/svelte-relay

Easily use GraphQL in Svelte, powered by the production-ready Relay runtime.
https://github.com/kesne/svelte-relay

Last synced: about 2 months ago
JSON representation

Easily use GraphQL in Svelte, powered by the production-ready Relay runtime.

Awesome Lists containing this project

README

        


Svelte Relay Logo


Svelte Relay


Easily use GraphQL in Svelte, powered by the production-ready Relay runtime.

## Features

[Relay](https://relay.dev) bindings for [Svelte](https://svelte.dev).

## Example

### 1. Set the Relay Environment into the context

```svelte

import { setRelayEnvironment } from 'svelte-relay';
import myEnvironment from './environment';

setRelayEnvironment(myEnvironment);

```

### 2. Write Queries

```svelte

import { getQuery } from 'svelte-relay';
// This is defined in the next section below.
import Artist from './Artist';

const query = getQuery(
graphql`
query ArtistQuery($artistID: String!) {
# The root field for the query
artist(id: $artistID) {
# A reference to your fragment container
...ArtistHeader_artist
}
}
`,
{
artistID: 'some-id',
},
);

{#await $query}

Loading

{:then data}

{:catch error}
{error.message}

{/await}
```

### 3. Use Fragments

```svelte

import { getFragment } from 'svelte-relay';

export let artist;

const data = getFragment(
graphql`
# This fragment is declaring that this component
# needs an Artist, and these specific fields on
# the Artist in order to render. Relay will
# guarantee that this data is fetched and available
# for this component.
fragment ArtistHeader_artist on Artist {
href
bio
name
image {
url
}
}
`,
artist,
);




{data.name}


{data.bio}




```