From 05db435c00a0709e23e893346e37568cec7d2291 Mon Sep 17 00:00:00 2001 From: omreego Date: Tue, 9 Jan 2024 09:25:22 -0800 Subject: [PATCH] Flows store --- src/adapter/index.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/adapter/index.ts b/src/adapter/index.ts index 12aef84..6127b1f 100644 --- a/src/adapter/index.ts +++ b/src/adapter/index.ts @@ -300,6 +300,7 @@ export function asV2AST(ast: gql.ASTNode, typeInfo: gql.TypeInfo): gql.ASTNode { })); } + /** Shim that retrieves data from a V2 graph using a V1 query */ export class QueryAdapter { constructor( @@ -420,10 +421,28 @@ export class QueryAdapter { args: Map = new Map(), postProcessV2Query: (v2Query: string) => string = _.identity ): AsyncIterable { + // Returns an object with a default async iterator const v1AST = gql.parse(v1Query); + const validationErrors = gql.validate(this.v1Schema, v1AST); const v1TypeInfo = new gql.TypeInfo(this.v1Schema); const nodePaths = this.nodePaths(v1AST, v1TypeInfo); - const v2Query = postProcessV2Query(gql.print(asV2AST(v1AST, v1TypeInfo))); + let v2Query: string; + if (validationErrors.length > 0) { + const v2ValidationErrors = gql.validate(this.v2Schema, v1AST); + if (v2ValidationErrors.length > 0) { + throw new VError( + 'invalid query: %s\nValidation errors: %s', + v1Query, + validationErrors.map((err) => err.message).join(', ') + + v2ValidationErrors.map((err) => err.message).join(', ') + ); + } + // Errors means the query is likely already a V2 query + v2Query = v1Query; + + } else { + v2Query = postProcessV2Query(gql.print(asV2AST(v1AST, v1TypeInfo))); + } const v2Nodes = this.faros.nodeIterable( graph, v2Query,