initial push

This commit is contained in:
cupcakearmy 2021-12-31 09:39:03 +01:00
commit 2bd04341cf
No known key found for this signature in database
GPG Key ID: 3235314B4D31232F
16 changed files with 5775 additions and 0 deletions

8
.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
.DS_Store
node_modules
/build
/.svelte-kit
/package
.env
.env.*
!.env.example

1
.graphqlrc.yml Normal file
View File

@ -0,0 +1 @@
schema: https://api.spacex.land/graphql/

1
.npmrc Normal file
View File

@ -0,0 +1 @@
engine-strict=true

18
README.md Normal file
View File

@ -0,0 +1,18 @@
# A sane and efficient guide for consuming GraphQL endpoints in Typescript
This is the companion repo to the [blog post]() about GraphQL with Typescript.
This is skeleton [svelte kit](https://kit.svelte.dev/) app. The relevant files are:
- `.graphqlrc.yml`
- `codegen.yaml`
- `src/lib/gql`
## Running
```bash
pnpm i
pnpm run generate # Generate GraphQL stuff
pnpm run dev
```

14
codegen.yaml Normal file
View File

@ -0,0 +1,14 @@
schema: https://api.spacex.land/graphql/
documents: "src/**/*.graphql"
generates:
./src/lib/gql/gen.ts:
plugins:
- "@graphql-codegen/typescript"
- "@graphql-codegen/typescript-operations"
- "@graphql-codegen/typescript-graphql-request"
config:
maybeValue: "T"
typesPrefix: GQL
immutableTypes: true
useTypeImports: true
avoidOptionals: true

26
package.json Normal file
View File

@ -0,0 +1,26 @@
{
"name": "typescript-graphql",
"private": true,
"type": "module",
"scripts": {
"dev": "svelte-kit dev",
"build": "svelte-kit build",
"generate": "graphql-codegen"
},
"devDependencies": {
"@graphql-codegen/cli": "^2.3.1",
"@graphql-codegen/typescript": "^2.4.2",
"@graphql-codegen/typescript-graphql-request": "^4.3.3",
"@graphql-codegen/typescript-operations": "^2.2.2",
"@sveltejs/adapter-auto": "next",
"@sveltejs/kit": "next",
"graphql": "^15",
"graphql-request": "^3.7.0",
"graphql-tag": "^2.12.6",
"svelte": "^3.44.0",
"svelte-check": "^2.2.6",
"svelte-preprocess": "^4.9.4",
"tslib": "^2.3.1",
"typescript": "^4.4.3"
}
}

4179
pnpm-lock.yaml generated Normal file

File diff suppressed because it is too large Load Diff

21
src/app.html Normal file
View File

@ -0,0 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="description" content="" />
<link rel="icon" href="/favicon.png" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="https://unpkg.com/tachyons@4.12.0/css/tachyons.min.css" />
%svelte.head%
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans',
'Helvetica Neue', sans-serif;
}
</style>
</head>
<body>
<div id="svelte">%svelte.body%</div>
</body>
</html>

1
src/global.d.ts vendored Normal file
View File

@ -0,0 +1 @@
/// <reference types="@sveltejs/kit" />

1377
src/lib/gql/gen.ts Normal file

File diff suppressed because it is too large Load Diff

5
src/lib/gql/index.ts Normal file
View File

@ -0,0 +1,5 @@
import { GraphQLClient } from 'graphql-request'
import { getSdk } from './gen'
const client = new GraphQLClient('https://api.spacex.land/graphql/')
export const SDK = getSdk(client)

22
src/lib/gql/root.graphql Normal file
View File

@ -0,0 +1,22 @@
query LaunchpadsMany {
launchpads(limit: 10) {
id
name
location {
name
}
successful_launches
status
}
}
query LaunchByYear($year: String!) {
launches(find: { launch_year: $year }) {
mission_id
mission_name
launch_date_utc
rocket {
rocket_name
}
}
}

58
src/routes/index.svelte Normal file
View File

@ -0,0 +1,58 @@
<script lang="ts" context="module">
import { SDK } from '$lib/gql'
import type { Load } from '@sveltejs/kit'
export const load: Load = async () => ({
props: {
launchpads: await SDK.LaunchpadsMany(),
},
})
</script>
<script lang="ts">
import type { GQLLaunchpadsManyQuery } from '$lib/gql/gen'
export let launchpads: GQLLaunchpadsManyQuery
let needle: string = '2020'
$: launches = SDK.LaunchByYear({ year: needle })
</script>
<main>
<h1>SpaceX Land</h1>
<h2>Launchpads</h2>
{#each launchpads.launchpads as launchpad (launchpad.id)}
<div class="mb3">
<b>{launchpad.name}</b>
<br />
{launchpad.location.name}
<br /> <i>Launches:</i>
{launchpad.successful_launches}
</div>
{/each}
<h2>Launches</h2>
<input bind:value={needle} />
{#await launches}
<br />
Loading...
{:then launches}
{#each launches.launches as launch}
<div class="mb3">
<b>{launch.mission_name}</b>
<br />
<i>Rocket:</i>
{launch.rocket.rocket_name}
</div>
{/each}
{/await}
</main>
<style>
main {
padding: 1em;
max-width: 50rem;
margin: 0 auto;
}
</style>

BIN
static/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

13
svelte.config.js Normal file
View File

@ -0,0 +1,13 @@
import adapter from '@sveltejs/adapter-auto'
import preprocess from 'svelte-preprocess'
/** @type {import('@sveltejs/kit').Config} */
const config = {
preprocess: preprocess(),
kit: {
adapter: adapter(),
target: '#svelte',
},
}
export default config

31
tsconfig.json Normal file
View File

@ -0,0 +1,31 @@
{
"compilerOptions": {
"moduleResolution": "node",
"module": "es2020",
"lib": ["es2020", "DOM"],
"target": "es2020",
/**
svelte-preprocess cannot figure out whether you have a value or a type, so tell TypeScript
to enforce using \`import type\` instead of \`import\` for Types.
*/
"importsNotUsedAsValues": "error",
"isolatedModules": true,
"resolveJsonModule": true,
/**
To have warnings/errors of the Svelte compiler at the correct position,
enable source maps by default.
*/
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"allowJs": true,
"checkJs": true,
"paths": {
"$lib": ["src/lib"],
"$lib/*": ["src/lib/*"]
}
},
"include": ["src/**/*.d.ts", "src/**/*.js", "src/**/*.ts", "src/**/*.svelte"]
}