Skip to content

Commit 8b9f2b6

Browse files
committed
Add schema linting
1 parent ed63b85 commit 8b9f2b6

File tree

9 files changed

+260
-1005
lines changed

9 files changed

+260
-1005
lines changed

Diff for: .eslintrc

-47
This file was deleted.

Diff for: .eslintrc.js

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const path = require("path")
2+
3+
module.exports = {
4+
parser: "@typescript-eslint/parser",
5+
plugins: ["@typescript-eslint", "graphql"],
6+
extends: [
7+
"eslint:recommended",
8+
"plugin:react/recommended",
9+
"plugin:@typescript-eslint/recommended",
10+
"prettier",
11+
],
12+
parserOptions: {
13+
ecmaFeatures: {
14+
jsx: true,
15+
},
16+
ecmaVersion: 6,
17+
project: "./tsconfig.json",
18+
sourceType: "module",
19+
},
20+
settings: {
21+
react: {
22+
version: "detect",
23+
},
24+
},
25+
env: {
26+
es6: true,
27+
browser: true,
28+
jest: true,
29+
node: true,
30+
},
31+
rules: {
32+
"@typescript-eslint/explicit-function-return-type": "off",
33+
"@typescript-eslint/explicit-member-accessibility": "off",
34+
"@typescript-eslint/indent": "off",
35+
"@typescript-eslint/member-delimiter-style": "off",
36+
"@typescript-eslint/no-explicit-any": "off",
37+
"@typescript-eslint/no-var-requires": "off",
38+
"@typescript-eslint/no-use-before-define": "off",
39+
"@typescript-eslint/no-unused-vars": [
40+
"error",
41+
{
42+
argsIgnorePattern: "^_",
43+
},
44+
],
45+
"graphql/template-strings": [
46+
"error",
47+
{
48+
env: "relay",
49+
schemaJsonFilepath: path.resolve(__dirname, "./schema.json"),
50+
tagName: "graphql",
51+
},
52+
],
53+
"no-console": [
54+
"error",
55+
{
56+
allow: ["warn", "error"],
57+
},
58+
],
59+
"react/display-name": 0,
60+
"react/prop-types": 0,
61+
},
62+
}

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ typings/
1010
# Gatsby files
1111
.cache/
1212
public
13+
schema.json

Diff for: README.md

+9
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ yarn build
3030

