Skip to content

Commit c9aaa0c

Browse files
committed
initial
0 parents  commit c9aaa0c

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+18579
-0
lines changed

.eslintignore

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
node_modules
2+
dist
3+
public
4+
.cache
5+
*.log
6+
gatsby-types.d.ts
7+
dist
8+
9+
.pnp.*
10+
.yarn/*
11+
!.yarn/patches
12+
!.yarn/plugins
13+
!.yarn/releases
14+
!.yarn/sdks
15+
!.yarn/versions

.eslintrc.js

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/**
2+
* This config is used when you run `lint` or `lint:fix`.
3+
* It is basic, adjust as you see fit! You can of course use your own configuration.
4+
* @see https://eslint.org/
5+
*/
6+
module.exports = {
7+
env: {
8+
node: true,
9+
jest: true,
10+
},
11+
extends: [`eslint:recommended`, `plugin:@typescript-eslint/recommended`, `plugin:prettier/recommended`],
12+
parser: `@typescript-eslint/parser`,
13+
plugins: [`@typescript-eslint`, `prettier`],
14+
root: true,
15+
rules: {
16+
// Use backtick for quotes
17+
quotes: `off`,
18+
"@typescript-eslint/quotes": [
19+
2,
20+
`backtick`,
21+
{
22+
avoidEscape: true,
23+
},
24+
],
25+
// Do not use trailing comma
26+
"prettier/prettier": [
27+
`error`,
28+
{
29+
trailingComma: `es5`,
30+
semi: false,
31+
singleQuote: false,
32+
printWidth: 120,
33+
},
34+
],
35+
"@typescript-eslint/array-type": [`error`, { default: `generic` }],
36+
"@typescript-eslint/ban-types": [
37+
`error`,
38+
{
39+
extendDefaults: true,
40+
types: {
41+
"{}": {
42+
fixWith: `Record<string, unknown>`,
43+
},
44+
object: {
45+
fixWith: `Record<string, unknown>`,
46+
},
47+
},
48+
},
49+
],
50+
"@typescript-eslint/naming-convention": [
51+
`error`,
52+
{
53+
selector: `default`,
54+
format: [`camelCase`],
55+
leadingUnderscore: `allow`,
56+
trailingUnderscore: `allow`,
57+
},
58+
{
59+
selector: `variable`,
60+
format: [`camelCase`, `UPPER_CASE`],
61+
leadingUnderscore: `allow`,
62+
trailingUnderscore: `allow`,
63+
},
64+
{
65+
selector: `typeLike`,
66+
format: [`PascalCase`],
67+
},
68+
{
69+
selector: `parameter`,
70+
format: [`camelCase`],
71+
leadingUnderscore: `allow`,
72+
},
73+
{
74+
selector: `memberLike`,
75+
modifiers: [`private`],
76+
format: [`camelCase`],
77+
leadingUnderscore: `require`,
78+
},
79+
{
80+
selector: `objectLiteralProperty`,
81+
format: null,
82+
},
83+
{
84+
selector: `interface`,
85+
format: [`PascalCase`],
86+
prefix: [`I`],
87+
},
88+
],
89+
},
90+
overrides: [
91+
{
92+
files: [`site/src/pages/*.tsx`],
93+
rules: {
94+
"@typescript-eslint/naming-convention": 0,
95+
},
96+
},
97+
{
98+
files: [`plugin/src/plugin-options-schema.ts`],
99+
rules: {
100+
"@typescript-eslint/naming-convention": 0,
101+
},
102+
},
103+
],
104+
}

