@khanacademy/graphql-flow 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.babelrc +6 -0
- package/.changeset/README.md +8 -0
- package/.changeset/config.json +11 -0
- package/.eslintignore +2 -0
- package/.eslintrc.js +10 -0
- package/.flowconfig +13 -0
- package/.github/actions/filter-files/action.yml +37 -0
- package/.github/actions/full-or-limited/action.yml +27 -0
- package/.github/actions/json-args/action.yml +32 -0
- package/.github/actions/setup/action.yml +28 -0
- package/.github/workflows/changeset-release.yml +80 -0
- package/.github/workflows/pr-checks.yml +64 -0
- package/.prettierrc +7 -0
- package/CHANGELOG.md +14 -0
- package/Readme.md +172 -0
- package/build-copy-source.js +28 -0
- package/dist/enums.js +57 -0
- package/dist/enums.js.flow +69 -0
- package/dist/generateResponseType.js +267 -0
- package/dist/generateResponseType.js.flow +419 -0
- package/dist/generateVariablesType.js +132 -0
- package/dist/generateVariablesType.js.flow +153 -0
- package/dist/index.js +88 -0
- package/dist/index.js.flow +93 -0
- package/dist/jest-mock-graphql-tag.js +169 -0
- package/dist/jest-mock-graphql-tag.js.flow +191 -0
- package/dist/schemaFromIntrospectionData.js +69 -0
- package/dist/schemaFromIntrospectionData.js.flow +68 -0
- package/dist/types.js +1 -0
- package/dist/types.js.flow +54 -0
- package/dist/utils.js +53 -0
- package/dist/utils.js.flow +50 -0
- package/flow-typed/npm/@babel/types_vx.x.x.js +5317 -0
- package/flow-typed/npm/jest_v23.x.x.js +1155 -0
- package/flow-typed/overrides.js +435 -0
- package/package.json +41 -0
- package/src/__test__/example-schema.graphql +65 -0
- package/src/__test__/graphql-flow.test.js +364 -0
- package/src/__test__/jest-mock-graphql-tag.test.js +51 -0
- package/src/enums.js +69 -0
- package/src/generateResponseType.js +419 -0
- package/src/generateVariablesType.js +153 -0
- package/src/index.js +93 -0
- package/src/jest-mock-graphql-tag.js +191 -0
- package/src/schemaFromIntrospectionData.js +68 -0
- package/src/types.js +54 -0
- package/src/utils.js +50 -0
- package/tools/find-files-with-gql.js +40 -0
package/.babelrc
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Changesets
|
|
2
|
+
|
|
3
|
+
Hello and welcome! This folder has been automatically generated by `@changesets/cli`, a build tool that works
|
|
4
|
+
with multi-package repos, or single-package repos to help you version and publish your code. You can
|
|
5
|
+
find the full documentation for it [in our repository](https://github.com/changesets/changesets)
|
|
6
|
+
|
|
7
|
+
We have a quick list of common questions to get you started engaging with this project in
|
|
8
|
+
[our documentation](https://github.com/changesets/changesets/blob/main/docs/common-questions.md)
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://unpkg.com/@changesets/config@1.7.0/schema.json",
|
|
3
|
+
"changelog": "@changesets/cli/changelog",
|
|
4
|
+
"commit": false,
|
|
5
|
+
"fixed": [],
|
|
6
|
+
"linked": [],
|
|
7
|
+
"access": "public",
|
|
8
|
+
"baseBranch": "main",
|
|
9
|
+
"updateInternalDependencies": "patch",
|
|
10
|
+
"ignore": []
|
|
11
|
+
}
|
package/.eslintignore
ADDED
package/.eslintrc.js
ADDED
package/.flowconfig
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
name: 'Filter files'
|
|
2
|
+
description: 'Filter the list of changed files'
|
|
3
|
+
inputs:
|
|
4
|
+
changed-files:
|
|
5
|
+
description: 'jsonified list of changed files from setup'
|
|
6
|
+
required: true
|
|
7
|
+
files:
|
|
8
|
+
description: 'comma-separated list of files to check for'
|
|
9
|
+
required: false
|
|
10
|
+
extensions:
|
|
11
|
+
description: 'comma-separated list of extensions to check for'
|
|
12
|
+
required: false
|
|
13
|
+
outputs:
|
|
14
|
+
filtered:
|
|
15
|
+
description: 'The jsonified list of files that match'
|
|
16
|
+
value: ${{ steps.result.outputs.result }}
|
|
17
|
+
runs:
|
|
18
|
+
using: "composite"
|
|
19
|
+
steps:
|
|
20
|
+
- uses: actions/github-script@v6
|
|
21
|
+
id: result
|
|
22
|
+
with:
|
|
23
|
+
script: |
|
|
24
|
+
const extensionsRaw = "${{ inputs.extensions }}";
|
|
25
|
+
const exactFilesRaw = "${{ inputs.files }}";
|
|
26
|
+
const inputFiles = JSON.parse(`${{ inputs.changed-files }}`);
|
|
27
|
+
const extensions = extensionsRaw.trim() ? extensionsRaw.split(',') : [];
|
|
28
|
+
const exactFiles = exactFilesRaw.trim() ? exactFilesRaw.split(',') : [];
|
|
29
|
+
|
|
30
|
+
const result = inputFiles.filter(name => {
|
|
31
|
+
return extensions.some(ext => name.endsWith(ext)) || (
|
|
32
|
+
exactFiles.includes(name)
|
|
33
|
+
)
|
|
34
|
+
})
|
|
35
|
+
console.log(`Filtered Files: ${JSON.stringify(result)}`)
|
|
36
|
+
return result;
|
|
37
|
+
result-encoding: json
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
name: Full or Limited
|
|
2
|
+
description: Do a full run if certain files have changed, or a limited run of some others have changed
|
|
3
|
+
inputs:
|
|
4
|
+
full-trigger:
|
|
5
|
+
description: A jsonified Array of string files that would trigger a full run
|
|
6
|
+
required: true
|
|
7
|
+
limited-trigger:
|
|
8
|
+
description: A jsonified Array of string files that should be passed to a limited run
|
|
9
|
+
required: true
|
|
10
|
+
full:
|
|
11
|
+
description: The command to run if a full run is triggered
|
|
12
|
+
limited:
|
|
13
|
+
description: The command to run, with {} replaced with the list of files to run on.
|
|
14
|
+
runs:
|
|
15
|
+
using: "composite"
|
|
16
|
+
steps:
|
|
17
|
+
- name: Full run
|
|
18
|
+
if: inputs.full-trigger != '[]'
|
|
19
|
+
run: ${{ inputs.full }}
|
|
20
|
+
shell: bash
|
|
21
|
+
|
|
22
|
+
- name: Limited run
|
|
23
|
+
if: inputs.full-trigger == '[]' && inputs.limited-trigger != '[]'
|
|
24
|
+
uses: ./.github/actions/json-args
|
|
25
|
+
with:
|
|
26
|
+
list: ${{ inputs.limited-trigger }}
|
|
27
|
+
run: ${{ inputs.limited }}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
name: 'Pass a jsonified-list of files as shell arguments'
|
|
2
|
+
description: 'Because file names with spaces are the worst'
|
|
3
|
+
inputs:
|
|
4
|
+
list:
|
|
5
|
+
description: A jsonified Array of string file names
|
|
6
|
+
required: true
|
|
7
|
+
run:
|
|
8
|
+
description: "a command to run, where the literal '{}' will be replaced with the files as individual arguments. If no '{}' is provided, the files will be appended."
|
|
9
|
+
required: true
|
|
10
|
+
runs:
|
|
11
|
+
using: "composite"
|
|
12
|
+
steps:
|
|
13
|
+
- uses: actions/github-script@v6
|
|
14
|
+
with:
|
|
15
|
+
script: |
|
|
16
|
+
const listRaw = `${{ inputs.list }}`;
|
|
17
|
+
const files = JSON.parse(listRaw);
|
|
18
|
+
const {execSync} = require('child_process');
|
|
19
|
+
if (files.some(name => name.match(/['"]/))) {
|
|
20
|
+
throw new Error(`Not going to mess with file names that have quotes in them.`)
|
|
21
|
+
}
|
|
22
|
+
const filesList = files.map(name => `"${name}"`).join(' ')
|
|
23
|
+
let cmd = `${{ inputs.run }}`;
|
|
24
|
+
if (cmd.includes('{}')) {
|
|
25
|
+
cmd = cmd.replace('{}', filesList)
|
|
26
|
+
} else {
|
|
27
|
+
cmd += ' ' + filesList;
|
|
28
|
+
}
|
|
29
|
+
console.log(`Running: ${cmd}`);
|
|
30
|
+
execSync(cmd, {
|
|
31
|
+
stdio: 'inherit',
|
|
32
|
+
})
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
name: 'Setup'
|
|
2
|
+
description: 'Perform various shared setup tasks'
|
|
3
|
+
inputs:
|
|
4
|
+
node-version:
|
|
5
|
+
description: 'Node version to use'
|
|
6
|
+
required: false
|
|
7
|
+
default: '16.x'
|
|
8
|
+
outputs:
|
|
9
|
+
changed_files:
|
|
10
|
+
description: "List of files changed in this PR, in JSON format"
|
|
11
|
+
value: ${{ steps.changed.outputs.added_modified }}
|
|
12
|
+
runs:
|
|
13
|
+
using: "composite"
|
|
14
|
+
steps:
|
|
15
|
+
- name: Use Node.js ${{ inputs.node-version }}
|
|
16
|
+
uses: actions/setup-node@v3
|
|
17
|
+
id: node
|
|
18
|
+
with:
|
|
19
|
+
node-version: ${{ inputs.node-version }}
|
|
20
|
+
cache: yarn
|
|
21
|
+
- name: Install dependencies
|
|
22
|
+
run: yarn install --frozen-lockfile
|
|
23
|
+
shell: bash
|
|
24
|
+
- name: Get All Changed Files
|
|
25
|
+
uses: jaredly/get-changed-files@absolute
|
|
26
|
+
id: changed
|
|
27
|
+
with:
|
|
28
|
+
format: json
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches:
|
|
6
|
+
- main
|
|
7
|
+
|
|
8
|
+
# This workflow will run changesets depending on two different scenarios:
|
|
9
|
+
#
|
|
10
|
+
# 1. If we are landing a specific commit into main (Author PR), then
|
|
11
|
+
# changesets will check if there are changes verifying the Markdown files
|
|
12
|
+
# generated automatically:
|
|
13
|
+
#
|
|
14
|
+
# a) There are new versions and there's NO Release PR, then the changesets
|
|
15
|
+
# action will create a new Release PR.
|
|
16
|
+
#
|
|
17
|
+
# b) There's a Release PR, then the changesets action will update the
|
|
18
|
+
# existing Release PR with the new commit.
|
|
19
|
+
#
|
|
20
|
+
# NOTE: (in both cases, changesets will modify the new version in
|
|
21
|
+
# package.json for each package, and will remove the MD files as part of the
|
|
22
|
+
# Release PR).
|
|
23
|
+
#
|
|
24
|
+
# 2. If we are landing the Release PR into main, then the changesets action
|
|
25
|
+
# will publish the changes to npm.
|
|
26
|
+
#
|
|
27
|
+
# For more info about this workflow, see:
|
|
28
|
+
# https://github.com/changesets/action#usage
|
|
29
|
+
jobs:
|
|
30
|
+
release:
|
|
31
|
+
name: Release
|
|
32
|
+
runs-on: ubuntu-latest
|
|
33
|
+
steps:
|
|
34
|
+
- uses: actions/checkout@v2
|
|
35
|
+
with:
|
|
36
|
+
fetch-depth: 0
|
|
37
|
+
- uses: ./.github/actions/setup
|
|
38
|
+
with:
|
|
39
|
+
node-version: 12.x
|
|
40
|
+
|
|
41
|
+
- name: Create Release Pull Request or Publish to npm
|
|
42
|
+
id: changesets
|
|
43
|
+
uses: changesets/action@v1
|
|
44
|
+
with:
|
|
45
|
+
publish: yarn publish:ci
|
|
46
|
+
env:
|
|
47
|
+
# We use a Personal Access Token here rather than the GITHUB_TOKEN
|
|
48
|
+
# so that it will trigger our other actions. The token has to be on
|
|
49
|
+
# the account of someone with appropriate access levels and given the
|
|
50
|
+
# repo scope.
|
|
51
|
+
GITHUB_TOKEN: ${{ secrets.KHAN_ACTIONS_BOT_TOKEN }}
|
|
52
|
+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
53
|
+
|
|
54
|
+
- name: Send a Slack notification for web if a publish happens
|
|
55
|
+
if: steps.changesets.outputs.published == 'true'
|
|
56
|
+
uses: rtCamp/action-slack-notify@v2
|
|
57
|
+
env:
|
|
58
|
+
SLACK_WEBHOOK: ${{ secrets.SLACK_FEIWEB_WEBHOOK }}
|
|
59
|
+
SLACK_CHANNEL: frontend-infra-web
|
|
60
|
+
SLACK_MSG_AUTHOR: ${{ github.event.pull_request.user.login }}
|
|
61
|
+
SLACK_USERNAME: GithubGoose
|
|
62
|
+
SLACK_ICON_EMOJI: ":goose:"
|
|
63
|
+
SLACK_MESSAGE: "A new version of ${{ github.event.repository.name }} was published! 🎉 \nRelease notes → https://github.com/Khan/${{ github.event.repository.name }}/releases/"
|
|
64
|
+
SLACK_TITLE: "New Graphql-Flow release!"
|
|
65
|
+
SLACK_FOOTER: Graphql-Flow Slack Notification
|
|
66
|
+
MSG_MINIMAL: true
|
|
67
|
+
|
|
68
|
+
- name: Send a Slack notification for mobile if a publish happens
|
|
69
|
+
if: steps.changesets.outputs.published == 'true'
|
|
70
|
+
uses: rtCamp/action-slack-notify@v2
|
|
71
|
+
env:
|
|
72
|
+
SLACK_WEBHOOK: ${{ secrets.SLACK_FEIMOBILE_WEBHOOK }}
|
|
73
|
+
SLACK_CHANNEL: frontend-infra-mobile
|
|
74
|
+
SLACK_MSG_AUTHOR: ${{ github.event.pull_request.user.login }}
|
|
75
|
+
SLACK_USERNAME: GithubGoose
|
|
76
|
+
SLACK_ICON_EMOJI: ":goose:"
|
|
77
|
+
SLACK_MESSAGE: "A new version of ${{ github.event.repository.name }} was published! 🎉 \nRelease notes → https://github.com/Khan/${{ github.event.repository.name }}/releases/"
|
|
78
|
+
SLACK_TITLE: "New Graphql-Flow release!"
|
|
79
|
+
SLACK_FOOTER: Graphql-Flow Slack Notification
|
|
80
|
+
MSG_MINIMAL: true
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
name: Lint & Test
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
# edited is needed because that's the trigger when the base branch is
|
|
6
|
+
# changed on a PR
|
|
7
|
+
# The rest are the defaults.
|
|
8
|
+
types: [edited, opened, synchronize, reopened]
|
|
9
|
+
|
|
10
|
+
jobs:
|
|
11
|
+
lint_and_test:
|
|
12
|
+
name: Lint & Test
|
|
13
|
+
runs-on: ${{ matrix.os }}
|
|
14
|
+
strategy:
|
|
15
|
+
matrix:
|
|
16
|
+
os: [ubuntu-latest]
|
|
17
|
+
node-version: [16.x]
|
|
18
|
+
steps:
|
|
19
|
+
- uses: actions/checkout@v2
|
|
20
|
+
- uses: ./.github/actions/setup
|
|
21
|
+
id: setup
|
|
22
|
+
with:
|
|
23
|
+
node-version: ${{ matrix.node-version }}
|
|
24
|
+
|
|
25
|
+
- id: js-files
|
|
26
|
+
name: Find .js changed files
|
|
27
|
+
uses: ./.github/actions/filter-files
|
|
28
|
+
with:
|
|
29
|
+
changed-files: ${{ steps.setup.outputs.changed_files }}
|
|
30
|
+
extensions: '.js'
|
|
31
|
+
|
|
32
|
+
- name: Run Flow
|
|
33
|
+
if: steps.js-files.outputs.filtered != '[]'
|
|
34
|
+
run: yarn flow
|
|
35
|
+
|
|
36
|
+
- id: eslint-reset
|
|
37
|
+
uses: ./.github/actions/filter-files
|
|
38
|
+
name: Files that would trigger a full eslint run
|
|
39
|
+
with:
|
|
40
|
+
changed-files: ${{ steps.setup.outputs.changed_files }}
|
|
41
|
+
files: '.eslintrc.js,package.json,.eslintignore'
|
|
42
|
+
|
|
43
|
+
- name: Eslint
|
|
44
|
+
uses: ./.github/actions/full-or-limited
|
|
45
|
+
with:
|
|
46
|
+
full-trigger: ${{ steps.eslint-reset.outputs.filtered }}
|
|
47
|
+
full: yarn eslint
|
|
48
|
+
limited-trigger: ${{ steps.js-files.outputs.filtered }}
|
|
49
|
+
limited: yarn eslint {}
|
|
50
|
+
|
|
51
|
+
- id: jest-reset
|
|
52
|
+
uses: ./.github/actions/filter-files
|
|
53
|
+
name: Files that would trigger a full jest run
|
|
54
|
+
with:
|
|
55
|
+
changed-files: ${{ steps.setup.outputs.changed_files }}
|
|
56
|
+
files: 'package.json,package-lock.json'
|
|
57
|
+
|
|
58
|
+
- name: Jest
|
|
59
|
+
uses: ./.github/actions/full-or-limited
|
|
60
|
+
with:
|
|
61
|
+
full-trigger: ${{ steps.jest-reset.outputs.filtered }}
|
|
62
|
+
full: yarn jest
|
|
63
|
+
limited-trigger: ${{ steps.js-files.outputs.filtered }}
|
|
64
|
+
limited: yarn jest --findRelatedTests {}
|
package/.prettierrc
ADDED
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# @khanacademy/graphql-flow
|
|
2
|
+
|
|
3
|
+
## 0.0.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- a4ae2c8: First release! Hoping this works.
|
|
8
|
+
- a3e8461: Make a simple change to check out the changeset stuff
|
|
9
|
+
|
|
10
|
+
## 0.0.1
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- a64c3c7: First release! Hoping this works.
|
package/Readme.md
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
# Graphql Flow Generation
|
|
2
|
+
|
|
3
|
+
This is a tool for generating flow types from graphql queries in javascript frontends.
|
|
4
|
+
|
|
5
|
+
The core of this tool is the `documentToFlowTypes` function, which takes a `DocumentNode` (such as is returned by the `graphql-tag` package) as well as your backend's graphql schema (see below for instructions on how to produce this), and produces a list of stringified flow types, one for each query and mutation defined in that graphql block.
|
|
6
|
+
|
|
7
|
+
It looks something like this:
|
|
8
|
+
|
|
9
|
+
```js
|
|
10
|
+
import gql from 'graphql-tag';
|
|
11
|
+
import {documentToFlowTypes, schemaFromIntrospectionData} from 'graphql-flow';
|
|
12
|
+
import myIntrospectionData from './server-introspection-response.json';
|
|
13
|
+
|
|
14
|
+
const schema = schemaFromIntrospectionData(myIntrospectionData);
|
|
15
|
+
|
|
16
|
+
const MyQuery = gql`
|
|
17
|
+
query SomeQuery {
|
|
18
|
+
human(id: "Han Solo") {
|
|
19
|
+
id
|
|
20
|
+
name
|
|
21
|
+
homePlanet
|
|
22
|
+
friends {
|
|
23
|
+
name
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
`;
|
|
28
|
+
|
|
29
|
+
console.log(documentToFlowTypes(MyQuery, schema))
|
|
30
|
+
/*
|
|
31
|
+
export type SomeQueryResponseType = {|
|
|
32
|
+
human: ?{|
|
|
33
|
+
id: string,
|
|
34
|
+
name: ?string,
|
|
35
|
+
homePlanet: ?string,
|
|
36
|
+
friends: ?$ReadOnlyArray<?{|
|
|
37
|
+
name: ?string
|
|
38
|
+
|}>,
|
|
39
|
+
|}
|
|
40
|
+
|};
|
|
41
|
+
*/
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
If you already have a setup whereby you collect all of your graphql literals, that may be all you need!
|
|
45
|
+
|
|
46
|
+
Otherwise, we provide a way to hook into jest to automatically collect your queries and generate the types.
|
|
47
|
+
|
|
48
|
+
## Options for `documentToFlowTypes`
|
|
49
|
+
|
|
50
|
+
```js
|
|
51
|
+
{
|
|
52
|
+
// Use nullable types where the graphql type is nullable. Included for legacy compatability,
|
|
53
|
+
// will probably remove once the mobile repo no longer needs it.
|
|
54
|
+
strictNullability: boolean = true,
|
|
55
|
+
// Output `$ReadOnlyArray<>` instead of `Array<>`, for stricter flow typing. On by default.
|
|
56
|
+
readOnlyArray: boolean = true,
|
|
57
|
+
// A mapping of custom scalar names to the underlying json representation.
|
|
58
|
+
scalars: {[key: string]: 'string' | 'boolean' | 'number'}
|
|
59
|
+
}
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Using jest to do the heavy lifting:
|
|
63
|
+
|
|
64
|
+
### jest-setup.js
|
|
65
|
+
|
|
66
|
+
Add the following snippet to your jest-setup.js
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
if (process.env.GRAPHQL_FLOW) {
|
|
70
|
+
jest.mock('graphql-tag', () => {
|
|
71
|
+
const introspectionData = jest.requireActual(
|
|
72
|
+
'./server-introspection-response.json',
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
return jest.requireActual('../tools/graphql-flow/jest-mock-graphql-tag.js')(
|
|
76
|
+
introspectionData,
|
|
77
|
+
{
|
|
78
|
+
pragma: '# @autogen\n',
|
|
79
|
+
loosePragma: '# @autogen-loose\n',
|
|
80
|
+
}
|
|
81
|
+
);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
That will mock out the `graphql-tag` function, so that everywhere you call 'gql`some-query`', we'll pick it up and
|
|
87
|
+
generate a type for it! As written, the mocking only happens if the `GRAPHQL_FLOW` env variable is set, so that it won't run every time.
|
|
88
|
+
|
|
89
|
+
By default, all queries are processed. To have them 'opt-in', use the `pragma` and `loosePragma` options. Queries with `loosePragma` in the source will have types generated that ignore nullability.
|
|
90
|
+
|
|
91
|
+
### Ensure all files with queries get processed by jest
|
|
92
|
+
|
|
93
|
+
Then you'll want to make a pseudo-'test' that makes sure to 'require' all of the files that define queries, so that
|
|
94
|
+
jest will process them and our mock will see them.
|
|
95
|
+
```js
|
|
96
|
+
// generate-types.test.js
|
|
97
|
+
import {findFilesWithQueries} from '../tools/graphql-flow/find-files-with-gql';
|
|
98
|
+
|
|
99
|
+
if (process.env.GRAPHQL_FLOW) {
|
|
100
|
+
findFilesWithQueries(path.join(__dirname, '..')).forEach(name => {
|
|
101
|
+
require(name);
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('should have found queries', () => {
|
|
105
|
+
const gql = require('graphql-tag')
|
|
106
|
+
expect(gql.collectedQueries.length).toBeGreaterThan(0)
|
|
107
|
+
})
|
|
108
|
+
} else {
|
|
109
|
+
it(`not generating graphql types because the env flag isn't set`, () => {
|
|
110
|
+
// not doing anything because the env flag isn't set.
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
### Run that test to generate the types!
|
|
116
|
+
|
|
117
|
+
You can add a script to package.json, like so:
|
|
118
|
+
```json
|
|
119
|
+
"scripts": {
|
|
120
|
+
"generate-types": "env GRAPHQL_FLOW=1 jest generate-types.test.js"
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
And then `yarn generate-types` or `npm run generate-types` gets your types generated!
|
|
125
|
+
|
|
126
|
+
🚀
|
|
127
|
+
|
|
128
|
+
### Options for the `jest-mock-graphql-tag.js` helper:
|
|
129
|
+
|
|
130
|
+
```js
|
|
131
|
+
{
|
|
132
|
+
// These are from the `documentToFlowTypes` options object above
|
|
133
|
+
strictNullability: boolean = true,
|
|
134
|
+
readOnlyArray: boolean = true,
|
|
135
|
+
scalars: {[key: string]: 'string' | 'boolean' | 'number'}
|
|
136
|
+
|
|
137
|
+
// Specify an opt-in pragma that must be present in a graphql string source
|
|
138
|
+
// in order for it to be picked up and processed
|
|
139
|
+
// e.g. set this to `"# @autogen\n"` to only generate types for queries that
|
|
140
|
+
// have the comment `# @autogen` in them.
|
|
141
|
+
pragma?: string,
|
|
142
|
+
// Specify a pragma that will turn off `strictNullability` for that
|
|
143
|
+
// source file. e.g. `"# @autogen-loose\n"`.
|
|
144
|
+
loosePragma?: string,
|
|
145
|
+
// If neither pragma nor loosePragma are specified, all graphql documents
|
|
146
|
+
// that contain a query or mutation will be processed.
|
|
147
|
+
}
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
## Introspecting your backend's graphql schema
|
|
151
|
+
Here's how to get your backend's schema in the way that this tool expects, using the builtin 'graphql introspection query':
|
|
152
|
+
|
|
153
|
+
```js
|
|
154
|
+
import {getIntrospectionQuery} from 'graphql';
|
|
155
|
+
import fs from 'fs';
|
|
156
|
+
import fetch from 'node-fetch';
|
|
157
|
+
|
|
158
|
+
const query = getIntrospectionQuery({descriptions: true}),
|
|
159
|
+
|
|
160
|
+
const response = await fetch(`https://my-backend.com`, {
|
|
161
|
+
method: 'POST',
|
|
162
|
+
body: query,
|
|
163
|
+
headers: {
|
|
164
|
+
// You definitely shouldn't be allowing arbitrary queries without
|
|
165
|
+
// some strict access control.
|
|
166
|
+
'X-header-that-allows-arbitrary-queries': 'my-secret-key',
|
|
167
|
+
},
|
|
168
|
+
contentType: 'application/json',
|
|
169
|
+
});
|
|
170
|
+
const fullResponse = await response.json();
|
|
171
|
+
fs.writeFileSync('./server-introspection-response.json', JSON.stringify(fullResponse.data, null, 2));
|
|
172
|
+
```
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// A simple script to copy over source files as *.js.flow
|
|
2
|
+
// so that flow will be happy with the distributed package.
|
|
3
|
+
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
|
|
7
|
+
// little recursive walk
|
|
8
|
+
const walk = (dir, fn) => {
|
|
9
|
+
fs.readdirSync(dir).forEach((name) => {
|
|
10
|
+
const full = path.join(dir, name);
|
|
11
|
+
if (fs.statSync(full).isDirectory()) {
|
|
12
|
+
walk(full, fn);
|
|
13
|
+
} else {
|
|
14
|
+
fn(full);
|
|
15
|
+
}
|
|
16
|
+
});
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const src = path.join(__dirname, 'src');
|
|
20
|
+
const dist = path.join(__dirname, 'dist');
|
|
21
|
+
|
|
22
|
+
walk(src, (name) => {
|
|
23
|
+
if (!name.endsWith('.js') || name.includes('__test__')) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
const next = path.join(dist, path.relative(src, name)) + '.flow';
|
|
27
|
+
fs.copyFileSync(name, next);
|
|
28
|
+
});
|
package/dist/enums.js
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.scalarTypeToFlow = exports.enumTypeToFlow = exports.builtinScalars = void 0;
|
|
7
|
+
|
|
8
|
+
var babelTypes = _interopRequireWildcard(require("@babel/types"));
|
|
9
|
+
|
|
10
|
+
var _utils = require("./utils");
|
|
11
|
+
|
|
12
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
13
|
+
|
|
14
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Both input & output types can have enums & scalars.
|
|
18
|
+
*/
|
|
19
|
+
const enumTypeToFlow = (config, name) => {
|
|
20
|
+
const enumConfig = config.schema.enumsByName[name];
|
|
21
|
+
let combinedDescription = enumConfig.enumValues.map(n => `- ${n.name}` + (n.description ? '\n\n ' + n.description.replace(/\n/g, '\n ') : '')).join('\n');
|
|
22
|
+
|
|
23
|
+
if (enumConfig.description) {
|
|
24
|
+
combinedDescription = enumConfig.description + '\n\n' + combinedDescription;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return (0, _utils.maybeAddDescriptionComment)(combinedDescription, babelTypes.unionTypeAnnotation(enumConfig.enumValues.map(n => babelTypes.stringLiteralTypeAnnotation(n.name))));
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
exports.enumTypeToFlow = enumTypeToFlow;
|
|
31
|
+
const builtinScalars = {
|
|
32
|
+
Boolean: 'boolean',
|
|
33
|
+
String: 'string',
|
|
34
|
+
DateTime: 'string',
|
|
35
|
+
Date: 'string',
|
|
36
|
+
ID: 'string',
|
|
37
|
+
Int: 'number',
|
|
38
|
+
Float: 'number'
|
|
39
|
+
};
|
|
40
|
+
exports.builtinScalars = builtinScalars;
|
|
41
|
+
|
|
42
|
+
const scalarTypeToFlow = (config, name) => {
|
|
43
|
+
if (builtinScalars[name]) {
|
|
44
|
+
return babelTypes.genericTypeAnnotation(babelTypes.identifier(builtinScalars[name]));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const underlyingType = config.scalars[name];
|
|
48
|
+
|
|
49
|
+
if (underlyingType != null) {
|
|
50
|
+
return babelTypes.genericTypeAnnotation(babelTypes.identifier(underlyingType));
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
config.errors.push(`Unexpected scalar '${name}'! Please add it to the "scalars" argument at the callsite of 'generateFlowTypes()'.`);
|
|
54
|
+
return babelTypes.genericTypeAnnotation(babelTypes.identifier(`UNKNOWN_SCALAR["${name}"]`));
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
exports.scalarTypeToFlow = scalarTypeToFlow;
|