@graphprotocol/graph-cli 0.37.4 → 0.37.5
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 +8 -0
- package/dist/bin.js +6 -0
- package/dist/cli.js +26 -0
- package/dist/codegen/schema.js +244 -0
- package/dist/codegen/schema.test.js +455 -0
- package/dist/codegen/template.js +66 -0
- package/dist/codegen/types/conversions.js +335 -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 +40 -0
- package/dist/codegen/util.test.js +93 -0
- package/dist/command-helpers/abi.js +76 -0
- package/dist/command-helpers/auth.js +76 -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 +48 -0
- package/dist/command-helpers/ipfs.js +5 -0
- package/dist/command-helpers/jsonrpc.js +27 -0
- package/dist/command-helpers/network.js +90 -0
- package/dist/command-helpers/network.test.js +86 -0
- package/dist/command-helpers/node.js +40 -0
- package/dist/command-helpers/scaffold.js +110 -0
- package/dist/command-helpers/spinner.js +80 -0
- package/dist/command-helpers/studio.js +14 -0
- package/dist/command-helpers/studio.test.js +41 -0
- package/dist/command-helpers/subgraph.js +22 -0
- package/dist/command-helpers/version.js +66 -0
- package/dist/command-helpers/version.test.js +186 -0
- package/dist/commands/add.js +187 -0
- package/dist/commands/auth.js +122 -0
- package/dist/commands/build.js +136 -0
- package/dist/commands/codegen.js +128 -0
- package/dist/commands/create.js +94 -0
- package/dist/commands/deploy.js +335 -0
- package/dist/commands/init.js +800 -0
- package/dist/commands/local.js +380 -0
- package/dist/commands/remove.js +95 -0
- package/dist/commands/test.js +293 -0
- package/dist/compiler/asc.js +83 -0
- package/dist/compiler/index.js +466 -0
- package/dist/debug.js +16 -0
- package/dist/migrations/mapping_api_version_0_0_1.js +83 -0
- package/dist/migrations/mapping_api_version_0_0_2.js +83 -0
- package/dist/migrations/mapping_api_version_0_0_3.js +83 -0
- package/dist/migrations/mapping_api_version_0_0_4.js +85 -0
- package/dist/migrations/mapping_api_version_0_0_5.js +85 -0
- package/dist/migrations/spec_version_0_0_2.js +45 -0
- package/dist/migrations/spec_version_0_0_3.js +50 -0
- package/dist/migrations/util/load-manifest.js +16 -0
- package/dist/migrations/util/versions.js +28 -0
- package/dist/migrations.js +54 -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/contract.js +2 -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 +28 -0
- package/dist/protocols/ethereum/abi.js +138 -0
- package/dist/protocols/ethereum/codegen/abi.js +474 -0
- package/dist/protocols/ethereum/codegen/abi.test.js +325 -0
- package/dist/protocols/ethereum/codegen/template.js +52 -0
- package/dist/protocols/ethereum/contract.js +22 -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 +172 -0
- package/dist/protocols/ethereum/type-generator.js +123 -0
- package/dist/protocols/index.js +266 -0
- package/dist/protocols/ipfs/codegen/file_template.js +53 -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 +23 -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 +82 -0
- package/dist/scaffold/ethereum.test.js +496 -0
- package/dist/scaffold/index.js +133 -0
- package/dist/scaffold/mapping.js +57 -0
- package/dist/scaffold/near.test.js +85 -0
- package/dist/scaffold/schema.js +86 -0
- package/dist/scaffold/tests.js +179 -0
- package/dist/schema.js +54 -0
- package/dist/subgraph.js +243 -0
- package/dist/type-generator.js +225 -0
- package/dist/validation/contract.js +44 -0
- package/dist/validation/index.js +10 -0
- package/dist/validation/manifest.js +266 -0
- package/dist/validation/schema.js +768 -0
- package/dist/validation/schema.test.js +35 -0
- package/dist/watcher.js +79 -0
- package/package.json +1 -1
|
@@ -0,0 +1,225 @@
|
|
|
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 path_1 = __importDefault(require("path"));
|
|
30
|
+
// @ts-expect-error TODO: type out if necessary
|
|
31
|
+
const Index_bs_js_1 = __importDefault(require("@float-capital/float-subgraph-uncrashable/src/Index.bs.js"));
|
|
32
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
33
|
+
const toolbox = __importStar(require("gluegun"));
|
|
34
|
+
const graphql = __importStar(require("graphql/language"));
|
|
35
|
+
const immutable_1 = __importDefault(require("immutable"));
|
|
36
|
+
const prettier_1 = __importDefault(require("prettier"));
|
|
37
|
+
const template_1 = __importDefault(require("./codegen/template"));
|
|
38
|
+
const typescript_1 = require("./codegen/typescript");
|
|
39
|
+
const fs_1 = require("./command-helpers/fs");
|
|
40
|
+
const spinner_1 = require("./command-helpers/spinner");
|
|
41
|
+
const migrations_1 = require("./migrations");
|
|
42
|
+
const schema_1 = __importDefault(require("./schema"));
|
|
43
|
+
const subgraph_1 = __importDefault(require("./subgraph"));
|
|
44
|
+
const watcher_1 = __importDefault(require("./watcher"));
|
|
45
|
+
class TypeGenerator {
|
|
46
|
+
sourceDir;
|
|
47
|
+
options;
|
|
48
|
+
protocol;
|
|
49
|
+
protocolTypeGenerator;
|
|
50
|
+
constructor(options) {
|
|
51
|
+
this.options = options;
|
|
52
|
+
this.sourceDir =
|
|
53
|
+
this.options.sourceDir ||
|
|
54
|
+
(this.options.subgraphManifest && path_1.default.dirname(this.options.subgraphManifest));
|
|
55
|
+
this.protocol = this.options.protocol;
|
|
56
|
+
this.protocolTypeGenerator = this.protocol?.getTypeGenerator?.({
|
|
57
|
+
sourceDir: this.sourceDir,
|
|
58
|
+
outputDir: this.options.outputDir,
|
|
59
|
+
});
|
|
60
|
+
process.on('uncaughtException', e => {
|
|
61
|
+
toolbox.print.error(`UNCAUGHT EXCEPTION: ${e}`);
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
async generateTypes() {
|
|
65
|
+
try {
|
|
66
|
+
if (!this.options.skipMigrations && this.options.subgraphManifest) {
|
|
67
|
+
await (0, migrations_1.applyMigrations)({
|
|
68
|
+
sourceDir: this.sourceDir,
|
|
69
|
+
manifestFile: this.options.subgraphManifest,
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
const subgraph = await this.loadSubgraph();
|
|
73
|
+
// Not all protocols support/have ABIs.
|
|
74
|
+
if (this.protocol.hasABIs()) {
|
|
75
|
+
const abis = await this.protocolTypeGenerator.loadABIs(subgraph);
|
|
76
|
+
await this.protocolTypeGenerator.generateTypesForABIs(abis);
|
|
77
|
+
}
|
|
78
|
+
await this.generateTypesForDataSourceTemplates(subgraph);
|
|
79
|
+
// Not all protocols support/have ABIs.
|
|
80
|
+
if (this.protocol.hasABIs()) {
|
|
81
|
+
const templateAbis = await this.protocolTypeGenerator.loadDataSourceTemplateABIs(subgraph);
|
|
82
|
+
await this.protocolTypeGenerator.generateTypesForDataSourceTemplateABIs(templateAbis);
|
|
83
|
+
}
|
|
84
|
+
const schema = await this.loadSchema(subgraph);
|
|
85
|
+
await this.generateTypesForSchema(schema);
|
|
86
|
+
toolbox.print.success('\nTypes generated successfully\n');
|
|
87
|
+
if (this.options.uncrashable && this.options.uncrashableConfig) {
|
|
88
|
+
await this.generateUncrashableEntities(schema);
|
|
89
|
+
toolbox.print.success('\nUncrashable Helpers generated successfully\n');
|
|
90
|
+
}
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
catch (e) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
async generateUncrashableEntities(graphSchema) {
|
|
98
|
+
const ast = graphql.parse(graphSchema.document);
|
|
99
|
+
const entityDefinitions = ast['definitions'];
|
|
100
|
+
return await (0, spinner_1.withSpinner)(`Generate Uncrashable Entity Helpers`, `Failed to generate Uncrashable Entity Helpers`, `Warnings while generating Uncrashable Entity Helpers`, async (spinner) => {
|
|
101
|
+
Index_bs_js_1.default.run(entityDefinitions, this.options.uncrashableConfig, this.options.outputDir);
|
|
102
|
+
const outputFile = path_1.default.join(this.options.outputDir, 'UncrashableEntityHelpers.ts');
|
|
103
|
+
(0, spinner_1.step)(spinner, 'Save uncrashable entities to', (0, fs_1.displayPath)(outputFile));
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
async loadSubgraph({ quiet } = { quiet: false }) {
|
|
107
|
+
const subgraphLoadOptions = { protocol: this.protocol, skipValidation: false };
|
|
108
|
+
if (quiet) {
|
|
109
|
+
return this.options.subgraph
|
|
110
|
+
? this.options.subgraph
|
|
111
|
+
: (await subgraph_1.default.load(this.options.subgraphManifest, subgraphLoadOptions)).result;
|
|
112
|
+
}
|
|
113
|
+
const manifestPath = (0, fs_1.displayPath)(this.options.subgraphManifest);
|
|
114
|
+
return await (0, spinner_1.withSpinner)(`Load subgraph from ${manifestPath}`, `Failed to load subgraph from ${manifestPath}`, `Warnings while loading subgraph from ${manifestPath}`, async (_spinner) => {
|
|
115
|
+
return this.options.subgraph
|
|
116
|
+
? this.options.subgraph
|
|
117
|
+
: subgraph_1.default.load(this.options.subgraphManifest, subgraphLoadOptions);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
async loadSchema(subgraph) {
|
|
121
|
+
const maybeRelativePath = subgraph.getIn(['schema', 'file']);
|
|
122
|
+
const absolutePath = path_1.default.resolve(this.sourceDir, maybeRelativePath);
|
|
123
|
+
return await (0, spinner_1.withSpinner)(`Load GraphQL schema from ${(0, fs_1.displayPath)(absolutePath)}`, `Failed to load GraphQL schema from ${(0, fs_1.displayPath)(absolutePath)}`, `Warnings while loading GraphQL schema from ${(0, fs_1.displayPath)(absolutePath)}`, async (_spinner) => {
|
|
124
|
+
const absolutePath = path_1.default.resolve(this.sourceDir, maybeRelativePath);
|
|
125
|
+
return schema_1.default.load(absolutePath);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
async generateTypesForSchema(schema) {
|
|
129
|
+
return await (0, spinner_1.withSpinner)(`Generate types for GraphQL schema`, `Failed to generate types for GraphQL schema`, `Warnings while generating types for GraphQL schema`, async (spinner) => {
|
|
130
|
+
// Generate TypeScript module from schema
|
|
131
|
+
const codeGenerator = schema.codeGenerator();
|
|
132
|
+
const code = prettier_1.default.format([
|
|
133
|
+
typescript_1.GENERATED_FILE_NOTE,
|
|
134
|
+
...codeGenerator.generateModuleImports(),
|
|
135
|
+
...codeGenerator.generateTypes(),
|
|
136
|
+
].join('\n'), {
|
|
137
|
+
parser: 'typescript',
|
|
138
|
+
});
|
|
139
|
+
const outputFile = path_1.default.join(this.options.outputDir, 'schema.ts');
|
|
140
|
+
(0, spinner_1.step)(spinner, 'Write types to', (0, fs_1.displayPath)(outputFile));
|
|
141
|
+
await fs_extra_1.default.mkdirs(path_1.default.dirname(outputFile));
|
|
142
|
+
await fs_extra_1.default.writeFile(outputFile, code);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
async generateTypesForDataSourceTemplates(subgraph) {
|
|
146
|
+
return await (0, spinner_1.withSpinner)(`Generate types for data source templates`, `Failed to generate types for data source templates`, `Warnings while generating types for data source templates`, async (spinner) => {
|
|
147
|
+
// Combine the generated code for all templates
|
|
148
|
+
const codeSegments = subgraph
|
|
149
|
+
.get('templates', immutable_1.default.List())
|
|
150
|
+
.reduce((codeSegments, template) => {
|
|
151
|
+
(0, spinner_1.step)(spinner, 'Generate types for data source template', String(template.get('name')));
|
|
152
|
+
const codeGenerator = new template_1.default(template, this.protocol);
|
|
153
|
+
// Only generate module imports once, because they are identical for
|
|
154
|
+
// all types generated for data source templates.
|
|
155
|
+
if (codeSegments.isEmpty()) {
|
|
156
|
+
codeSegments = codeSegments.concat(codeGenerator.generateModuleImports());
|
|
157
|
+
}
|
|
158
|
+
return codeSegments.concat(codeGenerator.generateTypes());
|
|
159
|
+
}, immutable_1.default.List());
|
|
160
|
+
if (!codeSegments.isEmpty()) {
|
|
161
|
+
const code = prettier_1.default.format([typescript_1.GENERATED_FILE_NOTE, ...codeSegments].join('\n'), {
|
|
162
|
+
parser: 'typescript',
|
|
163
|
+
});
|
|
164
|
+
const outputFile = path_1.default.join(this.options.outputDir, 'templates.ts');
|
|
165
|
+
(0, spinner_1.step)(spinner, `Write types for templates to`, (0, fs_1.displayPath)(outputFile));
|
|
166
|
+
await fs_extra_1.default.mkdirs(path_1.default.dirname(outputFile));
|
|
167
|
+
await fs_extra_1.default.writeFile(outputFile, code);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
async getFilesToWatch() {
|
|
172
|
+
try {
|
|
173
|
+
const files = [];
|
|
174
|
+
const subgraph = await this.loadSubgraph({ quiet: true });
|
|
175
|
+
// Add the subgraph manifest file
|
|
176
|
+
files.push(this.options.subgraphManifest);
|
|
177
|
+
// Add the GraphQL schema to the watched files
|
|
178
|
+
files.push(subgraph.getIn(['schema', 'file']));
|
|
179
|
+
// Add all file paths specified in manifest
|
|
180
|
+
subgraph.get('dataSources').map((dataSource) => {
|
|
181
|
+
dataSource.getIn(['mapping', 'abis']).map((abi) => {
|
|
182
|
+
files.push(abi.get('file'));
|
|
183
|
+
});
|
|
184
|
+
});
|
|
185
|
+
// Make paths absolute
|
|
186
|
+
return files.map(file => path_1.default.resolve(file));
|
|
187
|
+
}
|
|
188
|
+
catch (e) {
|
|
189
|
+
throw Error(`Failed to load subgraph: ${e.message}`);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
async watchAndGenerateTypes() {
|
|
193
|
+
const generator = this;
|
|
194
|
+
let spinner;
|
|
195
|
+
// Create watcher and generate types once and then on every change to a watched file
|
|
196
|
+
const watcher = new watcher_1.default({
|
|
197
|
+
onReady: () => (spinner = toolbox.print.spin('Watching subgraph files')),
|
|
198
|
+
onTrigger: async (changedFile) => {
|
|
199
|
+
if (changedFile !== undefined) {
|
|
200
|
+
spinner.info(`File change detected: ${(0, fs_1.displayPath)(changedFile)}\n`);
|
|
201
|
+
}
|
|
202
|
+
await generator.generateTypes();
|
|
203
|
+
spinner.start();
|
|
204
|
+
},
|
|
205
|
+
onCollectFiles: async () => await generator.getFilesToWatch(),
|
|
206
|
+
onError: error => {
|
|
207
|
+
spinner.stop();
|
|
208
|
+
toolbox.print.error(`${error}\n`);
|
|
209
|
+
spinner.start();
|
|
210
|
+
},
|
|
211
|
+
});
|
|
212
|
+
// Catch keyboard interrupt: close watcher and exit process
|
|
213
|
+
process.on('SIGINT', () => {
|
|
214
|
+
watcher.close();
|
|
215
|
+
process.exit();
|
|
216
|
+
});
|
|
217
|
+
try {
|
|
218
|
+
await watcher.watch();
|
|
219
|
+
}
|
|
220
|
+
catch (e) {
|
|
221
|
+
toolbox.print.error(String(e.message));
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
exports.default = TypeGenerator;
|
|
@@ -0,0 +1,44 @@
|
|
|
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.validateContractValues = exports.validateContract = void 0;
|
|
7
|
+
const immutable_1 = __importDefault(require("immutable"));
|
|
8
|
+
const validateContract = (value, ProtocolContract) => {
|
|
9
|
+
const contract = new ProtocolContract(value);
|
|
10
|
+
const { valid, error } = contract.validate();
|
|
11
|
+
if (!valid) {
|
|
12
|
+
return {
|
|
13
|
+
valid,
|
|
14
|
+
error: `Contract ${ProtocolContract.identifierName()} is invalid: ${value}\n${error}`,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
return { valid, error };
|
|
18
|
+
};
|
|
19
|
+
exports.validateContract = validateContract;
|
|
20
|
+
const validateContractValues = (manifest, protocol) => {
|
|
21
|
+
const ProtocolContract = protocol.getContract();
|
|
22
|
+
const fieldName = ProtocolContract.identifierName();
|
|
23
|
+
return manifest
|
|
24
|
+
.get('dataSources')
|
|
25
|
+
.filter((dataSource) => protocol.isValidKindName(dataSource.get('kind')))
|
|
26
|
+
.reduce((errors, dataSource, dataSourceIndex) => {
|
|
27
|
+
const path = ['dataSources', dataSourceIndex, 'source', fieldName];
|
|
28
|
+
// No need to validate if the source has no contract field
|
|
29
|
+
if (!dataSource.get('source').has(fieldName)) {
|
|
30
|
+
return errors;
|
|
31
|
+
}
|
|
32
|
+
const contractValue = dataSource.getIn(['source', fieldName]);
|
|
33
|
+
const { valid, error } = (0, exports.validateContract)(contractValue, ProtocolContract);
|
|
34
|
+
// Validate whether the contract is valid for the protocol
|
|
35
|
+
if (valid) {
|
|
36
|
+
return errors;
|
|
37
|
+
}
|
|
38
|
+
return errors.push(immutable_1.default.fromJS({
|
|
39
|
+
path,
|
|
40
|
+
message: error,
|
|
41
|
+
}));
|
|
42
|
+
}, immutable_1.default.List());
|
|
43
|
+
};
|
|
44
|
+
exports.validateContractValues = validateContractValues;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateSchema = exports.validateManifest = exports.validateContractValues = exports.validateContract = void 0;
|
|
4
|
+
const contract_1 = require("./contract");
|
|
5
|
+
Object.defineProperty(exports, "validateContract", { enumerable: true, get: function () { return contract_1.validateContract; } });
|
|
6
|
+
Object.defineProperty(exports, "validateContractValues", { enumerable: true, get: function () { return contract_1.validateContractValues; } });
|
|
7
|
+
const manifest_1 = require("./manifest");
|
|
8
|
+
Object.defineProperty(exports, "validateManifest", { enumerable: true, get: function () { return manifest_1.validateManifest; } });
|
|
9
|
+
const schema_1 = require("./schema");
|
|
10
|
+
Object.defineProperty(exports, "validateSchema", { enumerable: true, get: function () { return schema_1.validateSchema; } });
|
|
@@ -0,0 +1,266 @@
|
|
|
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.validateManifest = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
const immutable_1 = __importDefault(require("immutable"));
|
|
10
|
+
const js_yaml_1 = __importDefault(require("js-yaml"));
|
|
11
|
+
const protocols_1 = __importDefault(require("../protocols"));
|
|
12
|
+
const List = immutable_1.default.List;
|
|
13
|
+
const Map = immutable_1.default.Map;
|
|
14
|
+
/**
|
|
15
|
+
* Returns a user-friendly type name for a value.
|
|
16
|
+
*/
|
|
17
|
+
const typeName = (value) => List.isList(value) ? 'list' : Map.isMap(value) ? 'map' : typeof value;
|
|
18
|
+
/**
|
|
19
|
+
* Converts an immutable or plain JavaScript value to a YAML string.
|
|
20
|
+
*/
|
|
21
|
+
const toYAML = (x) => js_yaml_1.default
|
|
22
|
+
.safeDump(typeName(x) === 'list' || typeName(x) === 'map' ? x.toJS() : x, {
|
|
23
|
+
indent: 2,
|
|
24
|
+
})
|
|
25
|
+
.trim();
|
|
26
|
+
/**
|
|
27
|
+
* Looks up the type of a field in a GraphQL object type.
|
|
28
|
+
*/
|
|
29
|
+
const getFieldType = (type, fieldName) => {
|
|
30
|
+
const fieldDef = type
|
|
31
|
+
.get('fields')
|
|
32
|
+
.find((field) => field.getIn(['name', 'value']) === fieldName);
|
|
33
|
+
return fieldDef !== undefined ? fieldDef.get('type') : undefined;
|
|
34
|
+
};
|
|
35
|
+
/**
|
|
36
|
+
* Resolves a type in the GraphQL schema.
|
|
37
|
+
*/
|
|
38
|
+
const resolveType = (schema, type) => type.has('type')
|
|
39
|
+
? resolveType(schema, type.get('type'))
|
|
40
|
+
: type.get('kind') === 'NamedType'
|
|
41
|
+
? schema
|
|
42
|
+
.get('definitions')
|
|
43
|
+
.find((def) => def.getIn(['name', 'value']) === type.getIn(['name', 'value']))
|
|
44
|
+
: 'resolveType: unimplemented';
|
|
45
|
+
/**
|
|
46
|
+
* A map of supported validators.
|
|
47
|
+
*/
|
|
48
|
+
const validators = immutable_1.default.fromJS({
|
|
49
|
+
ScalarTypeDefinition: (value, ctx) => validators.get(ctx.getIn(['type', 'name', 'value']))(value, ctx),
|
|
50
|
+
UnionTypeDefinition: (value, ctx) => {
|
|
51
|
+
const unionVariants = ctx.getIn(['type', 'types']);
|
|
52
|
+
let errors = List();
|
|
53
|
+
for (const variantType of unionVariants) {
|
|
54
|
+
const variantErrors = validateValue(value, ctx.set('type', variantType));
|
|
55
|
+
// No errors found, union variant matched, early return
|
|
56
|
+
if (variantErrors.isEmpty()) {
|
|
57
|
+
return List();
|
|
58
|
+
}
|
|
59
|
+
errors = errors.push(variantErrors);
|
|
60
|
+
}
|
|
61
|
+
// Return errors from variant that matched the most
|
|
62
|
+
return List(errors.minBy((variantErrors) => variantErrors.count()));
|
|
63
|
+
},
|
|
64
|
+
NamedType: (value, ctx) => validateValue(value, ctx.update('type', type => resolveType(ctx.get('schema'), type))),
|
|
65
|
+
NonNullType: (value, ctx) => value !== null && value !== undefined
|
|
66
|
+
? validateValue(value, ctx.update('type', type => type.get('type')))
|
|
67
|
+
: immutable_1.default.fromJS([
|
|
68
|
+
{
|
|
69
|
+
path: ctx.get('path'),
|
|
70
|
+
message: `No value provided`,
|
|
71
|
+
},
|
|
72
|
+
]),
|
|
73
|
+
ListType: (value, ctx) => List.isList(value)
|
|
74
|
+
? value.reduce((errors, value, i) => errors.concat(validateValue(value, ctx.update('path', path => path.push(i)).update('type', type => type.get('type')))), List())
|
|
75
|
+
: immutable_1.default.fromJS([
|
|
76
|
+
{
|
|
77
|
+
path: ctx.get('path'),
|
|
78
|
+
message: `Expected list, found ${typeName(value)}:\n${toYAML(value)}`,
|
|
79
|
+
},
|
|
80
|
+
]),
|
|
81
|
+
ObjectTypeDefinition: (value, ctx) => {
|
|
82
|
+
return Map.isMap(value)
|
|
83
|
+
? ctx
|
|
84
|
+
.getIn(['type', 'fields'])
|
|
85
|
+
.map((fieldDef) => fieldDef.getIn(['name', 'value']))
|
|
86
|
+
.concat(value.keySeq())
|
|
87
|
+
.toSet()
|
|
88
|
+
.reduce((errors, key) => getFieldType(ctx.get('type'), key)
|
|
89
|
+
? errors.concat(validateValue(value.get(key), ctx
|
|
90
|
+
.update('path', (path) => path.push(key))
|
|
91
|
+
.set('type', getFieldType(ctx.get('type'), key))))
|
|
92
|
+
: errors.push(key == 'templates' && ctx.get('protocol').hasTemplates()
|
|
93
|
+
? immutable_1.default.fromJS({
|
|
94
|
+
path: ctx.get('path'),
|
|
95
|
+
message: `The way to declare data source templates has changed, ` +
|
|
96
|
+
`please move the templates from inside data sources to ` +
|
|
97
|
+
`a \`templates:\` field at the top level of the manifest.`,
|
|
98
|
+
})
|
|
99
|
+
: immutable_1.default.fromJS({
|
|
100
|
+
path: ctx.get('path'),
|
|
101
|
+
message: `Unexpected key in map: ${key}`,
|
|
102
|
+
})), List())
|
|
103
|
+
: immutable_1.default.fromJS([
|
|
104
|
+
{
|
|
105
|
+
path: ctx.get('path'),
|
|
106
|
+
message: `Expected map, found ${typeName(value)}:\n${toYAML(value)}`,
|
|
107
|
+
},
|
|
108
|
+
]);
|
|
109
|
+
},
|
|
110
|
+
EnumTypeDefinition: (value, ctx) => {
|
|
111
|
+
const enumValues = ctx.getIn(['type', 'values']).map((v) => {
|
|
112
|
+
return v.getIn(['name', 'value']);
|
|
113
|
+
});
|
|
114
|
+
const allowedValues = enumValues.toArray().join(', ');
|
|
115
|
+
return enumValues.includes(value)
|
|
116
|
+
? List()
|
|
117
|
+
: immutable_1.default.fromJS([
|
|
118
|
+
{
|
|
119
|
+
path: ctx.get('path'),
|
|
120
|
+
message: `Unexpected enum value: ${value}, allowed values: ${allowedValues}`,
|
|
121
|
+
},
|
|
122
|
+
]);
|
|
123
|
+
},
|
|
124
|
+
String: (value, ctx) => typeof value === 'string'
|
|
125
|
+
? List()
|
|
126
|
+
: immutable_1.default.fromJS([
|
|
127
|
+
{
|
|
128
|
+
path: ctx.get('path'),
|
|
129
|
+
message: `Expected string, found ${typeName(value)}:\n${toYAML(value)}`,
|
|
130
|
+
},
|
|
131
|
+
]),
|
|
132
|
+
BigInt: (value, ctx) => typeof value === 'number'
|
|
133
|
+
? List()
|
|
134
|
+
: immutable_1.default.fromJS([
|
|
135
|
+
{
|
|
136
|
+
path: ctx.get('path'),
|
|
137
|
+
message: `Expected BigInt, found ${typeName(value)}:\n${toYAML(value)}`,
|
|
138
|
+
},
|
|
139
|
+
]),
|
|
140
|
+
File: (value, ctx) => typeof value === 'string'
|
|
141
|
+
? fs_1.default.existsSync(ctx.get('resolveFile')(value))
|
|
142
|
+
? List()
|
|
143
|
+
: immutable_1.default.fromJS([
|
|
144
|
+
{
|
|
145
|
+
path: ctx.get('path'),
|
|
146
|
+
message: `File does not exist: ${path_1.default.relative(process.cwd(), value)}`,
|
|
147
|
+
},
|
|
148
|
+
])
|
|
149
|
+
: immutable_1.default.fromJS([
|
|
150
|
+
{
|
|
151
|
+
path: ctx.get('path'),
|
|
152
|
+
message: `Expected filename, found ${typeName(value)}:\n${value}`,
|
|
153
|
+
},
|
|
154
|
+
]),
|
|
155
|
+
Boolean: (value, ctx) => typeof value === 'boolean'
|
|
156
|
+
? List()
|
|
157
|
+
: immutable_1.default.fromJS([
|
|
158
|
+
{
|
|
159
|
+
path: ctx.get('path'),
|
|
160
|
+
message: `Expected true or false, found ${typeName(value)}:\n${toYAML(value)}`,
|
|
161
|
+
},
|
|
162
|
+
]),
|
|
163
|
+
});
|
|
164
|
+
const validateValue = (value, ctx) => {
|
|
165
|
+
const kind = ctx.getIn(['type', 'kind']);
|
|
166
|
+
const validator = validators.get(kind);
|
|
167
|
+
if (validator !== undefined) {
|
|
168
|
+
// If the type is nullable, accept undefined and null; if the nullable
|
|
169
|
+
// type is wrapped in a `NonNullType`, the validator for that `NonNullType`
|
|
170
|
+
// will catch the missing/unset value
|
|
171
|
+
if (kind !== 'NonNullType' && (value === undefined || value === null)) {
|
|
172
|
+
return List();
|
|
173
|
+
}
|
|
174
|
+
return validator(value, ctx);
|
|
175
|
+
}
|
|
176
|
+
return immutable_1.default.fromJS([
|
|
177
|
+
{
|
|
178
|
+
path: ctx.get('path'),
|
|
179
|
+
message: `No validator for unsupported schema type: ${kind}`,
|
|
180
|
+
},
|
|
181
|
+
]);
|
|
182
|
+
};
|
|
183
|
+
// Transforms list of data sources like this:
|
|
184
|
+
// [
|
|
185
|
+
// { name: 'contract0', kind: 'ethereum/contract', network: 'mainnet' },
|
|
186
|
+
// { name: 'contract1', kind: 'ethereum', network: 'mainnet' },
|
|
187
|
+
// { name: 'contract2', kind: 'ethereum/contract', network: 'gnosis' },
|
|
188
|
+
// { name: 'contract3', kind: 'near', network: 'near-mainnet' },
|
|
189
|
+
// ]
|
|
190
|
+
//
|
|
191
|
+
// Into Immutable JS structure like this (protocol kind is normalized):
|
|
192
|
+
// {
|
|
193
|
+
// ethereum: {
|
|
194
|
+
// mainnet: ['contract0', 'contract1'],
|
|
195
|
+
// gnosis: ['contract2'],
|
|
196
|
+
// },
|
|
197
|
+
// near: {
|
|
198
|
+
// 'near-mainnet': ['contract3'],
|
|
199
|
+
// },
|
|
200
|
+
// }
|
|
201
|
+
const dataSourceListToMap = (dataSources) => dataSources.reduce((protocolKinds, dataSource) => protocolKinds.update(protocols_1.default.normalizeName(dataSource.kind), (networks) => (networks || immutable_1.default.OrderedMap()).update(dataSource.network, (dataSourceNames) => (dataSourceNames || immutable_1.default.OrderedSet()).add(dataSource.name))), immutable_1.default.OrderedMap());
|
|
202
|
+
const validateDataSourceProtocolAndNetworks = (value) => {
|
|
203
|
+
const dataSources = [...value.dataSources, ...(value.templates || [])];
|
|
204
|
+
const protocolNetworkMap = dataSourceListToMap(dataSources);
|
|
205
|
+
if (protocolNetworkMap.size > 1) {
|
|
206
|
+
return immutable_1.default.fromJS([
|
|
207
|
+
{
|
|
208
|
+
path: [],
|
|
209
|
+
message: `Conflicting protocol kinds used in data sources and templates:
|
|
210
|
+
${protocolNetworkMap
|
|
211
|
+
.map((dataSourceNames, protocolKind) => ` ${protocolKind === undefined
|
|
212
|
+
? 'Data sources and templates having no protocol kind set'
|
|
213
|
+
: `Data sources and templates using '${protocolKind}'`}:\n${dataSourceNames
|
|
214
|
+
.valueSeq()
|
|
215
|
+
.flatten()
|
|
216
|
+
.map((ds) => ` - ${ds}`)
|
|
217
|
+
.join('\n')}`)
|
|
218
|
+
.join('\n')}
|
|
219
|
+
Recommendation: Make all data sources and templates use the same protocol kind.`,
|
|
220
|
+
},
|
|
221
|
+
]);
|
|
222
|
+
}
|
|
223
|
+
const networks = protocolNetworkMap.first();
|
|
224
|
+
if (networks.size > 1) {
|
|
225
|
+
return immutable_1.default.fromJS([
|
|
226
|
+
{
|
|
227
|
+
path: [],
|
|
228
|
+
message: `Conflicting networks used in data sources and templates:
|
|
229
|
+
${networks
|
|
230
|
+
.map((dataSources, network) => ` ${network === undefined
|
|
231
|
+
? 'Data sources and templates having no network set'
|
|
232
|
+
: `Data sources and templates using '${network}'`}:\n${dataSources.map((ds) => ` - ${ds}`).join('\n')}`)
|
|
233
|
+
.join('\n')}
|
|
234
|
+
Recommendation: Make all data sources and templates use the same network name.`,
|
|
235
|
+
},
|
|
236
|
+
]);
|
|
237
|
+
}
|
|
238
|
+
return List();
|
|
239
|
+
};
|
|
240
|
+
const validateManifest = (value, type, schema, protocol, { resolveFile }) => {
|
|
241
|
+
// Validate manifest using the GraphQL schema that defines its structure
|
|
242
|
+
const errors = value !== null && value !== undefined
|
|
243
|
+
? validateValue(immutable_1.default.fromJS(value), immutable_1.default.fromJS({
|
|
244
|
+
schema,
|
|
245
|
+
type,
|
|
246
|
+
path: [],
|
|
247
|
+
errors: [],
|
|
248
|
+
resolveFile,
|
|
249
|
+
protocol,
|
|
250
|
+
}))
|
|
251
|
+
: immutable_1.default.fromJS([
|
|
252
|
+
{
|
|
253
|
+
path: [],
|
|
254
|
+
message: `Expected non-empty value, found ${typeName(value)}:\n ${value}`,
|
|
255
|
+
},
|
|
256
|
+
]);
|
|
257
|
+
// Fail early because a broken manifest prevents us from performing
|
|
258
|
+
// additional validation steps
|
|
259
|
+
if (!errors.isEmpty()) {
|
|
260
|
+
return errors;
|
|
261
|
+
}
|
|
262
|
+
// Validate that all data sources are for the same `network` and `protocol` (kind)
|
|
263
|
+
// (this includes _no_ network/protocol at all)
|
|
264
|
+
return validateDataSourceProtocolAndNetworks(value);
|
|
265
|
+
};
|
|
266
|
+
exports.validateManifest = validateManifest;
|