.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
node_modules
2+
dist
3+
public
4+
.cache
5+
*.log
6+
gatsby-types.d.ts
7+
8+
.pnp.*
9+
.yarn/*
10+
!.yarn/patches
11+
!.yarn/plugins
12+
!.yarn/releases
13+
!.yarn/sdks
14+
!.yarn/versions

.yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs

+541
Large diffs are not rendered by default.

.yarn/plugins/@yarnpkg/plugin-typescript.cjs

+9
Large diffs are not rendered by default.

.yarn/releases/yarn-3.3.1.cjs

+823
Large diffs are not rendered by default.

.yarnrc.yml

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
nodeLinker: node-modules
2+
3+
plugins:
4+
- path: .yarn/plugins/@yarnpkg/plugin-typescript.cjs
5+
spec: "@yarnpkg/plugin-typescript"
6+
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
7+
spec: "@yarnpkg/plugin-interactive-tools"
8+
9+
yarnPath: .yarn/releases/yarn-3.3.1.cjs

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2023 Gatsbyjs
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# creating-source-plugin-tutorial (WIP)
2+
3+
Companion project for the "Creating a Source Plugin" Gatsby tutorial.
4+
5+
## Prerequisites
6+
7+
You'll need to have these tools installed:
8+
9+
- Node.js (v18 or newer)
10+
- Git
11+
- Yarn
12+
13+
You can also follow [Part 0: Set Up Your Development Environment](https://www.gatsbyjs.com/docs/tutorial/part-0/) for more detailed instructions.
14+
15+
## Usage
16+
17+
1. Clone this project
18+
1. `yarn` to install dependencies
19+
1. `yarn test` to run unit tests
20+
1. `yarn lint:fix` to run linting
21+
22+
### Development
23+
24+
1. `yarn develop:deps` to build & serve the API at http://localhost:4000, and to also watch the source plugin for changes
25+
1. `yarn develop:site` in another terminal window to run `gatsby develop` for the test site
26+
27+
If you make changes to the source plugin you will need to restart the `site` server to see the changes reflected in the site.
28+
29+
### Build
30+
31+
1. `yarn start:api` to build and serve the API at http://localhost:4000
32+
1. `yarn build` in another terminal window to build the production plugin and site
33+
1. `yarn serve:site` to serve the Gatsby site at http://localhost:9000. You should see an overview of all posts
34+
35+
## Project structure
36+
37+
This project includes three directories:
38+
39+
- `api` is the example mock backend API you will source from
40+
- `plugin` is the example source plugin
41+
- `site` is the example site
42+
43+
The source plugin consumes the API, and the site uses the source plugin.

api/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# api
2+
3+
An example GraphQL API that is used for sourcing data.

api/package.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "api",
3+
"version": "0.1.0",
4+
"description": "Example API to source from",
5+
"main": "./dist/index.js",
6+
"scripts": {
7+
"clean": "del-cli dist",
8+
"build": "tsc",
9+
"develop": "tsc -w & nodemon -q -w dist dist/index.js",
10+
"serve": "node ./dist/index.js"
11+
},
12+
"license": "MIT",
13+
"dependencies": {
14+
"graphql": "^16.6.0",
15+
"graphql-yoga": "^3.3.0"
16+
},
17+
"devDependencies": {
18+
"del-cli": "^5.0.0",
19+
"nodemon": "^2.0.20",
20+
"typescript": "^4.9.4"
21+
}
22+
}

api/src/data.ts

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
export interface IAuthor {
2+
id: number
3+
name: string
4+
}
5+
6+
export const authors = [
7+
{
8+
id: 1,
9+
name: `Jay Gatsby`,
10+
},
11+
{
12+
id: 2,
13+
name: `Daisy Buchanan`,
14+
},
15+
] satisfies Array<IAuthor>
16+
17+
export const postIds: Array<number> = [1, 2, 3]
18+
19+
export interface IPostImage {
20+
url: string
21+
alt: string
22+
width: number
23+
height: number
24+
}
25+
26+
export interface IPost {
27+
id: number
28+
slug: string
29+
title: string
30+
image: IPostImage
31+
author: string
32+
}
33+
34+
export const posts = [
35+
{
36+
id: postIds[0],
37+
slug: `post-1`,
38+
title: `The first post`,
39+
image: {
40+
url: `https://images.unsplash.com/photo-1615751072497-5f5169febe17?fm=jpg`,
41+
alt: `brown and white long coated dog`,
42+
width: 3024,
43+
height: 4032,
44+
},
45+
author: `Jay Gatsby`,
46+
},
47+
{
48+
id: postIds[1],
49+
slug: `post-2`,
50+
title: `The second post`,
51+
image: {
52+
url: `https://images.unsplash.com/photo-1591160690555-5debfba289f0?fm=jpg`,
53+
alt: `golden retriever puppy on focus`,
54+
width: 5394,
55+
height: 6743,
56+
},
57+
author: `Jay Gatsby`,
58+
},
59+
{
60+
id: postIds[2],
61+
slug: `post-3`,
62+
title: `The third post`,
63+
image: {
64+
url: `https://images.unsplash.com/photo-1547525623-c7d42c20284c?fm=jpg`,
65+
alt: `long fur white dog on grass`,
66+
width: 4000,
67+
height: 6000,
68+
},
69+
author: `Daisy Buchanan`,
70+
},
71+
] satisfies Array<IPost>

api/src/index.ts

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import fs from "fs"
2+
import path from "path"
3+
import { createSchema, createYoga } from "graphql-yoga"
4+
import { createServer } from "node:http"
5+
import { authors, posts, postIds, IAuthor, IPost } from "./data"
6+
7+
const typeDefsPath = path.resolve(__dirname, `../src/schema.graphql`)
8+
const typeDefs = fs.readFileSync(typeDefsPath, `utf-8`)
9+
10+
export const schema = createSchema({
11+
typeDefs,
12+
resolvers: {
13+
Query: {
14+
authors: () => authors,
15+
posts: () => posts,
16+
},
17+
Author: {
18+
id: (parent: IAuthor) => parent.id,
19+
name: (parent: IAuthor) => parent.name,
20+
},
21+
Post: {
22+
id: (parent: IPost) => parent.id,
23+
slug: (parent: IPost) => parent.slug,
24+
title: (parent: IPost) => parent.title,
25+
author: (parent: IPost) => parent.author,
26+
},
27+
Mutation: {
28+
createPost: (_, { slug, title }) => {
29+
const post = {
30+
id: postIds.length + 1,
31+
slug,
32+
title,
33+
image: {
34+
url: `https://images.unsplash.com/photo-1615751072497-5f5169febe17?fm=jpg`,
35+
alt: `brown and white long coated dog`,
36+
width: 3024,
37+
height: 4032,
38+
},
39+
author: `Jay Gatsby`,
40+
}
41+
posts.push(post)
42+
return post
43+
},
44+
updatePost: (_, { id, title }) => {
45+
const postIndex = posts.findIndex((post) => id === post.id)
46+
47+
if (postIndex === null) {
48+
return null
49+
}
50+
posts[postIndex] = { ...posts[postIndex], title }
51+
52+
return posts[postIndex]
53+
},
54+
deletePost: (_, { id }) => {
55+
const postIndex = posts.findIndex((post) => id === post.id)
56+
57+
if (postIndex === null) {
58+
return null
59+
}
60+
const post = posts[postIndex]
61+
posts.splice(postIndex, 1)
62+
63+
return post
64+
},
65+
},
66+
},
67+
})
68+
69+
const yoga = createYoga({ schema })
70+
const server = createServer(yoga)
71+
72+
server.listen(4000, () => {
73+
console.info(`Server is running at http://localhost:4000/graphql`)
74+
})

0 commit comments

Comments
 (0)