-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Closed
Labels
Description
As the spec states, this rule should validate that a given document is conforming to the schema, by verifying the type of fields for any given type.
When the schema defines a List type with non-null items and an Object is passed in the document, validation is not failing.
Schema:
type Post {
id: ID!
title: String
content: String
}
input UpdatePostInput {
id: ID!
title: String
content: String
}
input PostConditionInput {
title: StringConditionInput
content: StringConditionInput
and: [PostConditionInput!]
}
input StringConditionInput {
contains: String
}
type Mutation {
updatePost(input: UpdatePostInput!, conditions: PostConditionInput): Post
}
type Query {
listPosts : [Post!]!
}
Document:
mutation InvalidUpdate {
updatePost(
input: {
id: "P1"
title: "New Title"
content: "New Content"
}
conditions: {
and: {
title: { contains: "ABC" }
content: { contains: "DEF" }
}
}
) {
id
title
content
}
}
mutation ValidUpdate {
updatePost(
input: {
id: "P1"
title: "New Title"
content: "New Content"
}
conditions: {
and: [
{ title: { contains: "ABC" } }
{ content: { contains: "DEF" } }
]
}
) {
id
title
content
}
}
Notice that and
is an object instead of an array in InvalidUpdate
mutation, which would be valid, if a single object would be coerced into a single element array after parse and the object would be validated against the type of the list item.
The document above is passing validation, with the following code, using the latest 14.x version of the library.
import * as fs from 'fs';
import { buildSchema, parse } from 'graphql';
import { GraphQLSchema } from 'graphql/type';
import { validate, specifiedRules } from 'graphql/validation';
const schema = fs.readFileSync('schema.graphql').toString();
const mutation = fs.readFileSync('mutation.graphql').toString();
const rules = specifiedRules;
const schemaObject: GraphQLSchema = buildSchema(schema);
const document = parse(mutation);
const validataionResult = validate(schemaObject, document, rules);
console.log(JSON.stringify(validataionResult, null, 2));
galvesribeiro and mike-marcacci