Skip to content

Commit 82a4c89

Browse files
committed
Add example
1 parent 59a37e3 commit 82a4c89

File tree

10 files changed

+195
-7
lines changed

10 files changed

+195
-7
lines changed

.gitignore

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
/dist
2-
3-
/example/.sapper/
4-
/example/export/
5-
/example/build/
6-
/example/app/manifest/
2+
/example/public/bundle.*
73

84
node_modules
95
.vscode

example/public/global.css

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
html, body {
2+
position: relative;
3+
width: 100%;
4+
height: 100%;
5+
}
6+
7+
body {
8+
color: #333;
9+
margin: 0;
10+
padding: 8px;
11+
box-sizing: border-box;
12+
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
13+
}

example/public/index.html

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<meta charset='utf8'>
5+
<meta name='viewport' content='width=device-width'>
6+
7+
<title>Svelte app</title>
8+
9+
<link rel='stylesheet' href='global.css'>
10+
<link rel='stylesheet' href='bundle.css'>
11+
</head>
12+
13+
<body>
14+
<script src='bundle.js'></script>
15+
</body>
16+
</html>

example/rollup.config.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import svelte from 'rollup-plugin-svelte';
2+
import resolve from 'rollup-plugin-node-resolve';
3+
import commonjs from 'rollup-plugin-commonjs';
4+
import replace from 'rollup-plugin-replace';
5+
6+
export default {
7+
input: 'src/main.js',
8+
output: {
9+
sourcemap: true,
10+
format: 'iife',
11+
name: 'app',
12+
file: 'public/bundle.js',
13+
globals: {
14+
'apollo-boost': ''
15+
}
16+
},
17+
external: [],
18+
plugins: [
19+
svelte({
20+
dev: true,
21+
css: css => {
22+
css.write('public/bundle.css');
23+
}
24+
}),
25+
resolve(),
26+
commonjs(),
27+
replace({
28+
'process.env.NODE_ENV': JSON.stringify('development')
29+
})
30+
]
31+
};

example/server.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
const { GraphQLServer } = require('graphql-yoga');
2+
3+
// Source: https://thegreatestbooks.org/
4+
const books = [
5+
{ id: '1', title: 'In Search of Lost Time' },
6+
{ id: '2', title: 'Don Quixote' },
7+
{ id: '3', title: 'Ulysses' },
8+
{ id: '4', title: 'The Great Gatsby' },
9+
{ id: '5', title: 'Moby Dick' },
10+
{ id: '6', title: 'Hamlet' },
11+
{ id: '7', title: 'War and Peace' },
12+
{ id: '8', title: 'The Odyssey' },
13+
{ id: '9', title: 'One Hundred Years of Solitude' },
14+
{ id: '10', title: 'The Divine Comedy' }
15+
];
16+
17+
const typeDefs = `
18+
type Book {
19+
id: ID!
20+
title: String!
21+
}
22+
23+
type Query {
24+
books: [Book!]!
25+
}
26+
`;
27+
28+
const resolvers = {
29+
Query: {
30+
async books() {
31+
await latency();
32+
return books;
33+
}
34+
}
35+
};
36+
37+
const server = new GraphQLServer({ typeDefs, resolvers });
38+
server.start({ port: 4001, debug: true }, () => {
39+
console.log('GraphQL server listening on localhost:4001');
40+
});
41+
42+
async function latency(ms = 100) {
43+
return new Promise(resolve => {
44+
setTimeout(resolve, ms);
45+
});
46+
}

example/src/App.svelte

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<script>
2+
import { client } from './data';
3+
import { setClient } from '../../';
4+
import Books, { preload } from './Books.svelte';
5+
6+
// Approximate sapper preload
7+
const preloading = preload();
8+
9+
setClient(client);
10+
</script>
11+
12+
<section>
13+
<h2>Books</h2>
14+
15+
{#await preloading}
16+
<p>Preloading books....</p>
17+
{:then preloaded}
18+
<Books {...preloaded} />
19+
{:catch error}
20+
<p>Error preloading books: {error}</p>
21+
{/await}
22+
</section>

example/src/Books.svelte

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<script context="module">
2+
import { gql } from 'apollo-boost';
3+
import { client, BOOKS } from './data';
4+
5+
export async function preload() {
6+
return {
7+
cache: await client.query({ query: BOOKS })
8+
};
9+
}
10+
</script>
11+
<script>
12+
import { getClient, restore, query } from '../../';
13+
14+
export let cache;
15+
16+
const books = query(client, { query: BOOKS });
17+
</script>
18+
19+
<ul>
20+
{#await $books}
21+
<li>Loading...</li>
22+
{:then result}
23+
{#each result.data.books as book (book.id)}
24+
<li>{book.title}</li>
25+
{:else}
26+
<li>No books found</li>
27+
{/each}
28+
{:catch error}
29+
<li>Error loading books: {error}</li>
30+
{/await}
31+
</ul>

example/src/data.js

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import ApolloClient, { gql } from 'apollo-boost';
2+
3+
export const client = new ApolloClient({
4+
uri: 'http://localhost:4001'
5+
});
6+
7+
export const BOOKS = gql`
8+
{
9+
books {
10+
id
11+
title
12+
}
13+
}
14+
`;

example/src/main.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import App from './App.svelte';
2+
3+
const app = new App({
4+
target: document.body,
5+
props: {
6+
name: 'world'
7+
}
8+
});
9+
10+
export default app;

package.json

+11-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
"scripts": {
1616
"build": "rollup -c",
1717
"prepublishOnly": "npm run build",
18-
"example": "cd example && sapper dev",
1918
"test": "jest",
20-
"check": "tsc --noEmit"
19+
"typecheck": "tsc --noEmit",
20+
"example": "run-p example:*",
21+
"example:build": "cd example && rollup -c -w",
22+
"example:serve": "sirv example/public --dev",
23+
"example:server": "node example/server"
2124
},
2225
"dependencies": {
2326
"apollo-utilities": "^1.2.1",
@@ -31,18 +34,24 @@
3134
"devDependencies": {
3235
"@babel/plugin-transform-modules-commonjs": "^7.4.3",
3336
"@types/jest": "^24.0.11",
37+
"apollo-boost": "^0.3.1",
3438
"apollo-cache-inmemory": "^1.5.1",
3539
"apollo-client": "^2.5.1",
3640
"apollo-link": "^1.2.11",
3741
"apollo-link-state": "^0.4.2",
3842
"graphql": "^14.2.1",
43+
"graphql-yoga": "^1.17.4",
3944
"jest": "^24.7.1",
45+
"npm-run-all": "^4.1.5",
4046
"rollup": "^1.10.1",
4147
"rollup-plugin-commonjs": "^9.3.4",
4248
"rollup-plugin-dts": "^0.12.0",
4349
"rollup-plugin-filesize": "^5.0.1",
4450
"rollup-plugin-node-resolve": "^4.2.3",
51+
"rollup-plugin-replace": "^2.2.0",
52+
"rollup-plugin-svelte": "^5.0.3",
4553
"rollup-plugin-typescript": "^1.0.1",
54+
"sirv-cli": "^0.4.0",
4655
"svelte": "^3.0.0",
4756
"ts-jest": "^24.0.2",
4857
"tslib": "^1.9.3",

0 commit comments

Comments
 (0)