@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.
- package/CHANGELOG.md +7 -0
- package/babel.config.js +3 -0
- package/dist/bin.js +6 -0
- package/dist/cli.js +32 -0
- package/dist/codegen/schema.js +250 -0
- package/dist/codegen/schema.test.js +458 -0
- package/dist/codegen/template.js +65 -0
- package/dist/codegen/types/conversions.js +359 -0
- package/dist/codegen/types/index.js +71 -0
- package/dist/codegen/types/index.test.js +563 -0
- package/dist/codegen/typescript.js +200 -0
- package/dist/codegen/util.js +46 -0
- package/dist/codegen/util.test.js +93 -0
- package/dist/command-helpers/abi.js +78 -0
- package/dist/command-helpers/auth.js +77 -0
- package/dist/command-helpers/compiler.js +66 -0
- package/dist/command-helpers/data-sources.js +29 -0
- package/dist/command-helpers/fs.js +9 -0
- package/dist/command-helpers/gluegun.js +49 -0
- package/dist/command-helpers/ipfs.js +5 -0
- package/dist/command-helpers/jsonrpc.js +29 -0
- package/dist/command-helpers/network.js +93 -0
- package/dist/command-helpers/network.test.js +86 -0
- package/dist/command-helpers/node.js +40 -0
- package/dist/command-helpers/scaffold.js +112 -0
- package/dist/command-helpers/spinner.js +84 -0
- package/dist/command-helpers/studio.js +13 -0
- package/dist/command-helpers/studio.test.js +41 -0
- package/dist/command-helpers/subgraph.js +24 -0
- package/dist/command-helpers/version.js +74 -0
- package/dist/command-helpers/version.test.js +179 -0
- package/dist/commands/add.js +190 -0
- package/dist/commands/auth.js +127 -0
- package/dist/commands/build.js +140 -0
- package/dist/commands/codegen.js +135 -0
- package/dist/commands/create.js +86 -0
- package/dist/commands/deploy.js +334 -0
- package/dist/commands/init.js +788 -0
- package/dist/commands/local.js +387 -0
- package/dist/commands/remove.js +86 -0
- package/dist/commands/test.js +295 -0
- package/dist/compiler/asc.js +83 -0
- package/dist/compiler/index.js +474 -0
- package/dist/debug.js +16 -0
- package/dist/migrations/mapping_api_version_0_0_1.js +78 -0
- package/dist/migrations/mapping_api_version_0_0_2.js +76 -0
- package/dist/migrations/mapping_api_version_0_0_3.js +78 -0
- package/dist/migrations/mapping_api_version_0_0_4.js +80 -0
- package/dist/migrations/mapping_api_version_0_0_5.js +80 -0
- package/dist/migrations/spec_version_0_0_2.js +49 -0
- package/dist/migrations/spec_version_0_0_3.js +54 -0
- package/dist/migrations/util/load-manifest.js +18 -0
- package/dist/migrations/util/versions.js +29 -0
- package/dist/migrations.js +56 -0
- package/dist/protocols/arweave/manifest.graphql +57 -0
- package/dist/protocols/arweave/scaffold/manifest.js +18 -0
- package/dist/protocols/arweave/scaffold/mapping.js +52 -0
- package/dist/protocols/arweave/subgraph.js +23 -0
- package/dist/protocols/cosmos/manifest.graphql +76 -0
- package/dist/protocols/cosmos/scaffold/manifest.js +15 -0
- package/dist/protocols/cosmos/scaffold/mapping.js +38 -0
- package/dist/protocols/cosmos/subgraph.js +25 -0
- package/dist/protocols/ethereum/abi.js +140 -0
- package/dist/protocols/ethereum/codegen/abi.js +451 -0
- package/dist/protocols/ethereum/codegen/abi.test.js +328 -0
- package/dist/protocols/ethereum/codegen/template.js +50 -0
- package/dist/protocols/ethereum/contract.js +20 -0
- package/dist/protocols/ethereum/manifest.graphql +94 -0
- package/dist/protocols/ethereum/scaffold/manifest.js +32 -0
- package/dist/protocols/ethereum/scaffold/mapping.js +65 -0
- package/dist/protocols/ethereum/subgraph.js +171 -0
- package/dist/protocols/ethereum/type-generator.js +125 -0
- package/dist/protocols/index.js +268 -0
- package/dist/protocols/ipfs/codegen/file_template.js +50 -0
- package/dist/protocols/near/contract.js +41 -0
- package/dist/protocols/near/manifest.graphql +64 -0
- package/dist/protocols/near/scaffold/manifest.js +16 -0
- package/dist/protocols/near/scaffold/mapping.js +38 -0
- package/dist/protocols/near/subgraph.js +20 -0
- package/dist/protocols/subgraph.js +2 -0
- package/dist/protocols/substreams/manifest.graphql +45 -0
- package/dist/protocols/substreams/scaffold/manifest.js +12 -0
- package/dist/protocols/substreams/subgraph.js +23 -0
- package/dist/scaffold/cosmos.test.js +83 -0
- package/dist/scaffold/ethereum.test.js +505 -0
- package/dist/scaffold/index.js +128 -0
- package/dist/scaffold/mapping.js +61 -0
- package/dist/scaffold/near.test.js +86 -0
- package/dist/scaffold/schema.js +83 -0
- package/dist/scaffold/tests.js +175 -0
- package/dist/schema.js +54 -0
- package/dist/subgraph.js +249 -0
- package/dist/type-generator.js +224 -0
- package/dist/validation/contract.js +46 -0
- package/dist/validation/index.js +8 -0
- package/dist/validation/manifest.js +271 -0
- package/dist/validation/schema.js +796 -0
- package/dist/validation/schema.test.js +35 -0
- package/dist/watcher.js +79 -0
- package/jest.config.js +1 -1
- package/package.json +16 -9
- package/{bin/graph → src/bin.ts} +2 -1
- package/src/cli.ts +36 -0
- package/src/codegen/schema.js +23 -22
- package/src/codegen/schema.test.js +33 -27
- package/src/codegen/{template.js → template.ts} +8 -6
- package/src/codegen/types/conversions.ts +382 -0
- package/src/codegen/types/index.js +4 -7
- package/src/codegen/types/index.test.js +10 -4
- package/src/codegen/{typescript.js → typescript.ts} +44 -26
- package/src/codegen/util.js +1 -1
- package/src/codegen/util.test.js +5 -2
- package/src/command-helpers/abi.js +46 -30
- package/src/command-helpers/auth.js +6 -9
- package/src/command-helpers/compiler.js +4 -6
- package/src/command-helpers/data-sources.js +8 -11
- package/src/command-helpers/fs.ts +5 -0
- package/src/command-helpers/gluegun.js +2 -4
- package/src/command-helpers/ipfs.ts +3 -0
- package/src/command-helpers/{jsonrpc.js → jsonrpc.ts} +11 -16
- package/src/command-helpers/network.js +58 -44
- package/src/command-helpers/network.test.js +23 -19
- package/src/command-helpers/node.js +3 -7
- package/src/command-helpers/scaffold.js +34 -25
- package/src/command-helpers/{spinner.js → spinner.ts} +10 -10
- package/src/command-helpers/studio.test.js +61 -30
- package/src/command-helpers/studio.ts +20 -0
- package/src/command-helpers/{subgraph.js → subgraph.ts} +6 -6
- package/src/command-helpers/version.js +11 -16
- package/src/command-helpers/version.test.js +111 -82
- package/src/commands/add.js +49 -35
- package/src/commands/auth.js +13 -25
- package/src/commands/build.js +9 -9
- package/src/commands/codegen.js +12 -12
- package/src/commands/create.js +22 -22
- package/src/commands/deploy.js +39 -34
- package/src/commands/init.js +21 -24
- package/src/commands/local.js +15 -14
- package/src/commands/remove.js +22 -22
- package/src/commands/test.js +86 -70
- package/src/compiler/{asc.js → asc.ts} +29 -16
- package/src/compiler/index.js +18 -18
- package/src/{debug.js → debug.ts} +2 -2
- package/src/migrations/mapping_api_version_0_0_1.js +7 -7
- package/src/migrations/mapping_api_version_0_0_2.js +5 -5
- package/src/migrations/mapping_api_version_0_0_3.js +7 -7
- package/src/migrations/mapping_api_version_0_0_4.js +7 -7
- package/src/migrations/mapping_api_version_0_0_5.js +7 -7
- package/src/migrations/spec_version_0_0_2.js +5 -5
- package/src/migrations/spec_version_0_0_3.js +5 -5
- package/src/migrations/util/load-manifest.js +6 -9
- package/src/migrations/util/versions.js +5 -10
- package/src/{migrations.js → migrations.ts} +14 -15
- package/src/protocols/arweave/scaffold/manifest.js +1 -4
- package/src/protocols/arweave/scaffold/mapping.js +1 -3
- package/src/protocols/arweave/subgraph.ts +22 -0
- package/src/protocols/cosmos/scaffold/manifest.js +1 -4
- package/src/protocols/cosmos/scaffold/mapping.js +1 -3
- package/src/protocols/cosmos/subgraph.js +2 -2
- package/src/protocols/ethereum/abi.js +7 -13
- package/src/protocols/ethereum/codegen/abi.js +219 -222
- package/src/protocols/ethereum/codegen/abi.test.js +6 -6
- package/src/protocols/ethereum/codegen/template.js +3 -5
- package/src/protocols/ethereum/contract.js +3 -2
- package/src/protocols/ethereum/scaffold/manifest.js +4 -7
- package/src/protocols/ethereum/scaffold/mapping.js +4 -11
- package/src/protocols/ethereum/subgraph.js +21 -20
- package/src/protocols/ethereum/type-generator.js +24 -30
- package/src/protocols/{index.js → index.ts} +65 -46
- package/src/protocols/ipfs/codegen/file_template.js +2 -2
- package/src/protocols/near/{contract.js → contract.ts} +16 -12
- package/src/protocols/near/scaffold/manifest.js +2 -5
- package/src/protocols/near/scaffold/mapping.js +1 -3
- package/src/protocols/near/subgraph.js +3 -6
- package/src/protocols/subgraph.ts +12 -0
- package/src/protocols/substreams/scaffold/{manifest.js → manifest.ts} +2 -7
- package/src/protocols/substreams/subgraph.ts +22 -0
- package/src/scaffold/cosmos.test.js +3 -3
- package/src/scaffold/ethereum.test.js +4 -4
- package/src/scaffold/index.js +7 -7
- package/src/scaffold/mapping.js +3 -3
- package/src/scaffold/near.test.js +3 -3
- package/src/scaffold/schema.js +4 -4
- package/src/scaffold/tests.js +72 -58
- package/src/schema.ts +26 -0
- package/src/{subgraph.js → subgraph.ts} +80 -60
- package/src/type-generator.js +20 -20
- package/src/validation/contract.js +2 -6
- package/src/validation/index.js +1 -1
- package/src/validation/manifest.js +24 -22
- package/src/validation/schema.test.js +1 -1
- package/src/validation/{schema.js → schema.ts} +268 -221
- package/src/{watcher.js → watcher.ts} +23 -12
- package/tests/cli/globalSetup.js +2 -2
- package/tests/cli/globalTeardown.js +2 -2
- package/tests/cli/init.test.js +2 -2
- package/tests/cli/util.js +7 -13
- package/tsconfig.json +18 -0
- package/src/cli.js +0 -38
- package/src/codegen/types/conversions.js +0 -329
- package/src/command-helpers/fs.js +0 -8
- package/src/command-helpers/ipfs.js +0 -6
- package/src/command-helpers/studio.js +0 -15
- package/src/protocols/arweave/subgraph.js +0 -20
- package/src/protocols/substreams/subgraph.js +0 -17
- package/src/schema.js +0 -23
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
1
|
# @graphprotocol/graph-cli
|
|
2
2
|
|
|
3
|
+
## 0.37.3-alpha-20230110163550-d64bef6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#1020](https://github.com/graphprotocol/graph-cli/pull/1020) [`6184688`](https://github.com/graphprotocol/graph-cli/commit/6184688d7e6efaae49a722410befd28da0af87b0) Thanks [@saihaj](https://github.com/saihaj)! - dependencies updates:
|
|
8
|
+
- Updated dependency [`immutable@4.2.1` ↗︎](https://www.npmjs.com/package/immutable/v/4.2.1) (from `3.8.2`, in `dependencies`)
|
|
9
|
+
|
|
3
10
|
## 0.37.2
|
|
4
11
|
|
|
5
12
|
### Patch Changes
|
package/babel.config.js
ADDED
package/dist/bin.js
ADDED
package/dist/cli.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
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 path_1 = __importDefault(require("path"));
|
|
8
|
+
const gluegun_1 = require("gluegun");
|
|
9
|
+
const run = async (argv) => {
|
|
10
|
+
let builder = (0, gluegun_1.build)()
|
|
11
|
+
.brand('graph')
|
|
12
|
+
.src(__dirname);
|
|
13
|
+
const pluginDirs = [];
|
|
14
|
+
await Promise.all(['npm root -g', 'npm root', 'yarn global dir'].map(async (cmd) => {
|
|
15
|
+
try {
|
|
16
|
+
const dir = await gluegun_1.system.run(cmd, { trim: true });
|
|
17
|
+
pluginDirs.push(dir);
|
|
18
|
+
}
|
|
19
|
+
catch (_) {
|
|
20
|
+
// noop
|
|
21
|
+
}
|
|
22
|
+
}));
|
|
23
|
+
// Inject potential plugin directories
|
|
24
|
+
builder = pluginDirs.reduce((cli, dir) => cli.plugin(path_1.default.join(dir, '@graphprotocol', 'indexer-cli', 'dist')), builder);
|
|
25
|
+
const cli = builder
|
|
26
|
+
.help()
|
|
27
|
+
.version()
|
|
28
|
+
.defaultCommand()
|
|
29
|
+
.create();
|
|
30
|
+
return await cli.run(argv);
|
|
31
|
+
};
|
|
32
|
+
exports.run = run;
|
|
@@ -0,0 +1,250 @@
|
|
|
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 tsCodegen = __importStar(require("./typescript"));
|
|
31
|
+
const typesCodegen = __importStar(require("./types"));
|
|
32
|
+
const List = immutable_1.default.List;
|
|
33
|
+
class IdField {
|
|
34
|
+
static BYTES = Symbol('Bytes');
|
|
35
|
+
static STRING = Symbol('String');
|
|
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
|
+
constructor(schema) {
|
|
71
|
+
this.schema = schema;
|
|
72
|
+
}
|
|
73
|
+
generateModuleImports() {
|
|
74
|
+
return [
|
|
75
|
+
tsCodegen.moduleImports([
|
|
76
|
+
// Base classes
|
|
77
|
+
'TypedMap',
|
|
78
|
+
'Entity',
|
|
79
|
+
'Value',
|
|
80
|
+
'ValueKind',
|
|
81
|
+
// APIs
|
|
82
|
+
'store',
|
|
83
|
+
// Basic Scalar types
|
|
84
|
+
'Bytes',
|
|
85
|
+
'BigInt',
|
|
86
|
+
'BigDecimal',
|
|
87
|
+
], '@graphprotocol/graph-ts'),
|
|
88
|
+
];
|
|
89
|
+
}
|
|
90
|
+
generateTypes() {
|
|
91
|
+
return this.schema.ast
|
|
92
|
+
.get('definitions')
|
|
93
|
+
.filter(def => this._isEntityTypeDefinition(def))
|
|
94
|
+
.map(def => this._generateEntityType(def));
|
|
95
|
+
}
|
|
96
|
+
_isEntityTypeDefinition(def) {
|
|
97
|
+
return (def.get('kind') === 'ObjectTypeDefinition' &&
|
|
98
|
+
def
|
|
99
|
+
.get('directives')
|
|
100
|
+
.find(directive => directive.getIn(['name', 'value']) === 'entity') !== undefined);
|
|
101
|
+
}
|
|
102
|
+
_isInterfaceDefinition(def) {
|
|
103
|
+
return def.get('kind') === 'InterfaceTypeDefinition';
|
|
104
|
+
}
|
|
105
|
+
_generateEntityType(def) {
|
|
106
|
+
let name = def.getIn(['name', 'value']);
|
|
107
|
+
let klass = tsCodegen.klass(name, { export: true, extends: 'Entity' });
|
|
108
|
+
const fields = def.get('fields');
|
|
109
|
+
const idField = IdField.fromFields(fields);
|
|
110
|
+
// Generate and add a constructor
|
|
111
|
+
klass.addMethod(this._generateConstructor(name, fields));
|
|
112
|
+
// Generate and add save() and getById() methods
|
|
113
|
+
this._generateStoreMethods(name, idField).forEach(method => klass.addMethod(method));
|
|
114
|
+
// Generate and add entity field getters and setters
|
|
115
|
+
def
|
|
116
|
+
.get('fields')
|
|
117
|
+
.reduce((methods, field) => methods.concat(this._generateEntityFieldMethods(def, field)), List())
|
|
118
|
+
.forEach(method => klass.addMethod(method));
|
|
119
|
+
return klass;
|
|
120
|
+
}
|
|
121
|
+
_generateConstructor(entityName, fields) {
|
|
122
|
+
const idField = IdField.fromFields(fields);
|
|
123
|
+
return tsCodegen.method('constructor', [tsCodegen.param('id', idField.tsNamedType())], undefined, `
|
|
124
|
+
super()
|
|
125
|
+
this.set('id', ${idField.tsValueFrom()})
|
|
126
|
+
`);
|
|
127
|
+
}
|
|
128
|
+
_generateStoreMethods(entityName, idField) {
|
|
129
|
+
return List.of(tsCodegen.method('save', [], tsCodegen.namedType('void'), `
|
|
130
|
+
let id = this.get('id')
|
|
131
|
+
assert(id != null,
|
|
132
|
+
'Cannot save ${entityName} entity without an ID')
|
|
133
|
+
if (id) {
|
|
134
|
+
assert(id.kind == ${idField.tsValueKind()},
|
|
135
|
+
\`Entities of type ${entityName} must have an ID of type ${idField.gqlTypeName()} but the id '\${id.displayData()}' is of type \${id.displayKind()}\`)
|
|
136
|
+
store.set('${entityName}', ${idField.tsValueToString()}, this)
|
|
137
|
+
}`), tsCodegen.staticMethod('load', [tsCodegen.param('id', tsCodegen.namedType(idField.typeName()))], tsCodegen.nullableType(tsCodegen.namedType(entityName)), `
|
|
138
|
+
return changetype<${entityName} | null>(store.get('${entityName}', ${idField.tsToString()}))
|
|
139
|
+
`));
|
|
140
|
+
}
|
|
141
|
+
_generateEntityFieldMethods(entityDef, fieldDef) {
|
|
142
|
+
return List([
|
|
143
|
+
this._generateEntityFieldGetter(entityDef, fieldDef),
|
|
144
|
+
this._generateEntityFieldSetter(entityDef, fieldDef),
|
|
145
|
+
]);
|
|
146
|
+
}
|
|
147
|
+
_generateEntityFieldGetter(entityDef, fieldDef) {
|
|
148
|
+
let name = fieldDef.getIn(['name', 'value']);
|
|
149
|
+
let gqlType = fieldDef.get('type');
|
|
150
|
+
let fieldValueType = this._valueTypeFromGraphQl(gqlType);
|
|
151
|
+
let returnType = this._typeFromGraphQl(gqlType);
|
|
152
|
+
let isNullable = returnType instanceof tsCodegen.NullableType;
|
|
153
|
+
let getNonNullable = `return ${typesCodegen.valueToAsc('value!', fieldValueType)}`;
|
|
154
|
+
let getNullable = `if (!value || value.kind == ValueKind.NULL) {
|
|
155
|
+
return null
|
|
156
|
+
} else {
|
|
157
|
+
return ${typesCodegen.valueToAsc('value', fieldValueType)}
|
|
158
|
+
}`;
|
|
159
|
+
return tsCodegen.method(`get ${name}`, [], returnType, `
|
|
160
|
+
let value = this.get('${name}')
|
|
161
|
+
${isNullable ? getNullable : getNonNullable}
|
|
162
|
+
`);
|
|
163
|
+
}
|
|
164
|
+
_generateEntityFieldSetter(entityDef, fieldDef) {
|
|
165
|
+
let name = fieldDef.getIn(['name', 'value']);
|
|
166
|
+
let gqlType = fieldDef.get('type');
|
|
167
|
+
let fieldValueType = this._valueTypeFromGraphQl(gqlType);
|
|
168
|
+
let paramType = this._typeFromGraphQl(gqlType);
|
|
169
|
+
let isNullable = paramType instanceof tsCodegen.NullableType;
|
|
170
|
+
let paramTypeString = isNullable ? paramType.inner.toString() : paramType.toString();
|
|
171
|
+
let isArray = paramType instanceof tsCodegen.ArrayType;
|
|
172
|
+
if (isArray && paramType.inner instanceof tsCodegen.NullableType) {
|
|
173
|
+
let baseType = this._baseType(gqlType);
|
|
174
|
+
throw new Error(`
|
|
175
|
+
GraphQL schema can't have List's with Nullable members.
|
|
176
|
+
Error in '${name}' field of type '[${baseType}]'.
|
|
177
|
+
Suggestion: add an '!' to the member type of the List, change from '[${baseType}]' to '[${baseType}!]'`);
|
|
178
|
+
}
|
|
179
|
+
let setNonNullable = `
|
|
180
|
+
this.set('${name}', ${typesCodegen.valueFromAsc(`value`, fieldValueType)})
|
|
181
|
+
`;
|
|
182
|
+
let setNullable = `
|
|
183
|
+
if (!value) {
|
|
184
|
+
this.unset('${name}')
|
|
185
|
+
} else {
|
|
186
|
+
this.set('${name}', ${typesCodegen.valueFromAsc(`<${paramTypeString}>value`, fieldValueType)})
|
|
187
|
+
}
|
|
188
|
+
`;
|
|
189
|
+
return tsCodegen.method(`set ${name}`, [tsCodegen.param('value', paramType)], undefined, isNullable ? setNullable : setNonNullable);
|
|
190
|
+
}
|
|
191
|
+
_resolveFieldType(gqlType) {
|
|
192
|
+
let typeName = gqlType.getIn(['name', 'value']);
|
|
193
|
+
// If this is a reference to another type, the field has the type of
|
|
194
|
+
// the referred type's id field
|
|
195
|
+
const typeDef = this.schema.ast
|
|
196
|
+
.get('definitions')
|
|
197
|
+
.find(def => (this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) &&
|
|
198
|
+
def.getIn(['name', 'value']) === typeName);
|
|
199
|
+
if (typeDef) {
|
|
200
|
+
return IdField.fromTypeDef(typeDef).typeName();
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
return typeName;
|
|
204
|
+
}
|
|
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'), false);
|
|
213
|
+
}
|
|
214
|
+
else if (gqlType.get('kind') === 'ListType') {
|
|
215
|
+
return '[' + this._valueTypeFromGraphQl(gqlType.get('type')) + ']';
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
return this._resolveFieldType(gqlType);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
/** Determine the base type of `gqlType` by removing any non-null
|
|
222
|
+
* constraints and using the type of elements of lists */
|
|
223
|
+
_baseType(gqlType) {
|
|
224
|
+
if (gqlType.get('kind') === 'NonNullType') {
|
|
225
|
+
return this._baseType(gqlType.get('type'));
|
|
226
|
+
}
|
|
227
|
+
else if (gqlType.get('kind') === 'ListType') {
|
|
228
|
+
return this._baseType(gqlType.get('type'));
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
return gqlType.getIn(['name', 'value']);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
_typeFromGraphQl(gqlType, nullable = true) {
|
|
235
|
+
if (gqlType.get('kind') === 'NonNullType') {
|
|
236
|
+
return this._typeFromGraphQl(gqlType.get('type'), false);
|
|
237
|
+
}
|
|
238
|
+
else if (gqlType.get('kind') === 'ListType') {
|
|
239
|
+
let type = tsCodegen.arrayType(this._typeFromGraphQl(gqlType.get('type')));
|
|
240
|
+
return nullable ? tsCodegen.nullableType(type) : type;
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
// NamedType
|
|
244
|
+
let type = tsCodegen.namedType(typesCodegen.ascTypeForValue(this._resolveFieldType(gqlType)));
|
|
245
|
+
// In AssemblyScript, primitives cannot be nullable.
|
|
246
|
+
return nullable && !type.isPrimitive() ? tsCodegen.nullableType(type) : type;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
exports.default = SchemaCodeGenerator;
|