UNPKG

11.5 kBJavaScriptView Raw
1"use strict";
2var __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}));
13var __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});
18var __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};
25var __importDefault = (this && this.__importDefault) || function (mod) {
26 return (mod && mod.__esModule) ? mod : { "default": mod };
27};
28Object.defineProperty(exports, "__esModule", { value: true });
29const path_1 = __importDefault(require("path"));
30const fs_extra_1 = __importDefault(require("fs-extra"));
31const graphql = __importStar(require("graphql/language"));
32const immutable_1 = __importDefault(require("immutable"));
33const yaml_1 = __importDefault(require("yaml"));
34const types_1 = require("yaml/types");
35const debug_1 = __importDefault(require("./debug"));
36const validation = __importStar(require("./validation"));
37const manifest_1 = require("./manifest");
38const subgraphDebug = (0, debug_1.default)('graph-cli:subgraph');
39const throwCombinedError = (filename, errors) => {
40 throw new Error(errors.reduce((msg, e) => `${msg}
41
42 Path: ${e.path.length === 0 ? '/' : e.path.join(' > ')}
43 ${e.message.split('\n').join('\n ')}`, `Error in ${path_1.default.relative(process.cwd(), filename)}:`));
44};
45const buildCombinedWarning = (filename, warnings) => warnings.length > 0
46 ? warnings.reduce((msg, w) => `${msg}
47
48 Path: ${w.path.length === 0 ? '/' : w.path.join(' > ')}
49 ${w.message.split('\n').join('\n ')}`, `Warnings in ${path_1.default.relative(process.cwd(), filename)}:`) + '\n'
50 : null;
51class Subgraph {
52 static async validate(data, protocol, { resolveFile }) {
53 subgraphDebug(`Validating Subgraph with protocol "%s"`, protocol);
54 if (protocol.name == null) {
55 return immutable_1.default.fromJS([
56 {
57 path: [],
58 message: `Unable to determine for which protocol manifest file is built for. Ensure you have at least one 'dataSources' and/or 'templates' elements defined in your subgraph.`,
59 },
60 ]);
61 }
62 // Parse the default subgraph schema
63 const schema = graphql.parse(await fs_extra_1.default.readFile(path_1.default.join(__dirname, 'protocols', protocol.name, `manifest.graphql`), 'utf-8'));
64 // Obtain the root `SubgraphManifest` type from the schema
65 const rootType = schema.definitions.find(definition => {
66 // @ts-expect-error TODO: name field does not exist on definition, really?
67 return definition.name.value === 'SubgraphManifest';
68 });
69 // Validate the subgraph manifest using this schema
70 return validation.validateManifest(data, rootType, schema, protocol, {
71 resolveFile,
72 });
73 }
74 static validateSchema(manifest, { resolveFile }) {
75 const filename = resolveFile(manifest.schema.file);
76 const validationErrors = validation.validateSchema(filename);
77 let errors;
78 if (validationErrors.size > 0) {
79 errors = validationErrors.groupBy(error => error.get('entity')).sort();
80 const msg = errors.reduce((msg, errors, entity) => {
81 errors = errors.groupBy((error) => error.get('directive'));
82 const inner_msgs = errors.reduce((msg, errors, directive) => {
83 return `${msg}${directive
84 ? `
85 ${directive}:`
86 : ''}
87 ${errors
88 .map(error => error.get('message').split('\n').join('\n '))
89 .map(msg => `${directive ? ' ' : ''}- ${msg}`)
90 .join('\n ')}`;
91 }, ``);
92 return `${msg}
93
94 ${entity}:${inner_msgs}`;
95 }, `Error in ${path_1.default.relative(process.cwd(), filename)}:`);
96 throw new Error(msg);
97 }
98 }
99 static validateRepository(manifest) {
100 const repository = manifest.repository;
101 // repository is optional, so no need to throw error if it's not set
102 if (!repository)
103 return [];
104 return /^https:\/\/github\.com\/graphprotocol\/graph-tooling?$/.test(repository) ||
105 // For legacy reasons, we should error on example subgraphs
106 /^https:\/\/github\.com\/graphprotocol\/example-subgraphs?$/.test(repository)
107 ? [
108 {
109 path: ['repository'],
110 message: `\
111The repository is still set to ${repository}.
112Please replace it with a link to your subgraph source code.`,
113 },
114 ]
115 : [];
116 }
117 static validateDescription(manifest) {
118 // TODO: Maybe implement this in the future for each protocol example description
119 return (manifest?.description || '').startsWith('Gravatar for ')
120 ? [
121 {
122 path: ['description'],
123 message: `\
124The description is still the one from the example subgraph.
125Please update it to tell users more about your subgraph.`,
126 },
127 ]
128 : [];
129 }
130 static validateHandlers(manifest, protocol, protocolSubgraph) {
131 return manifest.dataSources
132 .filter(dataSource => protocol.isValidKindName(dataSource.kind))
133 .reduce((errors, dataSource, dataSourceIndex) => {
134 const path = ['dataSources', dataSourceIndex, 'mapping'];
135 const mapping = dataSource.mapping;
136 const handlerTypes = protocolSubgraph.handlerTypes();
137 subgraphDebug('Validating dataSource "%s" handlers with %d handlers types defined for protocol', dataSource.name, handlerTypes.size);
138 if (handlerTypes.size == 0) {
139 return errors;
140 }
141 const areAllHandlersEmpty = handlerTypes
142 // @ts-expect-error TODO: handlerTypes needs to be improved
143 .map(handlerType => mapping?.[handlerType] || [])
144 .every(handlers => handlers.length === 0);
145 const handlerNamesWithoutLast = handlerTypes.pop().join(', ');
146 return areAllHandlersEmpty
147 ? errors.push({
148 path,
149 message: `\
150Mapping has no ${handlerNamesWithoutLast} or ${handlerTypes.get(-1)}.
151At least one such handler must be defined.`,
152 })
153 : errors;
154 }, []);
155 }
156 static validateContractValues(manifest, protocol) {
157 if (!protocol.hasContract()) {
158 return [];
159 }
160 return validation.validateContractValues(manifest, protocol);
161 }
162 // Validate that data source names are unique, so they don't overwrite each other.
163 static validateUniqueDataSourceNames(manifest) {
164 const names = [];
165 return manifest.dataSources.reduce((errors, dataSource, dataSourceIndex) => {
166 const path = ['dataSources', dataSourceIndex, 'name'];
167 const name = dataSource.name;
168 if (names.includes(name)) {
169 errors = errors.push({
170 path,
171 message: `\
172More than one data source named '${name}', data source names must be unique.`,
173 });
174 }
175 names.push(name);
176 return errors;
177 }, []);
178 }
179 static validateUniqueTemplateNames(manifest) {
180 const names = [];
181 return (manifest?.templates || []).reduce((errors, template, templateIndex) => {
182 const path = ['templates', templateIndex, 'name'];
183 const name = template.name;
184 if (names.includes(name)) {
185 errors = errors.push({
186 path,
187 message: `\
188More than one template named '${name}', template names must be unique.`,
189 });
190 }
191 names.push(name);
192 return errors;
193 }, []);
194 }
195 static dump(manifest) {
196 types_1.strOptions.fold.lineWidth = 90;
197 // @ts-expect-error TODO: plain is the value behind the TS constant
198 types_1.strOptions.defaultType = 'PLAIN';
199 return yaml_1.default.stringify(manifest);
200 }
201 static async load(filename, { protocol, skipValidation } = {
202 skipValidation: false,
203 }) {
204 // Load and validate the manifest
205 let data = null;
206 let has_file_data_sources = false;
207 if (filename.match(/.js$/)) {
208 data = require(path_1.default.resolve(filename));
209 }
210 else {
211 const raw_data = await fs_extra_1.default.readFile(filename, 'utf-8');
212 has_file_data_sources = raw_data.includes('kind: file');
213 data = yaml_1.default.parse(raw_data);
214 }
215 // Helper to resolve files relative to the subgraph manifest
216 const resolveFile = maybeRelativeFile => path_1.default.resolve(path_1.default.dirname(filename), maybeRelativeFile);
217 // TODO: Validation for file data sources
218 if (!has_file_data_sources) {
219 const manifestErrors = await Subgraph.validate(data, protocol, {
220 resolveFile,
221 });
222 if (manifestErrors.size > 0) {
223 throwCombinedError(filename, manifestErrors);
224 }
225 }
226 const manifestSchema = manifest_1.Manifest.safeParse(data);
227 if (!manifestSchema.success) {
228 throw new Error(manifestSchema.error.message);
229 }
230 const manifest = manifestSchema.data;
231 // Validate the schema
232 Subgraph.validateSchema(manifest, { resolveFile });
233 // Perform other validations
234 const protocolSubgraph = protocol.getSubgraph({
235 manifest,
236 resolveFile,
237 });
238 const errors = skipValidation
239 ? []
240 : [
241 ...protocolSubgraph.validateManifest(),
242 ...Subgraph.validateContractValues(manifest, protocol),
243 ...Subgraph.validateUniqueDataSourceNames(manifest),
244 ...Subgraph.validateUniqueTemplateNames(manifest),
245 ...Subgraph.validateHandlers(manifest, protocol, protocolSubgraph),
246 ];
247 if (errors.length > 0) {
248 throwCombinedError(filename, errors);
249 }
250 // Perform warning validations
251 const warnings = skipValidation
252 ? []
253 : [...Subgraph.validateRepository(manifest), ...Subgraph.validateDescription(manifest)];
254 return {
255 result: manifest,
256 warning: warnings.length > 0 ? buildCombinedWarning(filename, warnings) : null,
257 };
258 }
259 static async write(manifest, filename) {
260 await fs_extra_1.default.writeFile(filename, Subgraph.dump(manifest));
261 }
262}
263exports.default = Subgraph;