Skip to content

Commit

Permalink
feat: validate graphql schema at build time
Browse files Browse the repository at this point in the history
  • Loading branch information
saihaj committed Dec 13, 2023
1 parent 45f3197 commit 055b47e
Show file tree
Hide file tree
Showing 4 changed files with 436 additions and 47 deletions.
4 changes: 4 additions & 0 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@
"@babel/core": "^7.20.5",
"@babel/preset-typescript": "^7.18.6",
"@float-capital/float-subgraph-uncrashable": "^0.0.0-alpha.4",
"@graphql-tools/graphql-file-loader": "^8.0.0",
"@graphql-tools/load": "^8.0.1",
"@graphql-tools/schema": "^10.0.2",
"@graphql-tools/url-loader": "^8.0.1",
"@oclif/core": "2.8.6",
"@oclif/plugin-autocomplete": "^2.3.6",
"@oclif/plugin-not-found": "^2.4.0",
Expand Down
6 changes: 3 additions & 3 deletions packages/cli/src/subgraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ export default class Subgraph {
});
}

static validateSchema(manifest: any, { resolveFile }: { resolveFile: ResolveFile }) {
static async validateSchema(manifest: any, { resolveFile }: { resolveFile: ResolveFile }) {
subgraphDebug.extend('validate')('Validating schema in manifest');
const filename = resolveFile(manifest.getIn(['schema', 'file']));
subgraphDebug.extend('validate')('Loaded schema from %s', filename);
const validationErrors = validation.validateSchema(filename);
const validationErrors = await validation.validateSchema(filename);
let errors: immutable.Collection<any, any>;

if (validationErrors.size > 0) {
Expand Down Expand Up @@ -296,7 +296,7 @@ More than one template named '${name}', template names must be unique.`,

// Validate the schema
subgraphDebug.extend('manifest')('Validating schema');
Subgraph.validateSchema(manifest, { resolveFile });
await Subgraph.validateSchema(manifest, { resolveFile });

// Perform other validations
const protocolSubgraph = protocol.getSubgraph({
Expand Down
42 changes: 37 additions & 5 deletions packages/cli/src/validation/schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import fs from 'fs';
import * as fs from 'node:fs';
import * as graphql from 'graphql/language';
import immutable from 'immutable';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { loadSchema as toolsLoadSchema } from '@graphql-tools/load';
import { mergeSchemas } from '@graphql-tools/schema';
import { UrlLoader } from '@graphql-tools/url-loader';
import debugFactory from '../debug';
import fetchWrapper from '../fetch';

const validateDebugger = debugFactory('graph-cli:validation');

Expand Down Expand Up @@ -1041,11 +1046,38 @@ const validateNamingCollisions = (local: any[], imported: any) => {
return validateNamingCollisionsInTypes(local.concat(imported));
};

export const validateSchema = (filename: string) => {
// TODO - temporarily not using `master` see https://github.com/graphprotocol/graph-node/pull/5069
// const META_SCHEMA_GRAPH_NODE= "https://raw.githubusercontent.com/graphprotocol/graph-node/master/graph/src/schema/meta.graphql"
const META_SCHEMA_GRAPH_NODE =
'https://raw.githubusercontent.com/graphprotocol/graph-node/saihaj/fix-meta-schema/graph/src/schema/meta.graphql';

export const validateSchema = async (filename: string) => {
validateDebugger('Validating schema: %s', filename);
const doc = loadSchema(filename);
validateDebugger('Loaded schema: %s', filename);
const schema = parseSchema(doc);

try {
const [doc, metaSchema] = await Promise.all([
toolsLoadSchema(filename, {
loaders: [new GraphQLFileLoader()],
assumeValid: true,
assumeValidSDL: true,
}),
toolsLoadSchema(META_SCHEMA_GRAPH_NODE, {
loaders: [new UrlLoader()],
fetch: fetchWrapper,
}),
]);
console.log('doc', doc);

Check failure on line 1069 in packages/cli/src/validation/schema.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
validateDebugger('Loaded schema: %s', filename);

const b = mergeSchemas({
schemas: [metaSchema],
});
console.log(b);

Check failure on line 1075 in packages/cli/src/validation/schema.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
} catch (e) {
console.trace('Error', e);

Check failure on line 1077 in packages/cli/src/validation/schema.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected console statement
}

const schema = parseSchema(loadSchema(filename));
validateDebugger.extend('schema')('Parsed schema: %M', schema);

return List.of(
Expand Down
Loading

0 comments on commit 055b47e

Please sign in to comment.