stevan / p5-Graph-QL

GraphQL implementation in Perl
2 stars 2 forks source link

Parameter enforcement on optional or default values #34

Open xsist10 opened 5 years ago

xsist10 commented 5 years ago

I think the validation rules are not taking default and nullable parameter configurations into account when validating the query.

I have a schema definition that includes a query that takes two parameters both marked as nullable (no ! post-fix) and both with the default value set to null.

The schema looks like this:

type Records {
    id : Int!
   external_id : Int
}

type Query {
    get_records(id : Int = null, external_id : Int = null) : [Records]
}

schema {
    query : Query
}

When I run the following query:

query TestQuery {
    get_records {
        id
    }
}

I get this error Message:

The `schema.field(get_records)` and `query.field(get_records)` both must expect arguments, not `yes` and `no`"

And when I run the following query:

query TestQuery {
    get_records(id: 1) {
        id
    }
}

I get this error Message:

The `schema.field(get_records).arity` and `query.field(get_records).arity` must match, not `2` and `1`
xsist10 commented 5 years ago

Added test

use strict;
use warnings;

use Graph::QL::Schema;
use Graph::QL::Operation;
use Graph::QL::Execution::QueryValidator;

use Test::More;

my $schema = Graph::QL::Schema->new_from_source(q[
    type Records {
        id : Int!
        external_id : Int
    }

    type Query {
        get_records(id : Int = null, external_id : Int = null) : [Records]
    }

    schema {
        query : Query
    }
]);

my @queries = (
    q[
        query TestQuery {
            get_records {
                id
            }
        }
    ],
    q[
        query TestQuery {
            get_records(id: 1) {
                id
            }
        }
    ],
);

foreach (@queries) {
    my $operation = Graph::QL::Operation->new_from_source($_);

    # Perform validation first
    my $validator = Graph::QL::Execution::QueryValidator->new(
        schema    => $schema,
        operation => $operation,
    );

    # Check if all the validation steps passed
    my $validate = $validator->validate();
    my $messages = join("\n", $validator->get_errors());
    ok($validate, "Query $_ passes the parameter validation checks properly with messages: $messages.");

}

done_testing();

1;