@graphprotocol/graph-cli 0.37.2 → 0.37.3-alpha-20230110163550-d64bef6

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 (206) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/babel.config.js +3 -0
  3. package/dist/bin.js +6 -0
  4. package/dist/cli.js +32 -0
  5. package/dist/codegen/schema.js +250 -0
  6. package/dist/codegen/schema.test.js +458 -0
  7. package/dist/codegen/template.js +65 -0
  8. package/dist/codegen/types/conversions.js +359 -0
  9. package/dist/codegen/types/index.js +71 -0
  10. package/dist/codegen/types/index.test.js +563 -0
  11. package/dist/codegen/typescript.js +200 -0
  12. package/dist/codegen/util.js +46 -0
  13. package/dist/codegen/util.test.js +93 -0
  14. package/dist/command-helpers/abi.js +78 -0
  15. package/dist/command-helpers/auth.js +77 -0
  16. package/dist/command-helpers/compiler.js +66 -0
  17. package/dist/command-helpers/data-sources.js +29 -0
  18. package/dist/command-helpers/fs.js +9 -0
  19. package/dist/command-helpers/gluegun.js +49 -0
  20. package/dist/command-helpers/ipfs.js +5 -0
  21. package/dist/command-helpers/jsonrpc.js +29 -0
  22. package/dist/command-helpers/network.js +93 -0
  23. package/dist/command-helpers/network.test.js +86 -0
  24. package/dist/command-helpers/node.js +40 -0
  25. package/dist/command-helpers/scaffold.js +112 -0
  26. package/dist/command-helpers/spinner.js +84 -0
  27. package/dist/command-helpers/studio.js +13 -0
  28. package/dist/command-helpers/studio.test.js +41 -0
  29. package/dist/command-helpers/subgraph.js +24 -0
  30. package/dist/command-helpers/version.js +74 -0
  31. package/dist/command-helpers/version.test.js +179 -0
  32. package/dist/commands/add.js +190 -0
  33. package/dist/commands/auth.js +127 -0
  34. package/dist/commands/build.js +140 -0
  35. package/dist/commands/codegen.js +135 -0
  36. package/dist/commands/create.js +86 -0
  37. package/dist/commands/deploy.js +334 -0
  38. package/dist/commands/init.js +788 -0
  39. package/dist/commands/local.js +387 -0
  40. package/dist/commands/remove.js +86 -0
  41. package/dist/commands/test.js +295 -0
  42. package/dist/compiler/asc.js +83 -0
  43. package/dist/compiler/index.js +474 -0
  44. package/dist/debug.js +16 -0
  45. package/dist/migrations/mapping_api_version_0_0_1.js +78 -0
  46. package/dist/migrations/mapping_api_version_0_0_2.js +76 -0
  47. package/dist/migrations/mapping_api_version_0_0_3.js +78 -0
  48. package/dist/migrations/mapping_api_version_0_0_4.js +80 -0
  49. package/dist/migrations/mapping_api_version_0_0_5.js +80 -0
  50. package/dist/migrations/spec_version_0_0_2.js +49 -0
  51. package/dist/migrations/spec_version_0_0_3.js +54 -0
  52. package/dist/migrations/util/load-manifest.js +18 -0
  53. package/dist/migrations/util/versions.js +29 -0
  54. package/dist/migrations.js +56 -0
  55. package/dist/protocols/arweave/manifest.graphql +57 -0
  56. package/dist/protocols/arweave/scaffold/manifest.js +18 -0
  57. package/dist/protocols/arweave/scaffold/mapping.js +52 -0
  58. package/dist/protocols/arweave/subgraph.js +23 -0
  59. package/dist/protocols/cosmos/manifest.graphql +76 -0
  60. package/dist/protocols/cosmos/scaffold/manifest.js +15 -0
  61. package/dist/protocols/cosmos/scaffold/mapping.js +38 -0
  62. package/dist/protocols/cosmos/subgraph.js +25 -0
  63. package/dist/protocols/ethereum/abi.js +140 -0
  64. package/dist/protocols/ethereum/codegen/abi.js +451 -0
  65. package/dist/protocols/ethereum/codegen/abi.test.js +328 -0
  66. package/dist/protocols/ethereum/codegen/template.js +50 -0
  67. package/dist/protocols/ethereum/contract.js +20 -0
  68. package/dist/protocols/ethereum/manifest.graphql +94 -0
  69. package/dist/protocols/ethereum/scaffold/manifest.js +32 -0
  70. package/dist/protocols/ethereum/scaffold/mapping.js +65 -0
  71. package/dist/protocols/ethereum/subgraph.js +171 -0
  72. package/dist/protocols/ethereum/type-generator.js +125 -0
  73. package/dist/protocols/index.js +268 -0
  74. package/dist/protocols/ipfs/codegen/file_template.js +50 -0
  75. package/dist/protocols/near/contract.js +41 -0
  76. package/dist/protocols/near/manifest.graphql +64 -0
  77. package/dist/protocols/near/scaffold/manifest.js +16 -0
  78. package/dist/protocols/near/scaffold/mapping.js +38 -0
  79. package/dist/protocols/near/subgraph.js +20 -0
  80. package/dist/protocols/subgraph.js +2 -0
  81. package/dist/protocols/substreams/manifest.graphql +45 -0
  82. package/dist/protocols/substreams/scaffold/manifest.js +12 -0
  83. package/dist/protocols/substreams/subgraph.js +23 -0
  84. package/dist/scaffold/cosmos.test.js +83 -0
  85. package/dist/scaffold/ethereum.test.js +505 -0
  86. package/dist/scaffold/index.js +128 -0
  87. package/dist/scaffold/mapping.js +61 -0
  88. package/dist/scaffold/near.test.js +86 -0
  89. package/dist/scaffold/schema.js +83 -0
  90. package/dist/scaffold/tests.js +175 -0
  91. package/dist/schema.js +54 -0
  92. package/dist/subgraph.js +249 -0
  93. package/dist/type-generator.js +224 -0
  94. package/dist/validation/contract.js +46 -0
  95. package/dist/validation/index.js +8 -0
  96. package/dist/validation/manifest.js +271 -0
  97. package/dist/validation/schema.js +796 -0
  98. package/dist/validation/schema.test.js +35 -0
  99. package/dist/watcher.js +79 -0
  100. package/jest.config.js +1 -1
  101. package/package.json +16 -9
  102. package/{bin/graph → src/bin.ts} +2 -1
  103. package/src/cli.ts +36 -0
  104. package/src/codegen/schema.js +23 -22
  105. package/src/codegen/schema.test.js +33 -27
  106. package/src/codegen/{template.js → template.ts} +8 -6
  107. package/src/codegen/types/conversions.ts +382 -0
  108. package/src/codegen/types/index.js +4 -7
  109. package/src/codegen/types/index.test.js +10 -4
  110. package/src/codegen/{typescript.js → typescript.ts} +44 -26
  111. package/src/codegen/util.js +1 -1
  112. package/src/codegen/util.test.js +5 -2
  113. package/src/command-helpers/abi.js +46 -30
  114. package/src/command-helpers/auth.js +6 -9
  115. package/src/command-helpers/compiler.js +4 -6
  116. package/src/command-helpers/data-sources.js +8 -11
  117. package/src/command-helpers/fs.ts +5 -0
  118. package/src/command-helpers/gluegun.js +2 -4
  119. package/src/command-helpers/ipfs.ts +3 -0
  120. package/src/command-helpers/{jsonrpc.js → jsonrpc.ts} +11 -16
  121. package/src/command-helpers/network.js +58 -44
  122. package/src/command-helpers/network.test.js +23 -19
  123. package/src/command-helpers/node.js +3 -7
  124. package/src/command-helpers/scaffold.js +34 -25
  125. package/src/command-helpers/{spinner.js → spinner.ts} +10 -10
  126. package/src/command-helpers/studio.test.js +61 -30
  127. package/src/command-helpers/studio.ts +20 -0
  128. package/src/command-helpers/{subgraph.js → subgraph.ts} +6 -6
  129. package/src/command-helpers/version.js +11 -16
  130. package/src/command-helpers/version.test.js +111 -82
  131. package/src/commands/add.js +49 -35
  132. package/src/commands/auth.js +13 -25
  133. package/src/commands/build.js +9 -9
  134. package/src/commands/codegen.js +12 -12
  135. package/src/commands/create.js +22 -22
  136. package/src/commands/deploy.js +39 -34
  137. package/src/commands/init.js +21 -24
  138. package/src/commands/local.js +15 -14
  139. package/src/commands/remove.js +22 -22
  140. package/src/commands/test.js +86 -70
  141. package/src/compiler/{asc.js → asc.ts} +29 -16
  142. package/src/compiler/index.js +18 -18
  143. package/src/{debug.js → debug.ts} +2 -2
  144. package/src/migrations/mapping_api_version_0_0_1.js +7 -7
  145. package/src/migrations/mapping_api_version_0_0_2.js +5 -5
  146. package/src/migrations/mapping_api_version_0_0_3.js +7 -7
  147. package/src/migrations/mapping_api_version_0_0_4.js +7 -7
  148. package/src/migrations/mapping_api_version_0_0_5.js +7 -7
  149. package/src/migrations/spec_version_0_0_2.js +5 -5
  150. package/src/migrations/spec_version_0_0_3.js +5 -5
  151. package/src/migrations/util/load-manifest.js +6 -9
  152. package/src/migrations/util/versions.js +5 -10
  153. package/src/{migrations.js → migrations.ts} +14 -15
  154. package/src/protocols/arweave/scaffold/manifest.js +1 -4
  155. package/src/protocols/arweave/scaffold/mapping.js +1 -3
  156. package/src/protocols/arweave/subgraph.ts +22 -0
  157. package/src/protocols/cosmos/scaffold/manifest.js +1 -4
  158. package/src/protocols/cosmos/scaffold/mapping.js +1 -3
  159. package/src/protocols/cosmos/subgraph.js +2 -2
  160. package/src/protocols/ethereum/abi.js +7 -13
  161. package/src/protocols/ethereum/codegen/abi.js +219 -222
  162. package/src/protocols/ethereum/codegen/abi.test.js +6 -6
  163. package/src/protocols/ethereum/codegen/template.js +3 -5
  164. package/src/protocols/ethereum/contract.js +3 -2
  165. package/src/protocols/ethereum/scaffold/manifest.js +4 -7
  166. package/src/protocols/ethereum/scaffold/mapping.js +4 -11
  167. package/src/protocols/ethereum/subgraph.js +21 -20
  168. package/src/protocols/ethereum/type-generator.js +24 -30
  169. package/src/protocols/{index.js → index.ts} +65 -46
  170. package/src/protocols/ipfs/codegen/file_template.js +2 -2
  171. package/src/protocols/near/{contract.js → contract.ts} +16 -12
  172. package/src/protocols/near/scaffold/manifest.js +2 -5
  173. package/src/protocols/near/scaffold/mapping.js +1 -3
  174. package/src/protocols/near/subgraph.js +3 -6
  175. package/src/protocols/subgraph.ts +12 -0
  176. package/src/protocols/substreams/scaffold/{manifest.js → manifest.ts} +2 -7
  177. package/src/protocols/substreams/subgraph.ts +22 -0
  178. package/src/scaffold/cosmos.test.js +3 -3
  179. package/src/scaffold/ethereum.test.js +4 -4
  180. package/src/scaffold/index.js +7 -7
  181. package/src/scaffold/mapping.js +3 -3
  182. package/src/scaffold/near.test.js +3 -3
  183. package/src/scaffold/schema.js +4 -4
  184. package/src/scaffold/tests.js +72 -58
  185. package/src/schema.ts +26 -0
  186. package/src/{subgraph.js → subgraph.ts} +80 -60
  187. package/src/type-generator.js +20 -20
  188. package/src/validation/contract.js +2 -6
  189. package/src/validation/index.js +1 -1
  190. package/src/validation/manifest.js +24 -22
  191. package/src/validation/schema.test.js +1 -1
  192. package/src/validation/{schema.js → schema.ts} +268 -221
  193. package/src/{watcher.js → watcher.ts} +23 -12
  194. package/tests/cli/globalSetup.js +2 -2
  195. package/tests/cli/globalTeardown.js +2 -2
  196. package/tests/cli/init.test.js +2 -2
  197. package/tests/cli/util.js +7 -13
  198. package/tsconfig.json +18 -0
  199. package/src/cli.js +0 -38
  200. package/src/codegen/types/conversions.js +0 -329
  201. package/src/command-helpers/fs.js +0 -8
  202. package/src/command-helpers/ipfs.js +0 -6
  203. package/src/command-helpers/studio.js +0 -15
  204. package/src/protocols/arweave/subgraph.js +0 -20
  205. package/src/protocols/substreams/subgraph.js +0 -17
  206. package/src/schema.js +0 -23
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const schema_1 = require("./schema");
4
+ describe('Schema validation', () => {
5
+ test('Type suggestions', () => {
6
+ expect((0, schema_1.typeSuggestion)('Address')).toEqual('Bytes');
7
+ expect((0, schema_1.typeSuggestion)('address')).toEqual('Bytes');
8
+ expect((0, schema_1.typeSuggestion)('bytes')).toEqual('Bytes');
9
+ expect((0, schema_1.typeSuggestion)('string')).toEqual('String');
10
+ expect((0, schema_1.typeSuggestion)('bool')).toEqual('Boolean');
11
+ expect((0, schema_1.typeSuggestion)('boolean')).toEqual('Boolean');
12
+ expect((0, schema_1.typeSuggestion)('float')).toEqual('BigDecimal');
13
+ expect((0, schema_1.typeSuggestion)('Float')).toEqual('BigDecimal');
14
+ expect((0, schema_1.typeSuggestion)(`int`)).toBe('Int');
15
+ expect((0, schema_1.typeSuggestion)(`uint`)).toBe('BigInt');
16
+ expect((0, schema_1.typeSuggestion)(`uint32`)).toBe('BigInt');
17
+ // Test i8..i32, int8..int32
18
+ for (let i = 8; i <= 32; i += 8) {
19
+ expect((0, schema_1.typeSuggestion)(`i${i}`)).toBe('Int');
20
+ expect((0, schema_1.typeSuggestion)(`int${i}`)).toBe('Int');
21
+ }
22
+ // Test u8..u24, uint8..uint24
23
+ for (let i = 8; i <= 24; i += 8) {
24
+ expect((0, schema_1.typeSuggestion)(`u${i}`)).toBe('Int');
25
+ expect((0, schema_1.typeSuggestion)(`uint${i}`)).toBe('Int');
26
+ }
27
+ // Test i40..i256, int40..int256, u40..u256, uint40..uint256
28
+ for (let i = 40; i <= 256; i += 8) {
29
+ expect((0, schema_1.typeSuggestion)(`i${i}`)).toBe('BigInt');
30
+ expect((0, schema_1.typeSuggestion)(`int${i}`)).toBe('BigInt');
31
+ expect((0, schema_1.typeSuggestion)(`u${i}`)).toBe('BigInt');
32
+ expect((0, schema_1.typeSuggestion)(`uint${i}`)).toBe('BigInt');
33
+ }
34
+ });
35
+ });
@@ -0,0 +1,79 @@
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
+ const chokidar_1 = __importDefault(require("chokidar"));
7
+ const path_1 = __importDefault(require("path"));
8
+ class Watcher {
9
+ onReady;
10
+ onTrigger;
11
+ onCollectFiles;
12
+ onError;
13
+ watcher;
14
+ constructor(options) {
15
+ const { onReady, onTrigger, onCollectFiles, onError } = options;
16
+ this.onReady = onReady;
17
+ this.onTrigger = onTrigger;
18
+ this.onCollectFiles = onCollectFiles;
19
+ this.onError = onError;
20
+ }
21
+ async watch() {
22
+ // Collect files to watch
23
+ let files = await this.onCollectFiles();
24
+ // Initialize watcher
25
+ this.watcher = chokidar_1.default.watch(files, {
26
+ persistent: true,
27
+ ignoreInitial: true,
28
+ atomic: 500,
29
+ });
30
+ // Bind variables locally
31
+ let watcher = this.watcher;
32
+ let onTrigger = this.onTrigger;
33
+ let onCollectFiles = this.onCollectFiles;
34
+ let onError = this.onError;
35
+ let onReady = this.onReady;
36
+ watcher.on('ready', async () => {
37
+ // Notify listeners that we're watching
38
+ onReady();
39
+ // Trigger once when ready
40
+ await onTrigger(undefined);
41
+ });
42
+ watcher.on('error', (error) => {
43
+ onError(error);
44
+ });
45
+ watcher.on('all', async (_, file) => {
46
+ try {
47
+ // Collect watch all new files to watch
48
+ let newFiles = await onCollectFiles();
49
+ // Collect watched files, if there are any
50
+ let watchedFiles = [];
51
+ let watched = watcher.getWatched();
52
+ watchedFiles = Object.keys(watched).reduce((files, dirname) => watched[dirname].reduce((files, filename) => {
53
+ files.push(path_1.default.resolve(path_1.default.join(dirname, filename)));
54
+ return files;
55
+ }, files), []);
56
+ let diff = (xs, ys) => ({
57
+ added: ys.filter((y) => xs.indexOf(y) < 0),
58
+ removed: xs.filter((x) => ys.indexOf(x) < 0),
59
+ });
60
+ // Diff previously watched files and new files; then remove and
61
+ // add files from/to the watcher accordingly
62
+ let filesDiff = diff(watchedFiles, newFiles);
63
+ watcher.unwatch(filesDiff.removed);
64
+ watcher.add(filesDiff.added);
65
+ // Run the trigger callback
66
+ await onTrigger(file);
67
+ }
68
+ catch (e) {
69
+ onError(e);
70
+ }
71
+ });
72
+ }
73
+ close() {
74
+ if (this.watcher !== undefined) {
75
+ this.watcher.close();
76
+ }
77
+ }
78
+ }
79
+ exports.default = Watcher;
package/jest.config.js CHANGED
@@ -136,7 +136,7 @@ module.exports = {
136
136
  testMatch: ['**/?(*.)+(spec|test).js?(x)'],
137
137
 
138
138
  // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped
139
- testPathIgnorePatterns: ['/src/commands/test.js', '/node_modules/'],
139
+ testPathIgnorePatterns: ['/src/commands/test.js', '/node_modules/', '/dist/'],
140
140
 
141
141
  // The regexp pattern Jest uses to detect test files
142
142
  // testRegex: "",
package/package.json CHANGED
@@ -1,9 +1,14 @@
1
1
  {
2
2
  "name": "@graphprotocol/graph-cli",
3
- "version": "0.37.2",
3
+ "version": "0.37.3-alpha-20230110163550-d64bef6",
4
4
  "license": "(Apache-2.0 OR MIT)",
5
5
  "description": "CLI for building for and deploying to The Graph",
6
+ "main": "dist/cli.js",
7
+ "bin": {
8
+ "graph": "dist/bin.js"
9
+ },
6
10
  "dependencies": {
11
+ "@float-capital/float-subgraph-uncrashable": "^0.0.0-alpha.4",
7
12
  "assemblyscript": "0.19.23",
8
13
  "binary-install-raw": "0.0.13",
9
14
  "chalk": "3.0.0",
@@ -11,12 +16,11 @@
11
16
  "debug": "4.3.1",
12
17
  "docker-compose": "0.23.4",
13
18
  "dockerode": "2.5.8",
14
- "@float-capital/float-subgraph-uncrashable": "^0.0.0-alpha.4",
15
19
  "fs-extra": "9.0.0",
16
20
  "glob": "7.1.6",
17
21
  "gluegun": "https://github.com/edgeandnode/gluegun#v4.3.1-pin-colors-dep",
18
22
  "graphql": "15.5.0",
19
- "immutable": "3.8.2",
23
+ "immutable": "4.2.1",
20
24
  "ipfs-http-client": "34.0.0",
21
25
  "jayson": "3.6.6",
22
26
  "js-yaml": "3.13.1",
@@ -31,15 +35,16 @@
31
35
  "which": "2.0.2",
32
36
  "yaml": "1.9.2"
33
37
  },
34
- "bin": {
35
- "graph": "bin/graph"
36
- },
37
38
  "devDependencies": {
38
- "eslint": "6.8.0",
39
+ "@types/debug": "^4.1.7",
40
+ "@types/fs-extra": "^9.0.13",
41
+ "@types/which": "^2.0.1",
42
+ "copyfiles": "^2.4.1",
39
43
  "jest": "26.0.0",
40
44
  "spawn-command": "0.0.2-1",
41
45
  "strip-ansi": "6.0.0",
42
- "tern": "0.24.3"
46
+ "tern": "0.24.3",
47
+ "typescript": "^4.9.4"
43
48
  },
44
49
  "publishConfig": {
45
50
  "access": "public"
@@ -47,6 +52,8 @@
47
52
  "scripts": {
48
53
  "test": "jest --verbose",
49
54
  "test:init": "jest tests/cli/init.test.js --verbose",
50
- "test:validation": "jest tests/cli/validation.test.js --verbose"
55
+ "test:validation": "jest tests/cli/validation.test.js --verbose",
56
+ "type-check": "tsc --noEmit",
57
+ "build": "tsc --build && copyfiles -u 1 src/**/*.graphql dist/ && chmod +x ./dist/bin.js"
51
58
  }
52
59
  }
@@ -1,4 +1,5 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // run the CLI with the current process arguments
4
- require('../src/cli').run(process.argv)
4
+ import { run } from './cli'
5
+ run(process.argv)
package/src/cli.ts ADDED
@@ -0,0 +1,36 @@
1
+ import path from 'path'
2
+ import { build, system } from 'gluegun'
3
+
4
+ const run = async (argv: string[]) => {
5
+ let builder = build()
6
+ .brand('graph')
7
+ .src(__dirname)
8
+
9
+ const pluginDirs: string[] = []
10
+ await Promise.all(
11
+ ['npm root -g', 'npm root', 'yarn global dir'].map(async cmd => {
12
+ try {
13
+ const dir = await system.run(cmd, { trim: true })
14
+ pluginDirs.push(dir)
15
+ } catch (_) {
16
+ // noop
17
+ }
18
+ }),
19
+ )
20
+
21
+ // Inject potential plugin directories
22
+ builder = pluginDirs.reduce(
23
+ (cli, dir) => cli.plugin(path.join(dir, '@graphprotocol', 'indexer-cli', 'dist')),
24
+ builder,
25
+ )
26
+
27
+ const cli = builder
28
+ .help()
29
+ .version()
30
+ .defaultCommand()
31
+ .create()
32
+
33
+ return await cli.run(argv)
34
+ }
35
+
36
+ export { run }
@@ -1,25 +1,25 @@
1
- const immutable = require('immutable')
1
+ import immutable from 'immutable'
2
2
 
3
- const tsCodegen = require('./typescript')
4
- const typesCodegen = require('./types')
3
+ import * as tsCodegen from './typescript'
4
+ import * as typesCodegen from './types'
5
5
 
6
6
  const List = immutable.List
7
7
 
8
8
  class IdField {
9
- static BYTES = Symbol("Bytes")
10
- static STRING = Symbol("String")
9
+ static BYTES = Symbol('Bytes')
10
+ static STRING = Symbol('String')
11
11
 
12
12
  constructor(idField) {
13
13
  const typeName = idField.getIn(['type', 'type', 'name', 'value'])
14
- this.kind = typeName === "Bytes" ? IdField.BYTES : IdField.STRING
14
+ this.kind = typeName === 'Bytes' ? IdField.BYTES : IdField.STRING
15
15
  }
16
16
 
17
17
  typeName() {
18
- return this.kind === IdField.BYTES ? "Bytes" : "string"
18
+ return this.kind === IdField.BYTES ? 'Bytes' : 'string'
19
19
  }
20
20
 
21
21
  gqlTypeName() {
22
- return this.kind === IdField.BYTES ? "Bytes" : "String"
22
+ return this.kind === IdField.BYTES ? 'Bytes' : 'String'
23
23
  }
24
24
 
25
25
  tsNamedType() {
@@ -27,19 +27,19 @@ class IdField {
27
27
  }
28
28
 
29
29
  tsValueFrom() {
30
- return this.kind === IdField.BYTES ? "Value.fromBytes(id)" : "Value.fromString(id)"
30
+ return this.kind === IdField.BYTES ? 'Value.fromBytes(id)' : 'Value.fromString(id)'
31
31
  }
32
32
 
33
33
  tsValueKind() {
34
- return this.kind === IdField.BYTES ? "ValueKind.BYTES" : "ValueKind.STRING"
34
+ return this.kind === IdField.BYTES ? 'ValueKind.BYTES' : 'ValueKind.STRING'
35
35
  }
36
36
 
37
37
  tsValueToString() {
38
- return this.kind == IdField.BYTES ? "id.toBytes().toHexString()" : "id.toString()"
38
+ return this.kind == IdField.BYTES ? 'id.toBytes().toHexString()' : 'id.toString()'
39
39
  }
40
40
 
41
41
  tsToString() {
42
- return this.kind == IdField.BYTES ? "id.toHexString()" : "id"
42
+ return this.kind == IdField.BYTES ? 'id.toHexString()' : 'id'
43
43
  }
44
44
 
45
45
  static fromFields(fields) {
@@ -48,11 +48,11 @@ class IdField {
48
48
  }
49
49
 
50
50
  static fromTypeDef(def) {
51
- return IdField.fromFields(def.get("fields"))
51
+ return IdField.fromFields(def.get('fields'))
52
52
  }
53
53
  }
54
54
 
55
- module.exports = class SchemaCodeGenerator {
55
+ export default class SchemaCodeGenerator {
56
56
  constructor(schema) {
57
57
  this.schema = schema
58
58
  }
@@ -206,17 +206,13 @@ module.exports = class SchemaCodeGenerator {
206
206
  let paramTypeString = isNullable ? paramType.inner.toString() : paramType.toString()
207
207
  let isArray = paramType instanceof tsCodegen.ArrayType
208
208
 
209
- if (
210
- isArray &&
211
- paramType.inner instanceof tsCodegen.NullableType
212
- ) {
209
+ if (isArray && paramType.inner instanceof tsCodegen.NullableType) {
213
210
  let baseType = this._baseType(gqlType)
214
211
 
215
212
  throw new Error(`
216
213
  GraphQL schema can't have List's with Nullable members.
217
214
  Error in '${name}' field of type '[${baseType}]'.
218
- Suggestion: add an '!' to the member type of the List, change from '[${baseType}]' to '[${baseType}!]'`
219
- )
215
+ Suggestion: add an '!' to the member type of the List, change from '[${baseType}]' to '[${baseType}!]'`)
220
216
  }
221
217
 
222
218
  let setNonNullable = `
@@ -246,8 +242,13 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
246
242
 
247
243
  // If this is a reference to another type, the field has the type of
248
244
  // the referred type's id field
249
- const typeDef = this.schema.ast.get("definitions").
250
- find(def => (this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) && def.getIn(["name", "value"]) === typeName)
245
+ const typeDef = this.schema.ast
246
+ .get('definitions')
247
+ .find(
248
+ def =>
249
+ (this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) &&
250
+ def.getIn(['name', 'value']) === typeName,
251
+ )
251
252
  if (typeDef) {
252
253
  return IdField.fromTypeDef(typeDef).typeName()
253
254
  } else {
@@ -1,7 +1,7 @@
1
- const prettier = require('prettier')
2
- const graphql = require('graphql/language')
3
- const immutable = require('immutable')
4
- const SchemaCodeGenerator = require('./schema')
1
+ import prettier from 'prettier'
2
+ import * as graphql from 'graphql/language'
3
+ import immutable from 'immutable'
4
+ import SchemaCodeGenerator from './schema'
5
5
  const {
6
6
  Class,
7
7
  Method,
@@ -12,11 +12,7 @@ const {
12
12
  ArrayType,
13
13
  } = require('./typescript')
14
14
 
15
- const formatTS = code =>
16
- prettier.format(
17
- code,
18
- { parser: 'typescript', semi: false }
19
- )
15
+ const formatTS = code => prettier.format(code, { parser: 'typescript', semi: false })
20
16
 
21
17
  const createSchemaCodeGen = schema =>
22
18
  new SchemaCodeGenerator({
@@ -248,7 +244,12 @@ describe('Schema code generator', () => {
248
244
  },
249
245
  {
250
246
  name: 'set wallets',
251
- params: [new Param('value', new NullableType(new ArrayType(new NamedType('string'))))],
247
+ params: [
248
+ new Param(
249
+ 'value',
250
+ new NullableType(new ArrayType(new NamedType('string'))),
251
+ ),
252
+ ],
252
253
  returnType: undefined,
253
254
  body: `
254
255
  if (!value) {
@@ -377,20 +378,21 @@ describe('Schema code generator', () => {
377
378
 
378
379
  const generatedTypes = codegen.generateTypes()
379
380
  testEntity(generatedTypes, {
380
- name: "Task",
381
+ name: 'Task',
381
382
  members: [],
382
383
  methods: [
383
384
  {
384
385
  name: 'constructor',
385
386
  params: [new Param('id', new NamedType('Bytes'))],
386
387
  returnType: undefined,
387
- body: "\n super()\n this.set('id', Value.fromBytes(id))\n "
388
+ body: "\n super()\n this.set('id', Value.fromBytes(id))\n ",
388
389
  },
389
390
  {
390
391
  name: 'save',
391
392
  params: [],
392
393
  returnType: new NamedType('void'),
393
- body: '\n' +
394
+ body:
395
+ '\n' +
394
396
  " let id = this.get('id')\n" +
395
397
  ' assert(id != null,\n' +
396
398
  " 'Cannot save Task entity without an ID')\n" +
@@ -398,63 +400,67 @@ describe('Schema code generator', () => {
398
400
  ' assert(id.kind == ValueKind.BYTES,\n' +
399
401
  " `Entities of type Task must have an ID of type Bytes but the id '${id.displayData()}' is of type ${id.displayKind()}`)\n" +
400
402
  " store.set('Task', id.toBytes().toHexString(), this)\n" +
401
- ' }'
403
+ ' }',
402
404
  },
403
405
  {
404
406
  name: 'load',
405
407
  static: true,
406
408
  params: [new Param('id', new NamedType('Bytes'))],
407
409
  returnType: new NullableType(new NamedType('Task')),
408
- body: '\n' +
410
+ body:
411
+ '\n' +
409
412
  " return changetype<Task | null>(store.get('Task', id.toHexString()))\n" +
410
- ' '
413
+ ' ',
411
414
  },
412
415
  {
413
416
  name: 'get id',
414
417
  params: [],
415
418
  returnType: new NamedType('Bytes'),
416
- body: '\n' +
419
+ body:
420
+ '\n' +
417
421
  " let value = this.get('id')\n" +
418
422
  ' return value!.toBytes()\n' +
419
- ' '
423
+ ' ',
420
424
  },
421
425
  {
422
426
  name: 'set id',
423
427
  params: [new Param('value', new NamedType('Bytes'))],
424
428
  returnType: undefined,
425
- body: "\n this.set('id', Value.fromBytes(value))\n "
429
+ body: "\n this.set('id', Value.fromBytes(value))\n ",
426
430
  },
427
431
  {
428
432
  name: 'get employee',
429
433
  params: [],
430
434
  returnType: new NamedType('Bytes'),
431
- body: '\n' +
435
+ body:
436
+ '\n' +
432
437
  " let value = this.get('employee')\n" +
433
438
  ' return value!.toBytes()\n' +
434
- ' '
439
+ ' ',
435
440
  },
436
441
  {
437
442
  name: 'set employee',
438
443
  params: [new Param('value', new NamedType('Bytes'))],
439
444
  returnType: undefined,
440
- body: "\n this.set('employee', Value.fromBytes(value))\n "
445
+ body: "\n this.set('employee', Value.fromBytes(value))\n ",
441
446
  },
442
447
  {
443
448
  name: 'get worker',
444
449
  params: [],
445
450
  returnType: new NamedType('Bytes'),
446
- body: '\n' +
451
+ body:
452
+ '\n' +
447
453
  " let value = this.get('worker')\n" +
448
454
  ' return value!.toBytes()\n' +
449
- ' '
455
+ ' ',
450
456
  },
451
457
  {
452
458
  name: 'set worker',
453
459
  params: [new Param('value', new NamedType('Bytes'))],
454
460
  returnType: undefined,
455
- body: "\n this.set('worker', Value.fromBytes(value))\n "
456
- }
457
- ]
461
+ body: "\n this.set('worker', Value.fromBytes(value))\n ",
462
+ },
463
+ ],
458
464
  })
459
465
  })
460
466
  })
@@ -1,16 +1,18 @@
1
- const immutable = require('immutable')
2
- const IpfsFileTemplateCodeGen = require('../protocols/ipfs/codegen/file_template')
1
+ import immutable from 'immutable'
2
+ import IpfsFileTemplateCodeGen from '../protocols/ipfs/codegen/file_template'
3
3
 
4
- const tsCodegen = require('./typescript')
4
+ import * as tsCodegen from './typescript'
5
5
 
6
- module.exports = class DataSourceTemplateCodeGenerator {
7
- constructor(template, protocol) {
6
+ export default class DataSourceTemplateCodeGenerator {
7
+ template: any
8
+ protocolTemplateCodeGen: any
9
+ constructor(template: any, protocol: any) {
8
10
  this.template = template
9
11
  let kind = template.get('kind')
10
12
 
11
13
  if (kind.split('/')[0] == protocol.name) {
12
14
  this.protocolTemplateCodeGen = protocol.getTemplateCodeGen(template)
13
- } else if (kind == "file/ipfs") {
15
+ } else if (kind == 'file/ipfs') {
14
16
  this.protocolTemplateCodeGen = new IpfsFileTemplateCodeGen(template)
15
17
  }
16
18
  }