@khanacademy/graphql-flow 1.2.0 → 2.0.0

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.
Files changed (78) hide show
  1. package/.babelrc +1 -1
  2. package/.eslintrc.js +0 -1
  3. package/.github/workflows/changeset-release.yml +1 -1
  4. package/CHANGELOG.md +10 -0
  5. package/dist/cli/config.js +2 -4
  6. package/dist/cli/run.js +1 -2
  7. package/dist/enums.js +8 -9
  8. package/dist/generateResponseType.js +33 -41
  9. package/dist/generateTypeFiles.js +9 -23
  10. package/dist/generateVariablesType.js +15 -31
  11. package/dist/index.js +8 -15
  12. package/dist/parser/parse.js +6 -7
  13. package/dist/parser/resolve.js +1 -2
  14. package/dist/parser/utils.js +1 -2
  15. package/dist/schemaFromIntrospectionData.js +1 -2
  16. package/dist/types.js +1 -2
  17. package/dist/utils.js +43 -3
  18. package/package.json +8 -7
  19. package/{dist/__test__/generateTypeFileContents.test.js → src/__test__/generateTypeFileContents.test.ts} +38 -41
  20. package/{dist/__test__/graphql-flow.test.js → src/__test__/graphql-flow.test.ts} +232 -235
  21. package/src/__test__/{processPragmas.test.js → processPragmas.test.ts} +0 -1
  22. package/{dist/cli/__test__/config.test.js → src/cli/__test__/config.test.ts} +5 -6
  23. package/{dist/cli/config.js.flow → src/cli/config.ts} +6 -11
  24. package/src/cli/{run.js → run.ts} +5 -4
  25. package/src/{enums.js → enums.ts} +20 -22
  26. package/src/{generateResponseType.js → generateResponseType.ts} +167 -182
  27. package/src/{generateTypeFiles.js → generateTypeFiles.ts} +20 -30
  28. package/src/{generateVariablesType.js → generateVariablesType.ts} +34 -44
  29. package/{dist/index.js.flow → src/index.ts} +32 -24
  30. package/{dist/parser/__test__/parse.test.js → src/parser/__test__/parse.test.ts} +12 -11
  31. package/src/parser/{parse.js → parse.ts} +65 -47
  32. package/{dist/parser/resolve.js.flow → src/parser/resolve.ts} +15 -11
  33. package/{dist/parser/utils.js.flow → src/parser/utils.ts} +0 -1
  34. package/{dist/schemaFromIntrospectionData.js.flow → src/schemaFromIntrospectionData.ts} +1 -4
  35. package/src/types.ts +97 -0
  36. package/src/utils.ts +73 -0
  37. package/tools/{find-files-with-gql.js → find-files-with-gql.ts} +2 -3
  38. package/tsconfig.json +110 -0
  39. package/types/flow-to-ts.d.ts +1 -0
  40. package/dist/__test__/example-schema.graphql +0 -67
  41. package/dist/__test__/processPragmas.test.js +0 -76
  42. package/dist/cli/config.js.map +0 -1
  43. package/dist/cli/run.js.flow +0 -236
  44. package/dist/cli/run.js.map +0 -1
  45. package/dist/enums.js.flow +0 -98
  46. package/dist/enums.js.map +0 -1
  47. package/dist/generateResponseType.js.flow +0 -583
  48. package/dist/generateResponseType.js.map +0 -1
  49. package/dist/generateTypeFiles.js.flow +0 -191
  50. package/dist/generateTypeFiles.js.map +0 -1
  51. package/dist/generateVariablesType.js.flow +0 -156
  52. package/dist/generateVariablesType.js.map +0 -1
  53. package/dist/index.js.map +0 -1
  54. package/dist/parser/parse.js.flow +0 -417
  55. package/dist/parser/parse.js.map +0 -1
  56. package/dist/parser/resolve.js.map +0 -1
  57. package/dist/parser/utils.js.map +0 -1
  58. package/dist/schemaFromIntrospectionData.js.map +0 -1
  59. package/dist/types.js.flow +0 -88
  60. package/dist/types.js.map +0 -1
  61. package/dist/utils.js.flow +0 -50
  62. package/dist/utils.js.map +0 -1
  63. package/flow-typed/npm/@babel/types_vx.x.x.js +0 -5331
  64. package/flow-typed/npm/jest_v23.x.x.js +0 -1155
  65. package/flow-typed/overrides.js +0 -435
  66. package/src/__test__/generateTypeFileContents.test.js +0 -157
  67. package/src/__test__/graphql-flow.test.js +0 -639
  68. package/src/cli/__test__/config.test.js +0 -120
  69. package/src/cli/config.js +0 -84
  70. package/src/cli/schema.json +0 -97
  71. package/src/index.js +0 -160
  72. package/src/parser/__test__/parse.test.js +0 -249
  73. package/src/parser/resolve.js +0 -119
  74. package/src/parser/utils.js +0 -25
  75. package/src/schemaFromIntrospectionData.js +0 -68
  76. package/src/types.js +0 -88
  77. package/src/utils.js +0 -50
  78. /package/{dist/cli/schema.json → schema.json} +0 -0
