Skip to content

ValuesOfCorrectType is not correctly implementing the spec #2222

@attilah

Description

@attilah

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));

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions