@graphprotocol/graph-cli 0.37.3-alpha-20230113140726-64762bb → 0.37.3

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 (131) hide show
  1. package/CHANGELOG.md +5 -1
  2. package/package.json +1 -1
  3. package/src/cli.ts +1 -1
  4. package/src/command-helpers/abi.ts +2 -0
  5. package/src/command-helpers/auth.ts +3 -3
  6. package/src/command-helpers/compiler.ts +1 -1
  7. package/src/command-helpers/fs.ts +1 -1
  8. package/src/command-helpers/network.test.ts +1 -1
  9. package/src/command-helpers/network.ts +1 -1
  10. package/src/command-helpers/node.ts +1 -1
  11. package/src/command-helpers/scaffold.ts +1 -1
  12. package/src/commands/codegen.ts +1 -1
  13. package/src/commands/create.ts +1 -1
  14. package/src/commands/deploy.ts +2 -2
  15. package/src/commands/init.ts +3 -3
  16. package/src/commands/local.ts +4 -4
  17. package/src/commands/remove.ts +1 -1
  18. package/src/commands/test.ts +3 -3
  19. package/src/compiler/index.ts +2 -2
  20. package/src/migrations/util/load-manifest.ts +1 -1
  21. package/src/migrations/util/versions.ts +1 -1
  22. package/src/protocols/ethereum/abi.ts +1 -1
  23. package/src/protocols/ethereum/codegen/abi.test.ts +1 -1
  24. package/src/protocols/ethereum/codegen/abi.ts +1 -1
  25. package/src/protocols/ethereum/type-generator.ts +1 -1
  26. package/src/subgraph.ts +1 -1
  27. package/src/type-generator.ts +1 -1
  28. package/src/validation/manifest.ts +2 -2
  29. package/src/validation/schema.ts +1 -1
  30. package/src/watcher.ts +1 -1
  31. package/tests/cli/init.test.ts +1 -1
  32. package/tests/cli/spawn-command.d.ts +1 -1
  33. package/tests/cli/util.ts +1 -1
  34. package/dist/bin.js +0 -6
  35. package/dist/cli.js +0 -26
  36. package/dist/codegen/schema.js +0 -244
  37. package/dist/codegen/schema.test.js +0 -455
  38. package/dist/codegen/template.js +0 -66
  39. package/dist/codegen/types/conversions.js +0 -335
  40. package/dist/codegen/types/index.js +0 -71
  41. package/dist/codegen/types/index.test.js +0 -563
  42. package/dist/codegen/typescript.js +0 -200
  43. package/dist/codegen/util.js +0 -40
  44. package/dist/codegen/util.test.js +0 -93
  45. package/dist/command-helpers/abi.js +0 -74
  46. package/dist/command-helpers/auth.js +0 -76
  47. package/dist/command-helpers/compiler.js +0 -66
  48. package/dist/command-helpers/data-sources.js +0 -29
  49. package/dist/command-helpers/fs.js +0 -9
  50. package/dist/command-helpers/gluegun.js +0 -48
  51. package/dist/command-helpers/ipfs.js +0 -5
  52. package/dist/command-helpers/jsonrpc.js +0 -27
  53. package/dist/command-helpers/network.js +0 -90
  54. package/dist/command-helpers/network.test.js +0 -86
  55. package/dist/command-helpers/node.js +0 -40
  56. package/dist/command-helpers/scaffold.js +0 -110
  57. package/dist/command-helpers/spinner.js +0 -80
  58. package/dist/command-helpers/studio.js +0 -14
  59. package/dist/command-helpers/studio.test.js +0 -41
  60. package/dist/command-helpers/subgraph.js +0 -22
  61. package/dist/command-helpers/version.js +0 -66
  62. package/dist/command-helpers/version.test.js +0 -186
  63. package/dist/commands/add.js +0 -187
  64. package/dist/commands/auth.js +0 -122
  65. package/dist/commands/build.js +0 -136
  66. package/dist/commands/codegen.js +0 -128
  67. package/dist/commands/create.js +0 -94
  68. package/dist/commands/deploy.js +0 -335
  69. package/dist/commands/init.js +0 -800
  70. package/dist/commands/local.js +0 -380
  71. package/dist/commands/remove.js +0 -95
  72. package/dist/commands/test.js +0 -293
  73. package/dist/compiler/asc.js +0 -83
  74. package/dist/compiler/index.js +0 -466
  75. package/dist/debug.js +0 -16
  76. package/dist/migrations/mapping_api_version_0_0_1.js +0 -83
  77. package/dist/migrations/mapping_api_version_0_0_2.js +0 -83
  78. package/dist/migrations/mapping_api_version_0_0_3.js +0 -83
  79. package/dist/migrations/mapping_api_version_0_0_4.js +0 -85
  80. package/dist/migrations/mapping_api_version_0_0_5.js +0 -85
  81. package/dist/migrations/spec_version_0_0_2.js +0 -45
  82. package/dist/migrations/spec_version_0_0_3.js +0 -50
  83. package/dist/migrations/util/load-manifest.js +0 -16
  84. package/dist/migrations/util/versions.js +0 -28
  85. package/dist/migrations.js +0 -54
  86. package/dist/protocols/arweave/manifest.graphql +0 -57
  87. package/dist/protocols/arweave/scaffold/manifest.js +0 -18
  88. package/dist/protocols/arweave/scaffold/mapping.js +0 -52
  89. package/dist/protocols/arweave/subgraph.js +0 -23
  90. package/dist/protocols/contract.js +0 -2
  91. package/dist/protocols/cosmos/manifest.graphql +0 -76
  92. package/dist/protocols/cosmos/scaffold/manifest.js +0 -15
  93. package/dist/protocols/cosmos/scaffold/mapping.js +0 -38
  94. package/dist/protocols/cosmos/subgraph.js +0 -28
  95. package/dist/protocols/ethereum/abi.js +0 -138
  96. package/dist/protocols/ethereum/codegen/abi.js +0 -474
  97. package/dist/protocols/ethereum/codegen/abi.test.js +0 -325
  98. package/dist/protocols/ethereum/codegen/template.js +0 -52
  99. package/dist/protocols/ethereum/contract.js +0 -22
  100. package/dist/protocols/ethereum/manifest.graphql +0 -94
  101. package/dist/protocols/ethereum/scaffold/manifest.js +0 -32
  102. package/dist/protocols/ethereum/scaffold/mapping.js +0 -65
  103. package/dist/protocols/ethereum/subgraph.js +0 -172
  104. package/dist/protocols/ethereum/type-generator.js +0 -123
  105. package/dist/protocols/index.js +0 -266
  106. package/dist/protocols/ipfs/codegen/file_template.js +0 -53
  107. package/dist/protocols/near/contract.js +0 -41
  108. package/dist/protocols/near/manifest.graphql +0 -64
  109. package/dist/protocols/near/scaffold/manifest.js +0 -16
  110. package/dist/protocols/near/scaffold/mapping.js +0 -38
  111. package/dist/protocols/near/subgraph.js +0 -23
  112. package/dist/protocols/subgraph.js +0 -2
  113. package/dist/protocols/substreams/manifest.graphql +0 -45
  114. package/dist/protocols/substreams/scaffold/manifest.js +0 -12
  115. package/dist/protocols/substreams/subgraph.js +0 -23
  116. package/dist/scaffold/cosmos.test.js +0 -82
  117. package/dist/scaffold/ethereum.test.js +0 -496
  118. package/dist/scaffold/index.js +0 -133
  119. package/dist/scaffold/mapping.js +0 -57
  120. package/dist/scaffold/near.test.js +0 -85
  121. package/dist/scaffold/schema.js +0 -86
  122. package/dist/scaffold/tests.js +0 -179
  123. package/dist/schema.js +0 -54
  124. package/dist/subgraph.js +0 -243
  125. package/dist/type-generator.js +0 -225
  126. package/dist/validation/contract.js +0 -44
  127. package/dist/validation/index.js +0 -10
  128. package/dist/validation/manifest.js +0 -266
  129. package/dist/validation/schema.js +0 -768
  130. package/dist/validation/schema.test.js +0 -35
  131. package/dist/watcher.js +0 -79
