Skip to content

Commit d872096

Browse files
authored
Handle aliases in graphql-flow. (#68)
## Summary: This adds a new option to the config for graphql-flow so that you can specify aliases to map to, to help resolve imports. I used the same format as Vite to keep things simple. This will make it so that we can support our `~/`-style imports. In the process I realized a number of things about this repo were broken, so I fixed them: - We weren't running tsc, so I fixed that, and then fixed a bunch of TS failures that were hidden. - The ESLint config was broken, so I fixed that (and then fixed all the failures that came up). - Prettier was broken (still expecting Flow), so I fixed that. - VS Code wasn't set up properly, so I fixed that. - The Github PR Workflow was broken, still expecting Flow/.js files, so I fixed that. Hopefully this helps things to run more smoothly! Issue: FEI-5745 ## Test plan: I ran `yarn tsc` and `yarn eslint src/**/*.ts` and they both passed. Author: jeresig Reviewers: jeresig, kevinb-khan Required Reviewers: Approved By: kevinb-khan Checks: ✅ Lint & Test (ubuntu-latest, 20.x) Pull Request URL: #68
1 parent a091fe4 commit d872096

28 files changed

+1284
-1214
lines changed

.changeset/shiny-fireants-yawn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@khanacademy/graphql-flow": minor
3+
---
4+
5+
Add alias resolution.

.eslintrc.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
module.exports = {
2-
extends: ['@khanacademy'],
32
root: true,
4-
parser: '@babel/eslint-parser',
5-
plugins: ['flowtype-errors'],
3+
plugins: ["prettier", "jest"],
4+
extends: ["eslint:recommended", "prettier"],
5+
parser: "@typescript-eslint/parser",
6+
parserOptions: {
7+
sourceType: "module",
8+
ecmaVersion: 2020,
9+
},
610
rules: {
7-
'prettier/prettier': ['error', {singleQuote: true}],
11+
"prettier/prettier": "error",
12+
"no-unused-vars": "off",
13+
"no-case-declarations": "off",
14+
},
15+
env: {
16+
es6: true,
17+
node: true,
18+
jest: true,
819
},
920
};

.github/workflows/changeset-release.yml

+3-5
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,10 @@ jobs:
3131
name: Release
3232
runs-on: ubuntu-latest
3333
steps:
34-
- uses: actions/checkout@v2
34+
- uses: actions/checkout@v4
35+
- uses: Khan/actions@shared-node-cache-v2
3536
with:
36-
fetch-depth: 0
37-
- uses: Khan/[email protected]
38-
with:
39-
node-version: 16.x
37+
node-version: 20.x
4038

4139
- name: Create Release Pull Request or Publish to npm
4240
id: changesets

.github/workflows/pr-checks.yml

+17-17
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,45 @@ jobs:
1414
strategy:
1515
matrix:
1616
os: [ubuntu-latest]
17-
node-version: [16.x]
17+
node-version: [20.x]
1818
steps:
19-
- uses: actions/checkout@v2
20-
- uses: Khan/actions@shared-node-cache-v0
19+
- uses: actions/checkout@v4
20+
- uses: Khan/actions@shared-node-cache-v2
2121
with:
2222
node-version: ${{ matrix.node-version }}
2323

2424
- name: Get All Changed Files
25-
uses: Khan/actions@get-changed-files-v1
25+
uses: Khan/actions@get-changed-files-v2
2626
id: changed
2727

28-
- id: js-files
29-
name: Find .js changed files
30-
uses: Khan/actions@filter-files-v0
28+
- id: ts-files
29+
name: Find .ts changed files
30+
uses: Khan/actions@filter-files-v1
3131
with:
3232
changed-files: ${{ steps.changed.outputs.files }}
33-
extensions: '.js'
33+
extensions: '.ts'
3434

35-
- name: Run Flow
36-
if: steps.js-files.outputs.filtered != '[]'
37-
run: yarn flow
35+
- name: Run TypeScript
36+
if: steps.ts-files.outputs.filtered != '[]'
37+
run: yarn tsc
3838

3939
- id: eslint-reset
40-
uses: Khan/actions@filter-files-v0
40+
uses: Khan/actions@filter-files-v1
4141
name: Files that would trigger a full eslint run
4242
with:
4343
changed-files: ${{ steps.changed.outputs.files }}
4444
files: '.eslintrc.js,package.json,.eslintignore'
4545

46-
- name: Eslint
46+
- name: ESlint
4747
uses: Khan/actions@full-or-limited-v0
4848
with:
4949
full-trigger: ${{ steps.eslint-reset.outputs.filtered }}
50-
full: yarn eslint
51-
limited-trigger: ${{ steps.js-files.outputs.filtered }}
50+
full: yarn eslint src/**/*.ts
51+
limited-trigger: ${{ steps.ts-files.outputs.filtered }}
5252
limited: yarn eslint {}
5353

5454
- id: jest-reset
55-
uses: Khan/actions@filter-files-v0
55+
uses: Khan/actions@filter-files-v1
5656
name: Files that would trigger a full jest run
5757
with:
5858
changed-files: ${{ steps.changed.outputs.files }}
@@ -63,5 +63,5 @@ jobs:
6363
with:
6464
full-trigger: ${{ steps.jest-reset.outputs.filtered }}
6565
full: yarn jest
66-
limited-trigger: ${{ steps.js-files.outputs.filtered }}
66+
limited-trigger: ${{ steps.ts-files.outputs.filtered }}
6767
limited: yarn jest --findRelatedTests {}

.prettierrc

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"parser": "flow",
2+
"parser": "typescript",
33
"tabWidth": 4,
44
"trailingComma": "all",
55
"bracketSpacing": false,
6-
"singleQuote": true
6+
"singleQuote": false
77
}