@@ -1,14 +1,13 @@
1
- // @flow
2
1
  import type {Config} from '../../types';
3
2
 
4
3
  import {findApplicableConfig, validateOrThrow} from '../config';
5
- import configSchema from '../schema.json'; // eslint-disable-line flowtype-errors/uncovered
4
+ import configSchema from '../../../schema.json';
6
5
 
7
6
  describe('findApplicableConfig', () => {
8
7
  it('should work with one that matches', () => {
9
8
  const config = {
10
9
  schemaFilePath: 'ok.graphql',
11
- };
10
+ } as const;
12
11
  expect(findApplicableConfig('/hello', config)).toBe(config);
13
12
  });
14
13
 
@@ -16,8 +15,8 @@ describe('findApplicableConfig', () => {
16
15
  const config = {
17
16
  schemaFilePath: 'ok.graphql',
18
17
  exclude: [/hello$/],
19
- };
20
- expect(findApplicableConfig('/hello', config)).toBeUndefined();
18
+ } as const;
19
+ expect(findApplicableConfig('/hello', config as any)).toBeUndefined();
21
20
  });
22
21
 
23
22
  it('should match & exclude with multiple configs', () => {
@@ -88,7 +87,7 @@ describe('jsonschema validation', () => {
88
87
  generatedDirectory: '__graphql-types__',
89
88
  exportAllObjectTypes: true,
90
89
  schemaFilePath: './composed_schema.graphql',
91
- };
90
+ } as const;
92
91
  const config: Config = {
93
92
  crawl: {
94
93
  root: '/here/we/crawl',
@@ -1,9 +1,8 @@
1
- // @flow
2
1
  import type {Schema} from '../types';
3
2
  import type {GraphQLSchema} from 'graphql/type/schema';
4
3
 
5
4
  import {schemaFromIntrospectionData} from '../schemaFromIntrospectionData';
6
- import configSchema from './schema.json'; // eslint-disable-line flowtype-errors/uncovered
5
+ import configSchema from '../../schema.json'; // eslint-disable-line flowtype-errors/uncovered
7
6
 
8
7
  import fs from 'fs';
9
8
  import {
@@ -11,12 +10,12 @@ import {
11
10
  buildSchema,
12
11
  getIntrospectionQuery,
13
12
  graphqlSync,
14
- type IntrospectionQuery,
13
+ IntrospectionQuery,
15
14
  } from 'graphql';
16
15
  import type {Config, GenerateConfig} from '../types';
17
16
  import {validate} from 'jsonschema'; // eslint-disable-line flowtype-errors/uncovered
18
17
 
19
- export const validateOrThrow = (value: mixed, jsonSchema: mixed) => {
18
+ export const validateOrThrow = (value: unknown, jsonSchema: unknown) => {
20
19
  /* eslint-disable flowtype-errors/uncovered */
21
20
  const result = validate(value, jsonSchema);
22
21
  if (!result.valid) {
@@ -28,7 +27,6 @@ export const validateOrThrow = (value: mixed, jsonSchema: mixed) => {
28
27
  };
29
28
 
30
29
  export const loadConfigFile = (configFile: string): Config => {
31
- // $FlowIgnore
32
30
  const data: Config = require(configFile); // eslint-disable-line flowtype-errors/uncovered
33
31
  validateOrThrow(data, configSchema); // eslint-disable-line flowtype-errors/uncovered
34
32
  return data;
@@ -47,7 +45,7 @@ export const getSchemas = (schemaFilePath: string): [GraphQLSchema, Schema] => {
47
45
  );
48
46
  const schemaForTypeGeneration = schemaFromIntrospectionData(
49
47
  // eslint-disable-next-line flowtype-errors/uncovered
50
- ((queryResponse.data: any): IntrospectionQuery),
48
+ (queryResponse.data as IntrospectionQuery),
51
49
  );
52
50
  return [schemaForValidation, schemaForTypeGeneration];
53
51
  } else {
@@ -65,15 +63,12 @@ export const getSchemas = (schemaFilePath: string): [GraphQLSchema, Schema] => {
65
63
  * - no item of `exclude` matches
66
64
  * - at least one item of `match` matches
67
65
  */
68
- export const findApplicableConfig = (
69
- path: string,
70
- configs: Array<GenerateConfig> | GenerateConfig,
71
- ): ?GenerateConfig => {
66
+ export const findApplicableConfig = (path: string, configs: Array<GenerateConfig> | GenerateConfig): GenerateConfig | null | undefined => {
72
67
  if (!Array.isArray(configs)) {
73
68
  configs = [configs];
74
69
  }
75
70
  return configs.find((config) => {
76
- if (config.exclude?.some((exclude) => new RegExp(exclude).test(path))) {
71
+ if (config.exclude?.some((exclude: any) => new RegExp(exclude).test(path))) {
77
72
  return false;
78
73
  }
79
74
  if (!config.match) {
@@ -1,5 +1,4 @@
1
1
  #!/usr/bin/env node
2
- // @flow
3
2
  /* eslint-disable no-console */
4
3
  import type {Schema} from '../types';
5
4
  import type {GraphQLSchema} from 'graphql/type/schema';
@@ -13,7 +12,7 @@ import {addTypenameToDocument} from 'apollo-utilities'; // eslint-disable-line f
13
12
 
14
13
  import {execSync} from 'child_process';
15
14
  import {existsSync, mkdirSync, readFileSync, writeFileSync} from 'fs';
16
- import {type DocumentNode} from 'graphql';
15
+ import {DocumentNode} from 'graphql';
17
16
  import {print} from 'graphql/language/printer';
18
17
  import {validate} from 'graphql/validation';
19
18
  import path from 'path';
@@ -129,7 +128,9 @@ console.log(Object.keys(resolved).length, 'resolved queries');
129
128
 
130
129
  /** Step (4) */
131
130
 
132
- const schemaCache: {[key: string]: [GraphQLSchema, Schema]} = {};
131
+ const schemaCache: {
132
+ [key: string]: [GraphQLSchema, Schema]
133
+ } = {};
133
134
  const getCachedSchemas = (schemaFilePath: string) => {
134
135
  if (!schemaCache[schemaFilePath]) {
135
136
  schemaCache[schemaFilePath] = getSchemas(
@@ -208,7 +209,7 @@ Object.keys(resolved).forEach((filePathAndLine) => {
208
209
  generateConfig,
209
210
  );
210
211
  // eslint-disable-next-line flowtype-errors/uncovered
211
- } catch (err) {
212
+ } catch (err: any) {
212
213
  console.error(`Error while generating operation from ${raw.loc.path}`);
213
214
  console.error(printed);
214
215
  // eslint-disable-next-line flowtype-errors/uncovered
@@ -1,9 +1,8 @@
1
- // @flow
2
1
  /**
3
2
  * Both input & output types can have enums & scalars.
4
3
  */
5
4
  import * as babelTypes from '@babel/types';
6
- import type {BabelNodeFlowType} from '@babel/types';
5
+ import type {TSType} from '@babel/types';
7
6
  import type {Context} from './types';
8
7
  import {maybeAddDescriptionComment} from './utils';
9
8
  import type {IntrospectionEnumType} from 'graphql/utilities/introspectionQuery';
@@ -12,13 +11,14 @@ export const experimentalEnumTypeToFlow = (
12
11
  ctx: Context,
13
12
  enumConfig: IntrospectionEnumType,
14
13
  description: string,
15
- ): BabelNodeFlowType => {
16
- const enumDeclaration = babelTypes.enumDeclaration(
14
+ ): TSType => {
15
+ const enumDeclaration = babelTypes.tsEnumDeclaration(
17
16
  // pass id into generic type annotation
18
17
  babelTypes.identifier(enumConfig.name),
19
- babelTypes.enumStringBody(
20
- enumConfig.enumValues.map((v) =>
21
- babelTypes.enumDefaultedMember(babelTypes.identifier(v.name)),
18
+ enumConfig.enumValues.map((v) =>
19
+ babelTypes.tsEnumMember(
20
+ babelTypes.identifier(v.name),
21
+ babelTypes.stringLiteral(v.name),
22
22
  ),
23
23
  ),
24
24
  );
@@ -29,14 +29,11 @@ export const experimentalEnumTypeToFlow = (
29
29
 
30
30
  return maybeAddDescriptionComment(
31
31
  description,
32
- babelTypes.genericTypeAnnotation(enumDeclaration.id),
32
+ babelTypes.tsTypeReference(enumDeclaration.id),
33
33
  );
34
34
  };
35
35
 
36
- export const enumTypeToFlow = (
37
- ctx: Context,
38
- name: string,
39
- ): BabelNodeFlowType => {
36
+ export const enumTypeToFlow = (ctx: Context, name: string): TSType => {
40
37
  const enumConfig = ctx.schema.enumsByName[name];
41
38
  let combinedDescription = enumConfig.enumValues
42
39
  .map(
@@ -56,15 +53,19 @@ export const enumTypeToFlow = (
56
53
  ? experimentalEnumTypeToFlow(ctx, enumConfig, combinedDescription)
57
54
  : maybeAddDescriptionComment(
58
55
  combinedDescription,
59
- babelTypes.unionTypeAnnotation(
56
+ babelTypes.tsUnionType(
60
57
  enumConfig.enumValues.map((n) =>
61
- babelTypes.stringLiteralTypeAnnotation(n.name),
58
+ babelTypes.tsLiteralType(
59
+ babelTypes.stringLiteral(n.name),
60
+ ),
62
61
  ),
63
62
  ),
64
63
  );
65
64
  };
66
65
 
67
- export const builtinScalars: {[key: string]: string} = {
66
+ export const builtinScalars: {
67
+ [key: string]: string,
68
+ } = {
68
69
  Boolean: 'boolean',
69
70
  String: 'string',
70
71
  DateTime: 'string',
@@ -74,25 +75,22 @@ export const builtinScalars: {[key: string]: string} = {
74
75
  Float: 'number',
75
76
  };
76
77
 
77
- export const scalarTypeToFlow = (
78
- ctx: Context,
79
- name: string,
80
- ): BabelNodeFlowType => {
78
+ export const scalarTypeToFlow = (ctx: Context, name: string): TSType => {
81
79
  if (builtinScalars[name]) {
82
- return babelTypes.genericTypeAnnotation(
80
+ return babelTypes.tsTypeReference(
83
81
  babelTypes.identifier(builtinScalars[name]),
84
82
  );
85
83
  }
86
84
  const underlyingType = ctx.scalars[name];
87
85
  if (underlyingType != null) {
88
- return babelTypes.genericTypeAnnotation(
86
+ return babelTypes.tsTypeReference(
89
87
  babelTypes.identifier(underlyingType),
90
88
  );
91
89
  }
92
90
  ctx.errors.push(
93
91
  `Unexpected scalar '${name}'! Please add it to the "scalars" argument at the callsite of 'generateFlowTypes()'.`,
94
92
  );
95
- return babelTypes.genericTypeAnnotation(
93
+ return babelTypes.tsTypeReference(
96
94
  babelTypes.identifier(`UNKNOWN_SCALAR["${name}"]`),
97
95
  );
98
96
  };