@graphprotocol/graph-cli
Version:
CLI for building for and deploying to The Graph
40 lines (39 loc) • 1.26 kB
JavaScript
import fs from 'fs-extra';
import * as graphql from 'graphql/language/index.js';
import SchemaCodeGenerator from './codegen/schema.js';
export default class Schema {
document;
ast;
filename;
constructor(document, ast, filename) {
this.document = document;
this.ast = ast;
this.filename = filename;
this.filename = filename;
this.document = document;
this.ast = ast;
}
codeGenerator() {
return new SchemaCodeGenerator(this);
}
static async load(filename) {
const document = await fs.readFile(filename, 'utf-8');
const ast = graphql.parse(document);
return new Schema(document, ast, filename);
}
static async loadFromString(document) {
try {
const ast = graphql.parse(document);
return new Schema(document, ast);
}
catch (e) {
throw new Error(`Failed to load schema from string: ${e.message}`);
}
}
getEntityNames() {
return this.ast.definitions
.filter(def => def.kind === 'ObjectTypeDefinition' &&
def.directives?.find(directive => directive.name.value === 'entity') !== undefined)
.map(entity => entity.name.value);
}
}