.vscode/settings.json

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
3+
"editor.formatOnPaste": true,
4+
"editor.formatOnSave": true,
5+
"eslint.format.enable": true
6+
}

package.json

+11-12
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@
1717
"@babel/preset-env": "^7.16.11",
1818
"@babel/preset-typescript": "^7.22.5",
1919
"@changesets/cli": "^2.21.1",
20-
"@khanacademy/eslint-config": "^0.1.0",
20+
"@khanacademy/eslint-config": "^4.0.0",
2121
"@types/jest": "^29.5.3",
22+
"@types/prop-types": "^15.7.12",
23+
"@types/react": "^18.3.3",
24+
"@typescript-eslint/eslint-plugin": "^7.17.0",
25+
"@typescript-eslint/parser": "^7.17.0",
2226
"babel-jest": "23.4.2",
23-
"eslint": "8.7.0",
27+
"eslint": "^8.57.0",
2428
"eslint-config-prettier": "7.0.0",
25-
"eslint-plugin-flowtype": "^8.0.3",
26-
"eslint-plugin-flowtype-errors": "^4.5.0",
27-
"eslint-plugin-jsx-a11y": "^6.5.1",
29+
"eslint-plugin-jest": "^28.6.0",
2830
"eslint-plugin-prettier": "^4.0.0",
29-
"eslint-plugin-react": "^7.29.2",
30-
"eslint-plugin-react-hooks": "^4.3.0",
31-
"flow-bin": "^0.172.0",
3231
"graphql-tag": "2.10.1",
3332
"jest": "^27.5.1",
33+
"prettier": "^2.5.1",
34+
"prettier-eslint": "^13.0.0",
3435
"typescript": "^5.1.6"
3536
},
3637
"dependencies": {
@@ -40,9 +41,7 @@
4041
"@babel/types": "^7.17.0",
4142
"@khanacademy/wonder-stuff-core": "^1.5.1",
4243
"apollo-utilities": "^1.3.4",
43-
"graphql": "^16.3.0",
44-
"jsonschema": "^1.4.1",
45-
"prettier": "^2.5.1",
46-
"prettier-eslint": "^13.0.0"
44+
"graphql": "^16.9.0",
45+
"jsonschema": "^1.4.1"
4746
}
4847
}

src/__test__/generateTypeFileContents.test.ts

+26-25
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
// Test the generate the type file contents
2+
import {describe, it, expect} from "@jest/globals";
3+
import gql from "graphql-tag";
24

