@graphprotocol/graph-cli 0.45.1-alpha-20230405193954-4e288f4 → 0.45.1-alpha-20230409224212-0d77249
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 +6 -5
- package/dist/codegen/schema.js +54 -53
- package/dist/codegen/schema.test.js +4 -3
- package/dist/schema.js +2 -1
- package/oclif.manifest.json +1 -1
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
# @graphprotocol/graph-cli
|
|
2
2
|
|
|
3
|
-
## 0.45.1-alpha-
|
|
3
|
+
## 0.45.1-alpha-20230409224212-0d77249
|
|
4
4
|
|
|
5
5
|
### Patch Changes
|
|
6
6
|
|
|
7
|
-
- [#
|
|
8
|
-
[`
|
|
9
|
-
Thanks [@
|
|
10
|
-
|
|
7
|
+
- [#1263](https://github.com/graphprotocol/graph-tooling/pull/1263)
|
|
8
|
+
[`2ba2ba9`](https://github.com/graphprotocol/graph-tooling/commit/2ba2ba96c6cdc10461903b0712f122eb6d245f86)
|
|
9
|
+
Thanks [@mihirgupta0900](https://github.com/mihirgupta0900)! - Fixes dependency error from
|
|
10
|
+
`ipfs-http-client` and `concat-stream`. Visit
|
|
11
|
+
https://github.com/graphprotocol/graph-tooling/issues/1262 for more details.
|
|
11
12
|
|
|
12
13
|
## 0.45.0
|
|
13
14
|
|
package/dist/codegen/schema.js
CHANGED
|
@@ -22,18 +22,17 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
25
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
/* eslint-disable unicorn/no-array-for-each */
|
|
30
|
+
const immutable_1 = __importDefault(require("immutable"));
|
|
26
31
|
const typesCodegen = __importStar(require("./types"));
|
|
27
32
|
const tsCodegen = __importStar(require("./typescript"));
|
|
28
33
|
class IdField {
|
|
29
34
|
constructor(idField) {
|
|
30
|
-
|
|
31
|
-
throw Error('id field must be non-nullable');
|
|
32
|
-
}
|
|
33
|
-
if (idField.type.type.kind !== 'NamedType') {
|
|
34
|
-
throw Error('id field must be a named type');
|
|
35
|
-
}
|
|
36
|
-
const typeName = idField.type.type.name.value;
|
|
35
|
+
const typeName = idField.getIn(['type', 'type', 'name', 'value']);
|
|
37
36
|
this.kind = typeName === 'Bytes' ? IdField.BYTES : IdField.STRING;
|
|
38
37
|
}
|
|
39
38
|
typeName() {
|
|
@@ -58,11 +57,11 @@ class IdField {
|
|
|
58
57
|
return this.kind == IdField.BYTES ? 'id.toHexString()' : 'id';
|
|
59
58
|
}
|
|
60
59
|
static fromFields(fields) {
|
|
61
|
-
const idField = fields
|
|
60
|
+
const idField = fields.find(field => field.getIn(['name', 'value']) === 'id');
|
|
62
61
|
return new IdField(idField);
|
|
63
62
|
}
|
|
64
63
|
static fromTypeDef(def) {
|
|
65
|
-
return IdField.fromFields(def.fields);
|
|
64
|
+
return IdField.fromFields(def.get('fields'));
|
|
66
65
|
}
|
|
67
66
|
}
|
|
68
67
|
IdField.BYTES = Symbol('Bytes');
|
|
@@ -90,33 +89,33 @@ class SchemaCodeGenerator {
|
|
|
90
89
|
];
|
|
91
90
|
}
|
|
92
91
|
generateTypes() {
|
|
93
|
-
return this.schema.ast
|
|
94
|
-
.
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
})
|
|
99
|
-
.filter(Boolean);
|
|
92
|
+
return this.schema.ast
|
|
93
|
+
.get('definitions')
|
|
94
|
+
.filter((def) => this._isEntityTypeDefinition(def))
|
|
95
|
+
.map((def) => this._generateEntityType(def));
|
|
100
96
|
}
|
|
101
97
|
_isEntityTypeDefinition(def) {
|
|
102
|
-
return (def.kind === 'ObjectTypeDefinition' &&
|
|
103
|
-
def
|
|
98
|
+
return (def.get('kind') === 'ObjectTypeDefinition' &&
|
|
99
|
+
def
|
|
100
|
+
.get('directives')
|
|
101
|
+
.find((directive) => directive.getIn(['name', 'value']) === 'entity') !== undefined);
|
|
104
102
|
}
|
|
105
103
|
_isInterfaceDefinition(def) {
|
|
106
|
-
return def.kind === 'InterfaceTypeDefinition';
|
|
104
|
+
return def.get('kind') === 'InterfaceTypeDefinition';
|
|
107
105
|
}
|
|
108
106
|
_generateEntityType(def) {
|
|
109
|
-
const name = def.name
|
|
107
|
+
const name = def.getIn(['name', 'value']);
|
|
110
108
|
const klass = tsCodegen.klass(name, { export: true, extends: 'Entity' });
|
|
111
|
-
const fields = def.fields;
|
|
109
|
+
const fields = def.get('fields');
|
|
112
110
|
const idField = IdField.fromFields(fields);
|
|
113
111
|
// Generate and add a constructor
|
|
114
112
|
klass.addMethod(this._generateConstructor(name, fields));
|
|
115
113
|
// Generate and add save() and getById() methods
|
|
116
114
|
this._generateStoreMethods(name, idField).forEach(method => klass.addMethod(method));
|
|
117
115
|
// Generate and add entity field getters and setters
|
|
118
|
-
def
|
|
119
|
-
|
|
116
|
+
def
|
|
117
|
+
.get('fields')
|
|
118
|
+
.reduce((methods, field) => methods.concat(this._generateEntityFieldMethods(def, field)), immutable_1.default.List())
|
|
120
119
|
.forEach((method) => klass.addMethod(method));
|
|
121
120
|
return klass;
|
|
122
121
|
}
|
|
@@ -128,8 +127,7 @@ class SchemaCodeGenerator {
|
|
|
128
127
|
`);
|
|
129
128
|
}
|
|
130
129
|
_generateStoreMethods(entityName, idField) {
|
|
131
|
-
return [
|
|
132
|
-
tsCodegen.method('save', [], tsCodegen.namedType('void'), `
|
|
130
|
+
return immutable_1.default.List.of(tsCodegen.method('save', [], tsCodegen.namedType('void'), `
|
|
133
131
|
let id = this.get('id')
|
|
134
132
|
assert(id != null,
|
|
135
133
|
'Cannot save ${entityName} entity without an ID')
|
|
@@ -137,24 +135,23 @@ class SchemaCodeGenerator {
|
|
|
137
135
|
assert(id.kind == ${idField.tsValueKind()},
|
|
138
136
|
\`Entities of type ${entityName} must have an ID of type ${idField.gqlTypeName()} but the id '\${id.displayData()}' is of type \${id.displayKind()}\`)
|
|
139
137
|
store.set('${entityName}', ${idField.tsValueToString()}, this)
|
|
140
|
-
}`),
|
|
141
|
-
tsCodegen.staticMethod('load', [tsCodegen.param('id', tsCodegen.namedType(idField.typeName()))], tsCodegen.nullableType(tsCodegen.namedType(entityName)), `
|
|
138
|
+
}`), tsCodegen.staticMethod('load', [tsCodegen.param('id', tsCodegen.namedType(idField.typeName()))], tsCodegen.nullableType(tsCodegen.namedType(entityName)), `
|
|
142
139
|
return changetype<${entityName} | null>(store.get('${entityName}', ${idField.tsToString()}))
|
|
143
|
-
`)
|
|
144
|
-
];
|
|
140
|
+
`));
|
|
145
141
|
}
|
|
146
142
|
_generateEntityFieldMethods(entityDef, fieldDef) {
|
|
147
|
-
return
|
|
143
|
+
return (immutable_1.default
|
|
144
|
+
.List([
|
|
148
145
|
this._generateEntityFieldGetter(entityDef, fieldDef),
|
|
149
146
|
this._generateEntityFieldSetter(entityDef, fieldDef),
|
|
150
|
-
]
|
|
147
|
+
])
|
|
151
148
|
// generator can return null if the field is not supported
|
|
152
149
|
// so we filter all falsy values
|
|
153
|
-
.filter(Boolean);
|
|
150
|
+
.filter(Boolean));
|
|
154
151
|
}
|
|
155
152
|
_generateEntityFieldGetter(_entityDef, fieldDef) {
|
|
156
|
-
const name = fieldDef.name
|
|
157
|
-
const gqlType = fieldDef.type;
|
|
153
|
+
const name = fieldDef.getIn(['name', 'value']);
|
|
154
|
+
const gqlType = fieldDef.get('type');
|
|
158
155
|
const fieldValueType = this._valueTypeFromGraphQl(gqlType);
|
|
159
156
|
const returnType = this._typeFromGraphQl(gqlType);
|
|
160
157
|
const isNullable = returnType instanceof tsCodegen.NullableType;
|
|
@@ -170,12 +167,14 @@ class SchemaCodeGenerator {
|
|
|
170
167
|
`);
|
|
171
168
|
}
|
|
172
169
|
_generateEntityFieldSetter(_entityDef, fieldDef) {
|
|
173
|
-
const name = fieldDef.name
|
|
174
|
-
const isDerivedField = !!fieldDef
|
|
170
|
+
const name = fieldDef.getIn(['name', 'value']);
|
|
171
|
+
const isDerivedField = !!fieldDef
|
|
172
|
+
.get('directives')
|
|
173
|
+
.find((directive) => directive.getIn(['name', 'value']) === 'derivedFrom');
|
|
175
174
|
// We cannot have setters for derived fields
|
|
176
175
|
if (isDerivedField)
|
|
177
176
|
return null;
|
|
178
|
-
const gqlType = fieldDef.type;
|
|
177
|
+
const gqlType = fieldDef.get('type');
|
|
179
178
|
const fieldValueType = this._valueTypeFromGraphQl(gqlType);
|
|
180
179
|
const paramType = this._typeFromGraphQl(gqlType);
|
|
181
180
|
const isNullable = paramType instanceof tsCodegen.NullableType;
|
|
@@ -201,11 +200,13 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
201
200
|
return tsCodegen.method(`set ${name}`, [tsCodegen.param('value', paramType)], undefined, isNullable ? setNullable : setNonNullable);
|
|
202
201
|
}
|
|
203
202
|
_resolveFieldType(gqlType) {
|
|
204
|
-
const typeName = gqlType.name
|
|
203
|
+
const typeName = gqlType.getIn(['name', 'value']);
|
|
205
204
|
// If this is a reference to another type, the field has the type of
|
|
206
205
|
// the referred type's id field
|
|
207
|
-
const typeDef = this.schema.ast
|
|
208
|
-
|
|
206
|
+
const typeDef = this.schema.ast
|
|
207
|
+
.get('definitions')
|
|
208
|
+
.find((def) => (this._isEntityTypeDefinition(def) || this._isInterfaceDefinition(def)) &&
|
|
209
|
+
def.getIn(['name', 'value']) === typeName);
|
|
209
210
|
if (typeDef) {
|
|
210
211
|
return IdField.fromTypeDef(typeDef).typeName();
|
|
211
212
|
}
|
|
@@ -216,31 +217,31 @@ Suggestion: add an '!' to the member type of the List, change from '[${baseType}
|
|
|
216
217
|
* other entity types, this is the same as the type of the id of the
|
|
217
218
|
* referred type, i.e., `string` or `Bytes`*/
|
|
218
219
|
_valueTypeFromGraphQl(gqlType) {
|
|
219
|
-
if (gqlType.kind === 'NonNullType') {
|
|
220
|
-
return this._valueTypeFromGraphQl(gqlType.type);
|
|
220
|
+
if (gqlType.get('kind') === 'NonNullType') {
|
|
221
|
+
return this._valueTypeFromGraphQl(gqlType.get('type'));
|
|
221
222
|
}
|
|
222
|
-
if (gqlType.kind === 'ListType') {
|
|
223
|
-
return '[' + this._valueTypeFromGraphQl(gqlType.type) + ']';
|
|
223
|
+
if (gqlType.get('kind') === 'ListType') {
|
|
224
|
+
return '[' + this._valueTypeFromGraphQl(gqlType.get('type')) + ']';
|
|
224
225
|
}
|
|
225
226
|
return this._resolveFieldType(gqlType);
|
|
226
227
|
}
|
|
227
228
|
/** Determine the base type of `gqlType` by removing any non-null
|
|
228
229
|
* constraints and using the type of elements of lists */
|
|
229
230
|
_baseType(gqlType) {
|
|
230
|
-
if (gqlType.kind === 'NonNullType') {
|
|
231
|
-
return this._baseType(gqlType.type);
|
|
231
|
+
if (gqlType.get('kind') === 'NonNullType') {
|
|
232
|
+
return this._baseType(gqlType.get('type'));
|
|
232
233
|
}
|
|
233
|
-
if (gqlType.kind === 'ListType') {
|
|
234
|
-
return this._baseType(gqlType.type);
|
|
234
|
+
if (gqlType.get('kind') === 'ListType') {
|
|
235
|
+
return this._baseType(gqlType.get('type'));
|
|
235
236
|
}
|
|
236
|
-
return gqlType.name
|
|
237
|
+
return gqlType.getIn(['name', 'value']);
|
|
237
238
|
}
|
|
238
239
|
_typeFromGraphQl(gqlType, nullable = true) {
|
|
239
|
-
if (gqlType.kind === 'NonNullType') {
|
|
240
|
-
return this._typeFromGraphQl(gqlType.type, false);
|
|
240
|
+
if (gqlType.get('kind') === 'NonNullType') {
|
|
241
|
+
return this._typeFromGraphQl(gqlType.get('type'), false);
|
|
241
242
|
}
|
|
242
|
-
if (gqlType.kind === 'ListType') {
|
|
243
|
-
const type = tsCodegen.arrayType(this._typeFromGraphQl(gqlType.type));
|
|
243
|
+
if (gqlType.get('kind') === 'ListType') {
|
|
244
|
+
const type = tsCodegen.arrayType(this._typeFromGraphQl(gqlType.get('type')));
|
|
244
245
|
return nullable ? tsCodegen.nullableType(type) : type;
|
|
245
246
|
}
|
|
246
247
|
// NamedType
|
|
@@ -27,12 +27,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
const graphql = __importStar(require("graphql/language"));
|
|
30
|
+
const immutable_1 = __importDefault(require("immutable"));
|
|
30
31
|
const prettier_1 = __importDefault(require("prettier"));
|
|
31
32
|
const schema_1 = __importDefault(require("../schema"));
|
|
32
33
|
const schema_2 = __importDefault(require("./schema"));
|
|
33
34
|
const typescript_1 = require("./typescript");
|
|
34
35
|
const formatTS = (code) => prettier_1.default.format(code, { parser: 'typescript', semi: false });
|
|
35
|
-
const createSchemaCodeGen = (schema) => new schema_2.default(new schema_1.default('', schema, graphql.parse(schema)));
|
|
36
|
+
const createSchemaCodeGen = (schema) => new schema_2.default(new schema_1.default('', schema, immutable_1.default.fromJS(graphql.parse(schema))));
|
|
36
37
|
const testEntity = (generatedTypes, expectedEntity) => {
|
|
37
38
|
const entity = generatedTypes.find(type => type.name === expectedEntity.name);
|
|
38
39
|
expect(entity instanceof typescript_1.Class).toBe(true);
|
|
@@ -63,7 +64,7 @@ describe('Schema code generator', () => {
|
|
|
63
64
|
barfoo: Int
|
|
64
65
|
}
|
|
65
66
|
`);
|
|
66
|
-
expect(codegen.generateTypes().
|
|
67
|
+
expect(codegen.generateTypes().size).toBe(0);
|
|
67
68
|
});
|
|
68
69
|
describe('Should generate correct classes for each entity', () => {
|
|
69
70
|
const codegen = createSchemaCodeGen(`
|
|
@@ -98,7 +99,7 @@ describe('Schema code generator', () => {
|
|
|
98
99
|
const foo = generatedTypes.find((type) => type.name === 'Foo');
|
|
99
100
|
expect(foo).toBe(undefined);
|
|
100
101
|
// Account and Wallet
|
|
101
|
-
expect(generatedTypes.
|
|
102
|
+
expect(generatedTypes.size).toBe(2);
|
|
102
103
|
});
|
|
103
104
|
test('Account is an entity with the correct methods', () => {
|
|
104
105
|
testEntity(generatedTypes, {
|
package/dist/schema.js
CHANGED
|
@@ -28,6 +28,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
30
30
|
const graphql = __importStar(require("graphql/language"));
|
|
31
|
+
const immutable_1 = __importDefault(require("immutable"));
|
|
31
32
|
const schema_1 = __importDefault(require("./codegen/schema"));
|
|
32
33
|
class Schema {
|
|
33
34
|
constructor(filename, document, ast) {
|
|
@@ -44,7 +45,7 @@ class Schema {
|
|
|
44
45
|
static async load(filename) {
|
|
45
46
|
const document = await fs_extra_1.default.readFile(filename, 'utf-8');
|
|
46
47
|
const ast = graphql.parse(document);
|
|
47
|
-
return new Schema(filename, document, ast);
|
|
48
|
+
return new Schema(filename, document, immutable_1.default.fromJS(ast));
|
|
48
49
|
}
|
|
49
50
|
}
|
|
50
51
|
exports.default = Schema;
|
package/oclif.manifest.json
CHANGED
package/package.json
CHANGED