diff --git a/docs/api.md b/docs/api.md index 6a4634d..8ca6416 100644 --- a/docs/api.md +++ b/docs/api.md @@ -146,7 +146,7 @@ POST - `/document/validate` Confirm that all client operations are supported by ```json { "graphName": "my_graph", - "document": "query { hello }" + "documents": ["query { hello }"] } ``` diff --git a/src/registry/document-validation/document-validation.test.ts b/src/registry/document-validation/document-validation.test.ts index de5f394..faf4ebc 100644 --- a/src/registry/document-validation/document-validation.test.ts +++ b/src/registry/document-validation/document-validation.test.ts @@ -51,7 +51,13 @@ test('Should validate document as valid', async (t) => { method: 'POST', url: '/document/validate', payload: { - document: `query { hello }`, + documents: [ + /* GraphQL */ ` + query { + hello + } + `, + ], graphName: `${t.context.graphName}`, }, }) @@ -94,7 +100,13 @@ test('Should validate document as invalid because field does not exist', async ( method: 'POST', url: '/document/validate', payload: { - document: `query { world }`, + documents: [ + /* GraphQL */ ` + query { + world + } + `, + ], graphName: `${t.context.graphName}`, }, }) @@ -108,14 +120,14 @@ test('Should validate document as invalid because field does not exist', async ( error: [ { source: { - body: '{\n world\n}\n', + body: '\n query {\n world\n }\n ', name: 'GraphQL request', locationOffset: { line: 1, column: 1 }, }, errors: [ { message: 'Cannot query field "world" on type "Query".', - locations: [{ line: 2, column: 3 }], + locations: [{ line: 3, column: 13 }], }, ], deprecated: [], @@ -136,11 +148,13 @@ test('Should return 400 error when graph does not exist', async (t) => { method: 'POST', url: '/document/validate', payload: { - document: /* GraphQL */ ` - query { - world - } - `, + documents: [ + /* GraphQL */ ` + query { + world + } + `, + ], graphName: `${t.context.graphName}`, }, }) diff --git a/src/registry/document-validation/document-validation.ts b/src/registry/document-validation/document-validation.ts index 0b5f62f..9c5c4e5 100644 --- a/src/registry/document-validation/document-validation.ts +++ b/src/registry/document-validation/document-validation.ts @@ -17,7 +17,7 @@ import { document, graphName } from '../../core/shared-schemas' export interface RequestContext { Body: { - document: string + documents: string[] graphName: string } } @@ -31,9 +31,9 @@ export const schema: FastifySchema = { }, body: S.object() .additionalProperties(false) - .required(['document', 'graphName']) + .required(['documents', 'graphName']) .prop('graphName', graphName) - .prop('document', document), + .prop('documents', S.array().items(document).minItems(1).maxItems(100)), } export default function documentValidation(fastify: FastifyInstance) { @@ -87,14 +87,14 @@ export default function documentValidation(fastify: FastifyInstance) { throw SchemaCompositionError(updated.error) } - let doc = null + let sources: Source[] = [] try { - doc = parse(req.body.document) + sources = req.body.documents.map((document) => new Source(document)) } catch { throw InvalidDocumentError() } - const invalidDocuments = validateDocument(updated.schema, [new Source(print(doc))], { + const invalidDocuments = validateDocument(updated.schema, sources, { apollo: true, strictDeprecated: true, maxDepth: 10,