Skip to content

Commit

Permalink
support validating an array of documents
Browse files Browse the repository at this point in the history
  • Loading branch information
StarpTech committed May 22, 2021
1 parent 9c4559e commit e214df6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 }"]
}
```

Expand Down
32 changes: 23 additions & 9 deletions src/registry/document-validation/document-validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
},
})
Expand Down Expand Up @@ -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}`,
},
})
Expand All @@ -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: [],
Expand All @@ -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}`,
},
})
Expand Down
12 changes: 6 additions & 6 deletions src/registry/document-validation/document-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { document, graphName } from '../../core/shared-schemas'

export interface RequestContext {
Body: {
document: string
documents: string[]
graphName: string
}
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e214df6

Please sign in to comment.