package/CHANGELOG.md CHANGED
@@ -1,15 +1,19 @@
1
1
  # @graphprotocol/graph-cli
2
2
 
3
- ## 0.37.3-alpha-20230113140726-64762bb
3
+ ## 0.37.3
4
4
 
5
5
  ### Patch Changes
6
6
 
7
7
  - [#1020](https://github.com/graphprotocol/graph-cli/pull/1020)
8
8
  [`4c5e452`](https://github.com/graphprotocol/graph-cli/commit/4c5e452cbc2eceb75db29019fb3b4c769c9618f4)
9
9
  Thanks [@saihaj](https://github.com/saihaj)! - dependencies updates:
10
+
10
11
  - Updated dependency [`immutable@4.2.1` ↗︎](https://www.npmjs.com/package/immutable/v/4.2.1) (from
11
12
  `3.8.2`, in `dependencies`)
12
13
 
14
+ - [`d42f199`](https://github.com/graphprotocol/graph-cli/commit/d42f199502df7bac79e8cafc667d5c712f5a3599)
15
+ Thanks [@darienmh](https://github.com/darienmh)! - fix `chapel` network etherscan URL
16
+
13
17
  ## 0.37.2
14
18
 
15
19
  ### Patch Changes
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@graphprotocol/graph-cli",
3
- "version": "0.37.3-alpha-20230113140726-64762bb",
3
+ "version": "0.37.3",
4
4
  "description": "CLI for building for and deploying to The Graph",
5
5
  "license": "(Apache-2.0 OR MIT)",
6
6
  "bin": {
package/src/cli.ts CHANGED
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import { build, system } from 'gluegun';
3
3
 
4
4
  const run = async (argv: string[]) => {
@@ -63,6 +63,8 @@ const getEtherscanLikeAPIUrl = (network: string) => {
63
63
  return `https://api.arbiscan.io/api`;
64
64
  case 'bsc':
65
65
  return `https://api.bscscan.com/api`;
66
+ case 'chapel':
67
+ return `https://api-testnet.bscscan.com/api`;
66
68
  case 'matic':
67
69
  return `https://api.polygonscan.com/api`;
68
70
  case 'mumbai':
@@ -1,6 +1,6 @@
1
- import fs from 'node:fs';
2
- import os from 'node:os';
3
- import path from 'node:path';
1
+ import fs from 'fs';
2
+ import os from 'os';
3
+ import path from 'path';
4
4
  import * as toolbox from 'gluegun';
5
5
  import { normalizeNodeUrl } from './node';
6
6
 
@@ -1,4 +1,4 @@
1
- import { URL } from 'node:url';
1
+ import { URL } from 'url';
2
2
  import * as toolbox from 'gluegun';
3
3
  import ipfsHttpClient from 'ipfs-http-client';
4
4
  import Compiler from '../compiler';
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
 
3
3
  const displayPath = (p: string) => path.relative(process.cwd(), p);
4
4
 
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import * as toolbox from 'gluegun';
3
3
  import yaml from 'yaml';
4
4
  import { initNetworksConfig, updateSubgraphNetwork } from './network';
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import { GluegunFilesystem, GluegunPatching } from 'gluegun';
3
3
  import yaml from 'yaml';
4
4
  import { step, withSpinner } from './spinner';
@@ -1,4 +1,4 @@
1
- import { URL } from 'node:url';
1
+ import { URL } from 'url';
2
2
  import { print } from 'gluegun';
3
3
 
4
4
  const SUBGRAPH_STUDIO_URL = 'https://api.studio.thegraph.com/deploy/';
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import fs from 'fs-extra';
3
3
  import { strings } from 'gluegun';
4
4
  import { Map } from 'immutable';
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import chalk from 'chalk';
3
3
  import { GluegunToolbox } from 'gluegun';
4
4
  import * as DataSourcesExtractor from '../command-helpers/data-sources';
@@ -1,4 +1,4 @@
1
- import { URL } from 'node:url';
1
+ import { URL } from 'url';
2
2
  import chalk from 'chalk';
3
3
  import { GluegunToolbox } from 'gluegun';
4
4
  import { identifyDeployKey as identifyAccessToken } from '../command-helpers/auth';
@@ -1,5 +1,5 @@
1
- import path from 'node:path';
2
- import { URL } from 'node:url';
1
+ import path from 'path';
2
+ import { URL } from 'url';
3
3
  import chalk from 'chalk';
4
4
  import { GluegunToolbox } from 'gluegun';
5
5
  import { identifyDeployKey } from '../command-helpers/auth';
@@ -1,6 +1,6 @@
1
- import fs from 'node:fs';
2
- import os from 'node:os';
3
- import path from 'node:path';
1
+ import fs from 'fs';
2
+ import os from 'os';
3
+ import path from 'path';
4
4
  import chalk from 'chalk';
5
5
  import { GluegunToolbox } from 'gluegun';
6
6
  import * as graphCli from '../cli';
@@ -1,7 +1,7 @@
1
- import { ChildProcess, spawn } from 'node:child_process';
2
- import http from 'node:http';
3
- import net from 'node:net';
4
- import path from 'node:path';
1
+ import { ChildProcess, spawn } from 'child_process';
2
+ import http from 'http';
3
+ import net from 'net';
4
+ import path from 'path';
5
5
  import chalk from 'chalk';
6
6
  import compose from 'docker-compose';
7
7
  import { GluegunToolbox } from 'gluegun';
@@ -1,4 +1,4 @@
1
- import { URL } from 'node:url';
1
+ import { URL } from 'url';
2
2
  import chalk from 'chalk';
3
3
  import { GluegunToolbox } from 'gluegun';
4
4
  import { identifyDeployKey as identifyAccessToken } from '../command-helpers/auth';
@@ -1,6 +1,6 @@
1
- import { exec, spawn } from 'node:child_process';
2
- import os from 'node:os';
3
- import path from 'node:path';
1
+ import { exec, spawn } from 'child_process';
2
+ import os from 'os';
3
+ import path from 'path';
4
4
  import { Binary } from 'binary-install-raw';
5
5
  import chalk from 'chalk';
6
6
  import { filesystem, GluegunToolbox, patching, print, system } from 'gluegun';
@@ -1,5 +1,5 @@
1
- import crypto from 'node:crypto';
2
- import path from 'node:path';
1
+ import crypto from 'crypto';
2
+ import path from 'path';
3
3
  import chalk from 'chalk';
4
4
  import fs from 'fs-extra';
5
5
  import * as toolbox from 'gluegun';
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import fs from 'fs-extra';
3
3
  import yaml from 'js-yaml';
4
4
 
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import fs from 'fs-extra';
3
3
 
4
4
  export async function getGraphTsVersion(sourceDir: string) {
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import fs from 'fs-extra';
3
3
  import immutable from 'immutable';
4
4
  import AbiCodeGenerator from './codegen/abi';
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import fs from 'fs-extra';
3
3
  import immutable from 'immutable';
4
4
  import * as ts from '../../../codegen/typescript';
@@ -1,4 +1,4 @@
1
- import fs from 'node:fs';
1
+ import fs from 'fs';
2
2
  import immutable from 'immutable';
3
3
  import request from 'sync-request';
4
4
  import Web3EthAbi from 'web3-eth-abi';
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import fs from 'fs-extra';
3
3
  import immutable from 'immutable';
4
4
  import prettier from 'prettier';
package/src/subgraph.ts CHANGED
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import fs from 'fs-extra';
3
3
  import * as graphql from 'graphql/language';
4
4
  import immutable from 'immutable';
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  // @ts-expect-error TODO: type out if necessary
3
3
  import uncrashable from '@float-capital/float-subgraph-uncrashable/src/Index.bs.js';
4
4
  import fs from 'fs-extra';
@@ -1,5 +1,5 @@
1
- import fs from 'node:fs';
2
- import path from 'node:path';
1
+ import fs from 'fs';
2
+ import path from 'path';
3
3
  import immutable from 'immutable';
4
4
  import yaml from 'js-yaml';
5
5
  import Protocol from '../protocols';
@@ -1,4 +1,4 @@
1
- import fs from 'node:fs';
1
+ import fs from 'fs';
2
2
  import * as graphql from 'graphql/language';
3
3
  import immutable from 'immutable';
4
4
 
package/src/watcher.ts CHANGED
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import chokidar from 'chokidar';
3
3
 
4
4
  export default class Watcher {
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import { cliTest } from './util';
3
3
 
4
4
  describe('Init', () => {
@@ -1,5 +1,5 @@
1
1
  declare module 'spawn-command' {
2
- import { spawn, SpawnOptions, ChildProcessByStdio } from 'node:child_process';
2
+ import { spawn, SpawnOptions, ChildProcessByStdio } from 'child_process';
3
3
 
4
4
  type SpawnResult = ChildProcessByStdio<Writable, Readable, Readable>;
5
5
  export { SpawnOptions, SpawnResult };
package/tests/cli/util.ts CHANGED
@@ -1,4 +1,4 @@
1
- import path from 'node:path';
1
+ import path from 'path';
2
2
  import fs from 'fs-extra';
3
3
  import spawn from 'spawn-command';
4
4
  import stripAnsi from 'strip-ansi';
package/dist/bin.js DELETED
@@ -1,6 +0,0 @@
1
- #!/usr/bin/env node
2
- "use strict";
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- // run the CLI with the current process arguments
5
- const cli_1 = require("./cli");
6
- (0, cli_1.run)(process.argv);
package/dist/cli.js DELETED
@@ -1,26 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.run = void 0;
7
- const node_path_1 = __importDefault(require("node:path"));
8
- const gluegun_1 = require("gluegun");
9
- const run = async (argv) => {
10
- let builder = (0, gluegun_1.build)().brand('graph').src(__dirname);
11
- const pluginDirs = [];
12
- await Promise.all(['npm root -g', 'npm root', 'yarn global dir'].map(async (cmd) => {
13
- try {
14
- const dir = await gluegun_1.system.run(cmd, { trim: true });
15
- pluginDirs.push(dir);
16
- }
17
- catch (_) {
18
- // noop
19
- }
20
- }));
21
- // Inject potential plugin directories
22
- builder = pluginDirs.reduce((cli, dir) => cli.plugin(node_path_1.default.join(dir, '@graphprotocol', 'indexer-cli', 'dist')), builder);
23
- const cli = builder.help().version().defaultCommand().create();
24
- return await cli.run(argv);
25
- };
26
- exports.run = run;
@@ -1,244 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
- Object.defineProperty(exports, "__esModule", { value: true });
29
- const immutable_1 = __importDefault(require("immutable"));
30
- const typesCodegen = __importStar(require("./types"));
31
- const tsCodegen = __importStar(require("./typescript"));
32
- class IdField {
33
- static BYTES = Symbol('Bytes');
34
- static STRING = Symbol('String');
35
- kind;
36
- constructor(idField) {
37
- const typeName = idField.getIn(['type', 'type', 'name', 'value']);
38
- this.kind = typeName === 'Bytes' ? IdField.BYTES : IdField.STRING;
39
- }
40
- typeName() {
41
- return this.kind === IdField.BYTES ? 'Bytes' : 'string';
42
- }
43
- gqlTypeName() {
44
- return this.kind === IdField.BYTES ? 'Bytes' : 'String';
45
- }
46
- tsNamedType() {
47
- return tsCodegen.namedType(this.typeName());
48
- }
49
- tsValueFrom() {
50
- return this.kind === IdField.BYTES ? 'Value.fromBytes(id)' : 'Value.fromString(id)';
51
- }
52
- tsValueKind() {
53
- return this.kind === IdField.BYTES ? 'ValueKind.BYTES' : 'ValueKind.STRING';
54
- }
55
- tsValueToString() {
56
- return this.kind == IdField.BYTES ? 'id.toBytes().toHexString()' : 'id.toString()';
57
- }
58
- tsToString() {
59
- return this.kind == IdField.BYTES ? 'id.toHexString()' : 'id';
60
- }
61
- static fromFields(fields) {
62
- const idField = fields.find(field => field.getIn(['name', 'value']) === 'id');
63
- return new IdField(idField);
64
- }
65
- static fromTypeDef(def) {
66
- return IdField.fromFields(def.get('fields'));
67
- }
68
- }
69
- class SchemaCodeGenerator {
70
- schema;
71
- constructor(schema) {
72
- this.schema = schema;
73
- this.schema = schema;
74
- }
75
- generateModuleImports() {
76
- return [
77
- tsCodegen.moduleImports([
78
- // Base classes
79
- 'TypedMap',
80
- 'Entity',
81
- 'Value',
82
- 'ValueKind',
83
- // APIs
84
- 'store',
85
- // Basic Scalar types
86
- 'Bytes',
87
- 'BigInt',
88
- 'BigDecimal',
89
- ], '@graphprotocol/graph-ts'),
90
- ];
91
- }
92
- generateTypes() {
93
- return this.schema.ast
94
- .get('definitions')
95
- .filter((def) => this._isEntityTypeDefinition(def))
96
- .map((def) => this._generateEntityType(def));
97
- }
98
- _isEntityTypeDefinition(def) {
99
- return (def.get('kind') === 'ObjectTypeDefinition' &&
100
- def
101
- .get('directives')
102
- .find((directive) => directive.getIn(['name', 'value']) === 'entity') !== undefined);
103
- }
104
- _isInterfaceDefinition(def) {
105
- return def.get('kind') === 'InterfaceTypeDefinition';
106
- }
107
- _generateEntityType(def) {
108
- const name = def.getIn(['name', 'value']);
109
- const klass = tsCodegen.klass(name, { export: true, extends: 'Entity' });
110
- const fields = def.get('fields');
111
- const idField = IdField.fromFields(fields);
112
- // Generate and add a constructor
113
- klass.addMethod(this._generateConstructor(name, fields));
114
- // Generate and add save() and getById() methods
115
- this._generateStoreMethods(name, idField).forEach(method => klass.addMethod(method));
116
- // Generate and add entity field getters and setters
117
- def
118
- .get('fields')
119
- .reduce((methods, field) => methods.concat(this._generateEntityFieldMethods(def, field)), immutable_1.default.List())
120
- .forEach((method) => klass.addMethod(method));
121
- return klass;
122
- }
123
- _generateConstructor(_entityName, fields) {
124
- const idField = IdField.fromFields(fields);
125
- return tsCodegen.method('constructor', [tsCodegen.param('id', idField.tsNamedType())], undefined, `
126
- super()
127
- this.set('id', ${idField.tsValueFrom()})
128
- `);
129
- }
130
- _generateStoreMethods(entityName, idField) {
131
- return immutable_1.default.List.of(tsCodegen.method('save', [], tsCodegen.namedType('void'), `
132
- let id = this.get('id')
133
- assert(id != null,
134
- 'Cannot save ${entityName} entity without an ID')
135
- if (id) {
136
- assert(id.kind == ${idField.tsValueKind()},
137
- \`Entities of type ${entityName} must have an ID of type ${idField.gqlTypeName()} but the id '\${id.displayData()}' is of type \${id.displayKind()}\`)
138
- store.set('${entityName}', ${idField.tsValueToString()}, this)
139
- }`), tsCodegen.staticMethod('load', [tsCodegen.param('id', tsCodegen.namedType(idField.typeName()))], tsCodegen.nullableType(tsCodegen.namedType(entityName)), `
140
- return changetype<${entityName} | null>(store.get('${entityName}', ${idField.tsToString()}))
141
- `));
142
- }
143
- _generateEntityFieldMethods(entityDef, fieldDef) {
144
- return immutable_1.default.List([
145
- this._generateEntityFieldGetter(entityDef, fieldDef),
146
- this._generateEntityFieldSetter(entityDef, fieldDef),
147
- ]);
148
- }
149
- _generateEntityFieldGetter(_entityDef, fieldDef) {
150
- const name = fieldDef.getIn(['name', 'value']);
151
- const gqlType = fieldDef.get('type');
152
- const fieldValueType = this._valueTypeFromGraphQl(gqlType);
153
- const returnType = this._typeFromGraphQl(gqlType);
154
- const isNullable = returnType instanceof tsCodegen.NullableType;
155
- const getNonNullable = `return ${typesCodegen.valueToAsc('value!', fieldValueType)}`;
156
- const getNullable = `if (!value || value.kind == ValueKind.NULL) {
157
- return null
158
- } else {
159
- return ${typesCodegen.valueToAsc('value', fieldValueType)}
160
- }`;
161
- return tsCodegen.method(`get ${name}`, [], returnType, `
162
- let value = this.get('${name}')
163
- ${isNullable ? getNullable : getNonNullable}
164
- `);
165
- }
166
- _generateEntityFieldSetter(_entityDef, fieldDef) {
167
- const name = fieldDef.getIn(['name', 'value']);
168
- const gqlType = fieldDef.get('type');
169
- const fieldValueType = this._valueTypeFromGraphQl(gqlType);
170
- const paramType = this._typeFromGraphQl(gqlType);
171
- const isNullable = paramType instanceof tsCodegen.NullableType;
172
- const paramTypeString = isNullable ? paramType.inner.toString() : paramType.toString();
173
- const isArray = paramType instanceof tsCodegen.ArrayType;
174
- if (isArray && paramType.inner instanceof tsCodegen.NullableType) {
175
- const baseType = this._baseType(gqlType);
176
- throw new Error(`
177
- GraphQL schema can't have List's with Nullable members.
178
- Error in '${name}' field of type '[${baseType}]'.
179
- Suggestion: add an '!' to the member type of the List, change from '[${baseType}]' to '[${baseType}!]'`);
180
- }
181
- const setNonNullable = `
182
- this.set('${name}', ${typesCodegen.valueFromAsc(`value`, fieldValueType)})
183
- `;
184
- const setNullable = `
185
- if (!value) {
186
- this.unset('${name}')
187
- } else {
188
- this.set('${name}', ${typesCodegen.valueFromAsc(`<${paramTypeString}>value`, fieldValueType)})
189
- }
190
- `;
191
- return tsCodegen.method(`set ${name}`, [tsCodegen.param('value', paramType)], undefined, isNullable ? setNullable : setNonNullable);
192
- }
193
- _resolveFieldType(gqlType) {
194
- const typeName = gqlType.getIn(['name', 'value']);
195
- // If this is a reference to another type, the field has the type of
196
- // the referred type's id field
197
- const typeDef = this.schema.ast
198
- .get('definitions')
199
- .find((def) => (this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) &&
200
- def.getIn(['name', 'value']) === typeName);
201
- if (typeDef) {
202
- return IdField.fromTypeDef(typeDef).typeName();
203
- }
204
- return typeName;
205
- }
206
- /** Return the type that values for this field must have. For scalar
207
- * types, that's the type from the subgraph schema. For references to
208
- * other entity types, this is the same as the type of the id of the
209
- * referred type, i.e., `string` or `Bytes`*/
210
- _valueTypeFromGraphQl(gqlType) {
211
- if (gqlType.get('kind') === 'NonNullType') {
212
- return this._valueTypeFromGraphQl(gqlType.get('type'));
213
- }
214
- if (gqlType.get('kind') === 'ListType') {
215
- return '[' + this._valueTypeFromGraphQl(gqlType.get('type')) + ']';
216
- }
217
- return this._resolveFieldType(gqlType);
218
- }
219
- /** Determine the base type of `gqlType` by removing any non-null
220
- * constraints and using the type of elements of lists */
221
- _baseType(gqlType) {
222
- if (gqlType.get('kind') === 'NonNullType') {
223
- return this._baseType(gqlType.get('type'));
224
- }
225
- if (gqlType.get('kind') === 'ListType') {
226
- return this._baseType(gqlType.get('type'));
227
- }
228
- return gqlType.getIn(['name', 'value']);
229
- }
230
- _typeFromGraphQl(gqlType, nullable = true) {
231
- if (gqlType.get('kind') === 'NonNullType') {
232
- return this._typeFromGraphQl(gqlType.get('type'), false);
233
- }
234
- if (gqlType.get('kind') === 'ListType') {
235
- const type = tsCodegen.arrayType(this._typeFromGraphQl(gqlType.get('type')));
236
- return nullable ? tsCodegen.nullableType(type) : type;
237
- }
238
- // NamedType
239
- const type = tsCodegen.namedType(typesCodegen.ascTypeForValue(this._resolveFieldType(gqlType)));
240
- // In AssemblyScript, primitives cannot be nullable.
241
- return nullable && !type.isPrimitive() ? tsCodegen.nullableType(type) : type;
242
- }
243
- }
244
- exports.default = SchemaCodeGenerator;