3131
![demo](https://user-images.githubusercontent.com/236943/51792565-abd93e00-2167-11e9-8bcb-87f7dccece52.gif)
3232

33+
### GraphQL Validation
34+
35+
Validation has been added via `eslint-plugin-graphql`:
36+
37+
<img width="512" alt="screen shot 2019-01-26 at 6 49 01 pm" src="https://user-images.githubusercontent.com/236943/51796580-8110d900-21ab-11e9-91a7-1cc6d6a068fc.png">
38+
39+
**TODO:** Figure out how to generate usable types for TypeScript with
40+
`apollo-cli`.
41+
3342
<details>
3443
<summary>Gatsby API Details</summary>
3544

Diff for: gatsby-node.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
* See: https://www.gatsbyjs.org/docs/node-apis/
77
*/
88

9-
const path = require("path")
9+
const write = require("write")
1010
const WebpackNotifierPlugin = require("webpack-notifier")
11-
const { createFilePath } = require("gatsby-source-filesystem")
11+
const path = require("path")
1212
const { toLower } = require("lodash")
13+
const { introspectionQuery, graphql, printSchema } = require("gatsby/graphql")
14+
const { createFilePath } = require("gatsby-source-filesystem")
1315

1416
// TODO: Fix apollo type generation
1517
// const WebpackShellPlugin = require("webpack-shell-plugin")
@@ -48,7 +50,7 @@ exports.createPages = ({ graphql, actions }) => {
4850
return new Promise((resolve, reject) => {
4951
resolve(
5052
graphql(`
51-
{
53+
query CreatePagesQuery {
5254
allMdx {
5355
edges {
5456
node {
@@ -114,3 +116,26 @@ exports.onCreateWebpackConfig = ({ actions }) => {
114116
},
115117
})
116118
}
119+
120+
/**
121+
* Generate GraphQL schema.json file to be read by tslint
122+
* Thanks: https://gist.github.com/kkemple/6169e8dc16369b7c01ad7408fc7917a9
123+
*/
124+
exports.onPostBootstrap = async ({ store }) => {
125+
try {
126+
const { schema } = store.getState()
127+
const jsonSchema = await graphql(schema, introspectionQuery)
128+
const sdlSchema = printSchema(schema)
129+
130+
write.sync("schema.json", JSON.stringify(jsonSchema.data), {})
131+
write.sync("schema.graphql", sdlSchema, {})
132+
133+
console.log("\n\n[gatsby-plugin-extract-schema] Wrote schema\n") // eslint-disable-line
134+
} catch (error) {
135+
console.error(
136+
"\n\n[gatsby-plugin-extract-schema] Failed to write schema: ",
137+
error,
138+
"\n"
139+
)
140+
}
141+
}

Diff for: package.json

+6-12
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,14 @@
66
"build": "gatsby build",
77
"clean": "rm -rf .cache && yarn start",
88
"develop": "nodemon --exec \"gatsby develop\"",
9-
"debug": "nodemon --exec \"node --inspect-brk --no-lazy node_modules/.bin/gatsby develop\" ",
10-
"emit-graphql-types": "apollo codegen:generate --tagName=graphql --target=typescript __generated__",
9+
"debug": "nodemon --exec \"node --inspect-brk --no-lazy node_modules/.bin/gatsby develop\"",
1110
"lint": "eslint . --ext ts --ext tsx",
1211
"prettier-write": "prettier --write \"src/**/*.js\"",
1312
"release": "auto shipit",
1413
"start": "yarn develop",
1514
"test": "echo \"Write tests! -> https://gatsby.app/unit-testing\"",
1615
"type-check": "tsc --pretty --noEmit"
1716
},
18-
"apollo": {
19-
"client": {
20-
"service": {
21-
"name": "gatsby",
22-
"url": "http://localhost:8000/___graphql"
23-
}
24-
}
25-
},
2617
"husky": {
2718
"hooks": {
2819
"pre-commit": "lint-staged",
@@ -40,6 +31,7 @@
4031
"ignore": [
4132
".cache/*",
4233
"public/*",
34+
"schema.json",
4335
"src/*"
4436
],
4537
"delay": "1500"
@@ -59,13 +51,14 @@
5951
"@types/rebass": "^0.3.4",
6052
"@types/styled-components": "^4.1.6",
6153
"@typescript-eslint/eslint-plugin": "^1.1.0",
62-
"apollo": "^2.1.9",
6354
"auto-release-cli": "2.3.0",
6455
"babel-eslint": "^10.0.1",
6556
"eslint": "^5.12.1",
6657
"eslint-config-prettier": "^3.6.0",
58+
"eslint-plugin-graphql": "^3.0.1",
6759
"eslint-plugin-react": "^7.12.4",
6860
"gatsby-plugin-typescript": "^2.0.3",
61+
"graphql-typescript-definitions": "^0.15.0",
6962
"husky": "^1.3.1",
7063
"jest": "^24.0.0",
7164
"lint-staged": "^8.1.0",
@@ -111,7 +104,8 @@
111104
"rebass": "^3.0.1",
112105
"styled-components": "^4.1.3",
113106
"styled-system": "^3.2.1",
114-
"unstated": "^2.1.1"
107+
"unstated": "^2.1.1",
108+
"write": "^1.0.3"
115109
},
116110
"license": "MIT",
117111
"publishConfig": {

Diff for: src/pages/index.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export default function Home() {
55
return (
66
<StaticQuery
77
query={graphql`
8-
query {
8+
query PagesQuery {
99
allMdx {
1010
edges {
1111
node {

Diff for: tsconfig.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
"allowSyntheticDefaultImports": true,
55
"baseUrl": "./src",
66
"jsx": "react",
7-
"lib": ["dom", "es2015", "es2016", "es2017"],
7+
"lib": ["dom", "esnext", "es2015", "es2016", "es2017"],
88
"moduleResolution": "node",
99
"noImplicitAny": false,
1010
"noUnusedLocals": true,
1111
"noUnusedParameters": true,
12-
"plugins": [{
13-
"name": "typescript-styled-plugin"
14-
}],
12+
"plugins": [
13+
{
14+
"name": "typescript-styled-plugin"
15+
}
16+
],
1517
"target": "es2015"
1618
},
1719
"include": ["**/*.ts", "**/*.tsx"],

0 commit comments

Comments
 (0)