3-
import {getSchemas} from '../cli/config';
4-
import {generateTypeFileContents, indexPrelude} from '../generateTypeFiles';
5-
import gql from 'graphql-tag';
5+
import {getSchemas} from "../cli/config";
6+
import {generateTypeFileContents, indexPrelude} from "../generateTypeFiles";
67

7-
const [_, exampleSchema] = getSchemas(__dirname + '/example-schema.graphql');
8+
const [_, exampleSchema] = getSchemas(__dirname + "/example-schema.graphql");
89

9-
describe('generateTypeFileContents', () => {
10-
it('split types should export response & variables types', () => {
10+
describe("generateTypeFileContents", () => {
11+
it("split types should export response & variables types", () => {
1112
const {indexContents, files} = generateTypeFileContents(
12-
'hello.js',
13+
"hello.js",
1314
exampleSchema,
1415
gql`
1516
query Hello {
@@ -18,9 +19,9 @@ describe('generateTypeFileContents', () => {
1819
}
1920
}
2021
`,
21-
{splitTypes: true, schemaFilePath: ''},
22-
'__generated__',
23-
indexPrelude('yarn queries'),
22+
{splitTypes: true, schemaFilePath: ""},
23+
"__generated__",
24+
indexPrelude("yarn queries"),
2425
);
2526
expect(indexContents).toMatchInlineSnapshot(`
2627
"// AUTOGENERATED
@@ -35,7 +36,7 @@ describe('generateTypeFileContents', () => {
3536
expect(
3637
Object.keys(files)
3738
.map((k: any) => `// ${k}\n${files[k]}`)
38-
.join('\n\n'),
39+
.join("\n\n"),
3940
).toMatchInlineSnapshot(`
4041
"// __generated__/Hello.ts
4142
// AUTOGENERATED -- DO NOT EDIT
@@ -56,9 +57,9 @@ describe('generateTypeFileContents', () => {
5657
`);
5758
});
5859

59-
it('should respect the typeFileName option', () => {
60+
it("should respect the typeFileName option", () => {
6061
const {files} = generateTypeFileContents(
61-
'hello.js',
62+
"hello.js",
6263
exampleSchema,
6364
gql`
6465
query Hello {
@@ -69,16 +70,16 @@ describe('generateTypeFileContents', () => {
6970
`,
7071
{
7172
splitTypes: true,
72-
typeFileName: 'prefix-[operationName]-suffix.js',
73-
schemaFilePath: '',
73+
typeFileName: "prefix-[operationName]-suffix.js",
74+
schemaFilePath: "",
7475
},
75-
'__generated__',
76-
indexPrelude('yarn queries'),
76+
"__generated__",
77+
indexPrelude("yarn queries"),
7778
);
7879
expect(
7980
Object.keys(files)
8081
.map((k: any) => `// ${k}\n${files[k]}`)
81-
.join('\n\n'),
82+
.join("\n\n"),
8283
).toMatchInlineSnapshot(`
8384
"// __generated__/prefix-Hello-suffix.js
8485
// AUTOGENERATED -- DO NOT EDIT
@@ -99,10 +100,10 @@ describe('generateTypeFileContents', () => {
99100
`);
100101
});
101102

102-
describe('experimentalEnums', () => {
103-
it('should generate the expected values', () => {
103+
describe("experimentalEnums", () => {
104+
it("should generate the expected values", () => {
104105
const {files} = generateTypeFileContents(
105-
'hello.js',
106+
"hello.js",
106107
exampleSchema,
107108
gql`
108109
query Hello {
@@ -113,15 +114,15 @@ describe('generateTypeFileContents', () => {
113114
`,
114115
{
115116
experimentalEnums: true,
116-
schemaFilePath: '',
117+
schemaFilePath: "",
117118
},
118-
'__generated__',
119-
indexPrelude('yarn queries'),
119+
"__generated__",
120+
indexPrelude("yarn queries"),
120121
);
121122
expect(
122123
Object.keys(files)
123124
.map((k: any) => `// ${k}\n${files[k]}`)
124-
.join('\n\n'),
125+
.join("\n\n"),
125126
).toMatchInlineSnapshot(`
126127
"// __generated__/Hello.ts
127128
// AUTOGENERATED -- DO NOT EDIT

0 commit comments

Comments
 (0)