graphql-php/tests/Executor/VariablesTest.php

921 lines
30 KiB
PHP
Raw Normal View History

2015-07-15 20:05:46 +03:00
<?php
2018-09-01 18:07:06 +03:00
declare(strict_types=1);
namespace GraphQL\Tests\Executor;
use GraphQL\Error\Error;
2016-04-09 10:36:53 +03:00
use GraphQL\Executor\Executor;
2015-07-15 20:05:46 +03:00
use GraphQL\Language\Parser;
2018-09-01 18:07:06 +03:00
use GraphQL\Tests\Executor\TestClasses\ComplexScalar;
2015-07-15 20:05:46 +03:00
use GraphQL\Type\Definition\InputObjectType;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
2018-09-01 18:07:06 +03:00
use GraphQL\Type\Schema;
2018-07-29 18:43:10 +03:00
use PHPUnit\Framework\TestCase;
2018-09-01 18:07:06 +03:00
use function json_encode;
2015-07-15 20:05:46 +03:00
/**
* Execute: Handles inputs
* Handles objects and nullability
*/
2018-07-29 18:43:10 +03:00
class VariablesTest extends TestCase
2015-07-15 20:05:46 +03:00
{
public function testUsingInlineStructs() : void
2015-07-15 20:05:46 +03:00
{
// executes with complex input:
2018-08-22 14:40:15 +03:00
$result = $this->executeQuery('
2015-07-15 20:05:46 +03:00
{
fieldWithObjectInput(input: {a: "foo", b: ["bar"], c: "baz"})
}
2018-08-22 14:40:15 +03:00
');
2015-07-15 20:05:46 +03:00
$expected = [
2018-09-01 18:07:06 +03:00
'data' => ['fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}'],
2015-07-15 20:05:46 +03:00
];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
// properly parses single value to list:
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery('
2015-07-15 20:05:46 +03:00
{
fieldWithObjectInput(input: {a: "foo", b: "bar", c: "baz"})
}
2018-08-22 14:40:15 +03:00
');
2015-07-15 20:05:46 +03:00
$expected = ['data' => ['fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2016-11-18 19:59:28 +03:00
// properly parses null value to null
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery('
2016-11-18 19:59:28 +03:00
{
fieldWithObjectInput(input: {a: null, b: null, c: "C", d: null})
}
2018-08-22 14:40:15 +03:00
');
2016-11-18 19:59:28 +03:00
$expected = ['data' => ['fieldWithObjectInput' => '{"a":null,"b":null,"c":"C","d":null}']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2016-11-18 19:59:28 +03:00
// properly parses null value in list
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery('
2016-11-18 19:59:28 +03:00
{
fieldWithObjectInput(input: {b: ["A",null,"C"], c: "C"})
}
2018-08-22 14:40:15 +03:00
');
2016-11-18 19:59:28 +03:00
$expected = ['data' => ['fieldWithObjectInput' => '{"b":["A",null,"C"],"c":"C"}']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
// does not use incorrect value
2018-08-22 14:40:15 +03:00
$result = $this->executeQuery('
{
fieldWithObjectInput(input: ["foo", "bar", "baz"])
}
2018-08-22 14:40:15 +03:00
');
$expected = [
2018-09-01 18:07:06 +03:00
'data' => ['fieldWithObjectInput' => null],
2016-11-19 00:15:40 +03:00
'errors' => [[
2018-09-01 18:07:06 +03:00
'message' => 'Argument "input" has invalid value ["foo", "bar", "baz"].',
'path' => ['fieldWithObjectInput'],
'locations' => [['line' => 3, 'column' => 39]],
],
],
];
2018-08-22 14:40:15 +03:00
$this->assertArraySubset($expected, $result->toArray());
// properly runs parseLiteral on complex scalar types
2018-08-22 14:40:15 +03:00
$result = $this->executeQuery('
{
2016-11-19 00:15:40 +03:00
fieldWithObjectInput(input: {c: "foo", d: "SerializedValue"})
}
2018-08-22 14:40:15 +03:00
');
$this->assertEquals(
2016-11-19 00:15:40 +03:00
['data' => ['fieldWithObjectInput' => '{"c":"foo","d":"DeserializedValue"}']],
2018-08-22 14:40:15 +03:00
$result->toArray()
);
2015-07-15 20:05:46 +03:00
}
2018-09-01 18:07:06 +03:00
private function executeQuery($query, $variableValues = null)
{
$document = Parser::parse($query);
return Executor::execute($this->schema(), $document, null, null, $variableValues);
}
/**
* Describe: Handles nullable scalars
*/
public function schema() : Schema
{
$ComplexScalarType = ComplexScalar::create();
$TestInputObject = new InputObjectType([
'name' => 'TestInputObject',
'fields' => [
'a' => ['type' => Type::string()],
'b' => ['type' => Type::listOf(Type::string())],
'c' => ['type' => Type::nonNull(Type::string())],
'd' => ['type' => $ComplexScalarType],
],
]);
$TestNestedInputObject = new InputObjectType([
'name' => 'TestNestedInputObject',
'fields' => [
'na' => ['type' => Type::nonNull($TestInputObject)],
'nb' => ['type' => Type::nonNull(Type::string())],
],
]);
$TestType = new ObjectType([
'name' => 'TestType',
'fields' => [
'fieldWithObjectInput' => $this->fieldWithInputArg(['type' => $TestInputObject]),
'fieldWithNullableStringInput' => $this->fieldWithInputArg(['type' => Type::string()]),
'fieldWithNonNullableStringInput' => $this->fieldWithInputArg(['type' => Type::nonNull(Type::string())]),
'fieldWithDefaultArgumentValue' => $this->fieldWithInputArg([
'type' => Type::string(),
'defaultValue' => 'Hello World',
]),
'fieldWithNestedInputObject' => $this->fieldWithInputArg([
'type' => $TestNestedInputObject,
'defaultValue' => 'Hello World',
]),
'list' => $this->fieldWithInputArg(['type' => Type::listOf(Type::string())]),
'nnList' => $this->fieldWithInputArg(['type' => Type::nonNull(Type::listOf(Type::string()))]),
'listNN' => $this->fieldWithInputArg(['type' => Type::listOf(Type::nonNull(Type::string()))]),
'nnListNN' => $this->fieldWithInputArg(['type' => Type::nonNull(Type::listOf(Type::nonNull(Type::string())))]),
],
]);
return new Schema(['query' => $TestType]);
}
private function fieldWithInputArg($inputArg)
{
return [
'type' => Type::string(),
'args' => ['input' => $inputArg],
'resolve' => function ($_, $args) {
if (isset($args['input'])) {
return json_encode($args['input']);
}
return null;
},
];
}
public function testUsingVariables() : void
2015-07-15 20:05:46 +03:00
{
$doc = '
2018-08-22 14:40:15 +03:00
query q($input:TestInputObject) {
fieldWithObjectInput(input: $input)
}
2015-07-15 20:05:46 +03:00
';
2018-08-22 14:40:15 +03:00
// executes with complex input:
2015-07-15 20:05:46 +03:00
$params = ['input' => ['a' => 'foo', 'b' => ['bar'], 'c' => 'baz']];
2018-08-22 14:40:15 +03:00
$result = $this->executeQuery($doc, $params);
2015-07-15 20:05:46 +03:00
$this->assertEquals(
['data' => ['fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}']],
2018-08-22 14:40:15 +03:00
$result->toArray()
2015-07-15 20:05:46 +03:00
);
// uses default value when not provided:
2018-08-22 14:40:15 +03:00
$result = $this->executeQuery('
query ($input: TestInputObject = {a: "foo", b: ["bar"], c: "baz"}) {
fieldWithObjectInput(input: $input)
}
');
$expected = [
2018-09-01 18:07:06 +03:00
'data' => ['fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}'],
];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
// properly parses single value to list:
2015-07-15 20:05:46 +03:00
$params = ['input' => ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']];
2018-08-22 14:40:15 +03:00
$result = $this->executeQuery($doc, $params);
2015-07-15 20:05:46 +03:00
$this->assertEquals(
['data' => ['fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}']],
2018-08-22 14:40:15 +03:00
$result->toArray()
2015-07-15 20:05:46 +03:00
);
// executes with complex scalar input:
2018-09-01 18:07:06 +03:00
$params = ['input' => ['c' => 'foo', 'd' => 'SerializedValue']];
$result = $this->executeQuery($doc, $params);
2015-07-15 20:05:46 +03:00
$expected = [
2018-09-01 18:07:06 +03:00
'data' => ['fieldWithObjectInput' => '{"c":"foo","d":"DeserializedValue"}'],
2015-07-15 20:05:46 +03:00
];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
// errors on null for nested non-null:
2018-09-01 18:07:06 +03:00
$params = ['input' => ['a' => 'foo', 'b' => 'bar', 'c' => null]];
$result = $this->executeQuery($doc, $params);
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$input" got invalid value ' .
'{"a":"foo","b":"bar","c":null}; ' .
'Expected non-nullable type String! not to be null at value.c.',
2018-08-22 14:40:15 +03:00
'locations' => [['line' => 2, 'column' => 21]],
2018-09-01 18:07:06 +03:00
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
// errors on incorrect type:
2018-09-01 18:07:06 +03:00
$params = ['input' => 'foo bar'];
$result = $this->executeQuery($doc, $params);
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$input" got invalid value "foo bar"; ' .
'Expected type TestInputObject to be an object.',
2018-09-01 18:07:06 +03:00
'locations' => [['line' => 2, 'column' => 21]],
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
// errors on omission of nested non-null:
$params = ['input' => ['a' => 'foo', 'b' => 'bar']];
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, $params);
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$input" got invalid value {"a":"foo","b":"bar"}; ' .
'Field value.c of required type String! was not provided.',
2018-08-22 14:40:15 +03:00
'locations' => [['line' => 2, 'column' => 21]],
2018-09-01 18:07:06 +03:00
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
// errors on deep nested errors and with many errors
$nestedDoc = '
query q($input: TestNestedInputObject) {
fieldWithNestedObjectInput(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$params = ['input' => ['na' => ['a' => 'foo']]];
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($nestedDoc, $params);
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$input" got invalid value {"na":{"a":"foo"}}; ' .
'Field value.na.c of required type String! was not provided.',
'locations' => [['line' => 2, 'column' => 19]],
2018-09-01 18:07:06 +03:00
'category' => 'graphql',
],
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$input" got invalid value {"na":{"a":"foo"}}; ' .
'Field value.nb of required type String! was not provided.',
'locations' => [['line' => 2, 'column' => 19]],
2018-09-01 18:07:06 +03:00
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
// errors on addition of unknown input field
2018-09-01 18:07:06 +03:00
$params = ['input' => ['a' => 'foo', 'b' => 'bar', 'c' => 'baz', 'extra' => 'dog']];
$result = $this->executeQuery($doc, $params);
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$input" got invalid value ' .
'{"a":"foo","b":"bar","c":"baz","extra":"dog"}; ' .
'Field "extra" is not defined by type TestInputObject.',
2018-08-22 14:40:15 +03:00
'locations' => [['line' => 2, 'column' => 21]],
2018-09-01 18:07:06 +03:00
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows nullable inputs to be omitted')
*/
public function testAllowsNullableInputsToBeOmitted() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery('
2015-07-15 20:05:46 +03:00
{
fieldWithNullableStringInput
}
2018-08-22 14:40:15 +03:00
');
2015-07-15 20:05:46 +03:00
$expected = [
2018-09-01 18:07:06 +03:00
'data' => ['fieldWithNullableStringInput' => null],
2015-07-15 20:05:46 +03:00
];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows nullable inputs to be omitted in a variable')
*/
public function testAllowsNullableInputsToBeOmittedInAVariable() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery('
2015-07-15 20:05:46 +03:00
query SetsNullable($value: String) {
fieldWithNullableStringInput(input: $value)
}
2018-08-22 14:40:15 +03:00
');
$expected = ['data' => ['fieldWithNullableStringInput' => null]];
2015-07-15 20:05:46 +03:00
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows nullable inputs to be omitted in an unlisted variable')
*/
public function testAllowsNullableInputsToBeOmittedInAnUnlistedVariable() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery('
2015-07-15 20:05:46 +03:00
query SetsNullable {
fieldWithNullableStringInput(input: $value)
}
2018-08-22 14:40:15 +03:00
');
$expected = ['data' => ['fieldWithNullableStringInput' => null]];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
2018-09-01 18:07:06 +03:00
// Describe: Handles non-nullable scalars
/**
* @see it('allows nullable inputs to be set to null in a variable')
*/
public function testAllowsNullableInputsToBeSetToNullInAVariable() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery('
2015-07-15 20:05:46 +03:00
query SetsNullable($value: String) {
fieldWithNullableStringInput(input: $value)
}
2018-08-22 14:40:15 +03:00
');
$expected = ['data' => ['fieldWithNullableStringInput' => null]];
2015-07-15 20:05:46 +03:00
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows nullable inputs to be set to a value in a variable')
*/
public function testAllowsNullableInputsToBeSetToAValueInAVariable() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query SetsNullable($value: String) {
fieldWithNullableStringInput(input: $value)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['value' => 'a']);
2015-07-15 20:05:46 +03:00
$expected = ['data' => ['fieldWithNullableStringInput' => '"a"']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows nullable inputs to be set to a value directly')
*/
public function testAllowsNullableInputsToBeSetToAValueDirectly() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery('
2015-07-15 20:05:46 +03:00
{
fieldWithNullableStringInput(input: "a")
}
2018-08-22 14:40:15 +03:00
');
2015-07-15 20:05:46 +03:00
$expected = ['data' => ['fieldWithNullableStringInput' => '"a"']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
2016-11-19 00:15:40 +03:00
/**
* @see it('allows non-nullable inputs to be omitted given a default')
2016-11-19 00:15:40 +03:00
*/
public function testAllowsNonNullableInputsToBeOmittedGivenADefault() : void
2016-11-19 00:15:40 +03:00
{
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery('
2016-11-19 00:15:40 +03:00
query SetsNonNullable($value: String = "default") {
fieldWithNonNullableStringInput(input: $value)
}
2018-08-22 14:40:15 +03:00
');
2016-11-19 00:15:40 +03:00
$expected = [
2018-09-01 18:07:06 +03:00
'data' => ['fieldWithNonNullableStringInput' => '"default"'],
2016-11-19 00:15:40 +03:00
];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2016-11-19 00:15:40 +03:00
}
/**
* @see it('does not allow non-nullable inputs to be omitted in a variable')
*/
public function testDoesntAllowNonNullableInputsToBeOmittedInAVariable() : void
2015-07-15 20:05:46 +03:00
{
2018-08-22 14:40:15 +03:00
$result = $this->executeQuery('
2015-07-15 20:05:46 +03:00
query SetsNonNullable($value: String!) {
fieldWithNonNullableStringInput(input: $value)
}
2018-08-22 14:40:15 +03:00
');
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' => 'Variable "$value" of required type "String!" was not provided.',
'locations' => [['line' => 2, 'column' => 31]],
2018-09-01 18:07:06 +03:00
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('does not allow non-nullable inputs to be set to null in a variable')
*/
public function testDoesNotAllowNonNullableInputsToBeSetToNullInAVariable() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query SetsNonNullable($value: String!) {
fieldWithNonNullableStringInput(input: $value)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['value' => null]);
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$value" got invalid value null; ' .
'Expected non-nullable type String! not to be null.',
'locations' => [['line' => 2, 'column' => 31]],
2018-09-01 18:07:06 +03:00
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows non-nullable inputs to be set to a value in a variable')
*/
public function testAllowsNonNullableInputsToBeSetToAValueInAVariable() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query SetsNonNullable($value: String!) {
fieldWithNonNullableStringInput(input: $value)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['value' => 'a']);
2015-07-15 20:05:46 +03:00
$expected = ['data' => ['fieldWithNonNullableStringInput' => '"a"']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows non-nullable inputs to be set to a value directly')
*/
public function testAllowsNonNullableInputsToBeSetToAValueDirectly() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery('
2015-07-15 20:05:46 +03:00
{
fieldWithNonNullableStringInput(input: "a")
}
2018-08-22 14:40:15 +03:00
');
2015-07-15 20:05:46 +03:00
$expected = ['data' => ['fieldWithNonNullableStringInput' => '"a"']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('reports error for missing non-nullable inputs')
*/
public function testReportsErrorForMissingNonNullableInputs() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery('
2015-07-15 20:05:46 +03:00
{
fieldWithNonNullableStringInput
}
2018-08-22 14:40:15 +03:00
');
2016-11-19 00:15:40 +03:00
$expected = [
2018-09-01 18:07:06 +03:00
'data' => ['fieldWithNonNullableStringInput' => null],
2016-11-19 00:15:40 +03:00
'errors' => [[
2018-09-01 18:07:06 +03:00
'message' => 'Argument "input" of required type "String!" was not provided.',
'locations' => [['line' => 3, 'column' => 9]],
'path' => ['fieldWithNonNullableStringInput'],
'category' => 'graphql',
],
],
2016-11-19 00:15:40 +03:00
];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2016-11-19 00:15:40 +03:00
}
2018-09-01 18:07:06 +03:00
// Describe: Handles lists and nullability
/**
* @see it('reports error for array passed into string input')
*/
public function testReportsErrorForArrayPassedIntoStringInput() : void
{
2018-09-01 18:07:06 +03:00
$doc = '
query SetsNonNullable($value: String!) {
fieldWithNonNullableStringInput(input: $value)
}
';
$variables = ['value' => [1, 2, 3]];
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, $variables);
$expected = [
'errors' => [[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$value" got invalid value [1,2,3]; Expected type ' .
'String; String cannot represent an array value: [1,2,3]',
2018-09-01 18:07:06 +03:00
'category' => 'graphql',
'locations' => [
2018-09-01 18:07:06 +03:00
['line' => 2, 'column' => 31],
],
],
],
];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
}
/**
* @see it('serializing an array via GraphQLString throws TypeError')
*/
public function testSerializingAnArrayViaGraphQLStringThrowsTypeError() : void
{
2018-07-29 18:43:10 +03:00
$this->expectException(Error::class);
$this->expectExceptionMessage('String cannot represent non scalar value: [1,2,3]');
Type::string()->serialize([1, 2, 3]);
}
2016-11-19 00:15:40 +03:00
/**
* @see it('reports error for non-provided variables for non-nullable inputs')
2016-11-19 00:15:40 +03:00
*/
public function testReportsErrorForNonProvidedVariablesForNonNullableInputs() : void
2016-11-19 00:15:40 +03:00
{
// Note: this test would typically fail validation before encountering
// this execution error, however for queries which previously validated
// and are being run against a new schema which have introduced a breaking
// change to make a formerly non-required argument required, this asserts
// failure before allowing the underlying code to receive a non-null value.
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery('
2016-11-19 00:15:40 +03:00
{
fieldWithNonNullableStringInput(input: $foo)
}
2018-08-22 14:40:15 +03:00
');
2016-11-19 00:15:40 +03:00
$expected = [
2018-09-01 18:07:06 +03:00
'data' => ['fieldWithNonNullableStringInput' => null],
2016-11-19 00:15:40 +03:00
'errors' => [[
2018-09-01 18:07:06 +03:00
'message' =>
2016-11-19 00:15:40 +03:00
'Argument "input" of required type "String!" was provided the ' .
'variable "$foo" which was not provided a runtime value.',
'locations' => [['line' => 3, 'column' => 48]],
2018-09-01 18:07:06 +03:00
'path' => ['fieldWithNonNullableStringInput'],
'category' => 'graphql',
],
],
2016-11-19 00:15:40 +03:00
];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows lists to be null')
*/
public function testAllowsListsToBeNull() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query q($input:[String]) {
list(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['input' => null]);
$expected = ['data' => ['list' => null]];
2015-07-15 20:05:46 +03:00
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows lists to contain values')
*/
public function testAllowsListsToContainValues() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query q($input:[String]) {
list(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['input' => ['A']]);
2015-07-15 20:05:46 +03:00
$expected = ['data' => ['list' => '["A"]']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows lists to contain null')
*/
public function testAllowsListsToContainNull() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query q($input:[String]) {
list(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['input' => ['A', null, 'B']]);
2015-07-15 20:05:46 +03:00
$expected = ['data' => ['list' => '["A",null,"B"]']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('does not allow non-null lists to be null')
*/
public function testDoesNotAllowNonNullListsToBeNull() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query q($input:[String]!) {
nnList(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['input' => null]);
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$input" got invalid value null; ' .
'Expected non-nullable type [String]! not to be null.',
'locations' => [['line' => 2, 'column' => 17]],
2018-09-01 18:07:06 +03:00
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows non-null lists to contain values')
*/
public function testAllowsNonNullListsToContainValues() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query q($input:[String]!) {
nnList(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['input' => ['A']]);
2015-07-15 20:05:46 +03:00
$expected = ['data' => ['nnList' => '["A"]']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows non-null lists to contain null')
*/
public function testAllowsNonNullListsToContainNull() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query q($input:[String]!) {
nnList(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['input' => ['A', null, 'B']]);
2015-07-15 20:05:46 +03:00
$expected = ['data' => ['nnList' => '["A",null,"B"]']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows lists of non-nulls to be null')
*/
public function testAllowsListsOfNonNullsToBeNull() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query q($input:[String!]) {
listNN(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['input' => null]);
$expected = ['data' => ['listNN' => null]];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows lists of non-nulls to contain values')
*/
public function testAllowsListsOfNonNullsToContainValues() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query q($input:[String!]) {
listNN(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['input' => ['A']]);
2015-07-15 20:05:46 +03:00
$expected = ['data' => ['listNN' => '["A"]']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('does not allow lists of non-nulls to contain null')
*/
public function testDoesNotAllowListsOfNonNullsToContainNull() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query q($input:[String!]) {
listNN(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['input' => ['A', null, 'B']]);
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$input" got invalid value ["A",null,"B"]; ' .
'Expected non-nullable type String! not to be null at value[1].',
2018-09-01 18:07:06 +03:00
'locations' => [['line' => 2, 'column' => 17]],
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('does not allow non-null lists of non-nulls to be null')
*/
public function testDoesNotAllowNonNullListsOfNonNullsToBeNull() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query q($input:[String!]!) {
nnListNN(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['input' => null]);
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$input" got invalid value null; ' .
'Expected non-nullable type [String!]! not to be null.',
2018-09-01 18:07:06 +03:00
'locations' => [['line' => 2, 'column' => 17]],
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('allows non-null lists of non-nulls to contain values')
*/
public function testAllowsNonNullListsOfNonNullsToContainValues() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query q($input:[String!]!) {
nnListNN(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['input' => ['A']]);
2015-07-15 20:05:46 +03:00
$expected = ['data' => ['nnListNN' => '["A"]']];
2018-08-22 14:40:15 +03:00
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
2018-09-01 18:07:06 +03:00
// Describe: Execute: Uses argument default values
/**
* @see it('does not allow non-null lists of non-nulls to contain null')
*/
public function testDoesNotAllowNonNullListsOfNonNullsToContainNull() : void
2015-07-15 20:05:46 +03:00
{
2018-09-01 18:07:06 +03:00
$doc = '
2015-07-15 20:05:46 +03:00
query q($input:[String!]!) {
nnListNN(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, ['input' => ['A', null, 'B']]);
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$input" got invalid value ["A",null,"B"]; ' .
'Expected non-nullable type String! not to be null at value[1].',
2018-09-01 18:07:06 +03:00
'locations' => [['line' => 2, 'column' => 17]],
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
2015-07-15 20:05:46 +03:00
}
/**
* @see it('does not allow invalid types to be used as values')
*/
public function testDoesNotAllowInvalidTypesToBeUsedAsValues() : void
{
2018-09-01 18:07:06 +03:00
$doc = '
query q($input: TestType!) {
fieldWithObjectInput(input: $input)
}
';
2018-09-01 18:07:06 +03:00
$vars = ['input' => ['list' => ['A', 'B']]];
$result = $this->executeQuery($doc, $vars);
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$input" expected value of type "TestType!" which cannot ' .
'be used as an input type.',
'locations' => [['line' => 2, 'column' => 25]],
2018-09-01 18:07:06 +03:00
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
}
/**
* @see it('does not allow unknown types to be used as values')
*/
public function testDoesNotAllowUnknownTypesToBeUsedAsValues() : void
{
2018-09-01 18:07:06 +03:00
$doc = '
query q($input: UnknownType!) {
fieldWithObjectInput(input: $input)
}
';
$vars = ['input' => 'whoknows'];
2018-09-01 18:07:06 +03:00
$result = $this->executeQuery($doc, $vars);
$expected = [
'errors' => [
[
2018-09-01 18:07:06 +03:00
'message' =>
'Variable "$input" expected value of type "UnknownType!" which ' .
'cannot be used as an input type.',
'locations' => [['line' => 2, 'column' => 25]],
2018-09-01 18:07:06 +03:00
'category' => 'graphql',
],
],
];
$this->assertEquals($expected, $result->toArray());
}
/**
* @see it('when no argument provided')
*/
public function testWhenNoArgumentProvided() : void
{
2018-08-22 14:40:15 +03:00
$result = $this->executeQuery('{
fieldWithDefaultArgumentValue
}');
$this->assertEquals(
['data' => ['fieldWithDefaultArgumentValue' => '"Hello World"']],
2018-08-22 14:40:15 +03:00
$result->toArray()
);
}
/**
* @see it('when omitted variable provided')
*/
public function testWhenOmittedVariableProvided() : void
{
2018-08-22 14:40:15 +03:00
$result = $this->executeQuery('query optionalVariable($optional: String) {
fieldWithDefaultArgumentValue(input: $optional)
}');
$this->assertEquals(
['data' => ['fieldWithDefaultArgumentValue' => '"Hello World"']],
2018-08-22 14:40:15 +03:00
$result->toArray()
);
}
/**
* @see it('not when argument cannot be coerced')
*/
public function testNotWhenArgumentCannotBeCoerced() : void
{
2018-08-22 14:40:15 +03:00
$result = $this->executeQuery('{
fieldWithDefaultArgumentValue(input: WRONG_TYPE)
}');
2016-11-19 00:15:40 +03:00
$expected = [
2018-09-01 18:07:06 +03:00
'data' => ['fieldWithDefaultArgumentValue' => null],
2016-11-19 00:15:40 +03:00
'errors' => [[
2018-09-01 18:07:06 +03:00
'message' =>
'Argument "input" has invalid value WRONG_TYPE.',
2018-09-01 18:07:06 +03:00
'locations' => [['line' => 2, 'column' => 50]],
'path' => ['fieldWithDefaultArgumentValue'],
'category' => 'graphql',
],
],
2018-08-22 14:40:15 +03:00
];
2018-09-01 18:07:06 +03:00
$this->assertEquals($expected, $result->toArray());
2018-08-22 14:40:15 +03:00
}
2015-07-15 20:05:46 +03:00
}