public function testUsingVariables()
{
// executes with complex input:
$doc = '
query q($input:TestInputObject) {
fieldWithObjectInput(input: $input)
}
';
$ast = Parser::parse($doc);
$params = ['input' => ['a' => 'foo', 'b' => ['bar'], 'c' => 'baz']];
$schema = $this->schema();
$this->assertEquals(['data' => ['fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}']], Executor::execute($schema, $ast, null, null, $params)->toArray());
// uses default value when not provided:
$withDefaultsNode = Parser::parse('
query q($input: TestInputObject = {a: "foo", b: ["bar"], c: "baz"}) {
fieldWithObjectInput(input: $input)
}
');
$result = Executor::execute($this->schema(), $withDefaultsNode)->toArray();
$expected = ['data' => ['fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}']];
$this->assertEquals($expected, $result);
// properly parses single value to array:
$params = ['input' => ['a' => 'foo', 'b' => 'bar', 'c' => 'baz']];
$this->assertEquals(['data' => ['fieldWithObjectInput' => '{"a":"foo","b":["bar"],"c":"baz"}']], Executor::execute($schema, $ast, null, null, $params)->toArray());
// executes with complex scalar input:
$params = ['input' => ['c' => 'foo', 'd' => 'SerializedValue']];
$result = Executor::execute($schema, $ast, null, null, $params)->toArray();
$expected = ['data' => ['fieldWithObjectInput' => '{"c":"foo","d":"DeserializedValue"}']];
$this->assertEquals($expected, $result);
// errors on null for nested non-null:
$params = ['input' => ['a' => 'foo', 'b' => 'bar', 'c' => null]];
$expected = FormattedError::create('Variable "$input" got invalid value {"a":"foo","b":"bar","c":null}.' . "\n" . 'In field "c": Expected "String!", found null.', [new SourceLocation(2, 17)]);
try {
Executor::execute($schema, $ast, null, null, $params);
$this->fail('Expected exception not thrown');
} catch (Error $err) {
$this->assertEquals($expected, Error::formatError($err));
}
// errors on incorrect type:
$params = ['input' => 'foo bar'];
try {
Executor::execute($schema, $ast, null, null, $params);
$this->fail('Expected exception not thrown');
} catch (Error $error) {
$expected = FormattedError::create('Variable "$input" got invalid value "foo bar".' . "\n" . 'Expected "TestInputObject", found not an object.', [new SourceLocation(2, 17)]);
$this->assertEquals($expected, Error::formatError($error));
}
// errors on omission of nested non-null:
$params = ['input' => ['a' => 'foo', 'b' => 'bar']];
try {
Executor::execute($schema, $ast, null, null, $params);
$this->fail('Expected exception not thrown');
} catch (Error $e) {
$expected = FormattedError::create('Variable "$input" got invalid value {"a":"foo","b":"bar"}.' . "\n" . 'In field "c": Expected "String!", found null.', [new SourceLocation(2, 17)]);
$this->assertEquals($expected, Error::formatError($e));
}
// errors on deep nested errors and with many errors
$nestedDoc = '
query q($input: TestNestedInputObject) {
fieldWithNestedObjectInput(input: $input)
}
';
$nestedAst = Parser::parse($nestedDoc);
$params = ['input' => ['na' => ['a' => 'foo']]];
try {
Executor::execute($schema, $nestedAst, null, null, $params);
$this->fail('Expected exception not thrown');
} catch (Error $error) {
$expected = FormattedError::create('Variable "$input" got invalid value {"na":{"a":"foo"}}.' . "\n" . 'In field "na": In field "c": Expected "String!", found null.' . "\n" . 'In field "nb": Expected "String!", found null.', [new SourceLocation(2, 19)]);
$this->assertEquals($expected, Error::formatError($error));
}
// errors on addition of unknown input field
$params = ['input' => ['a' => 'foo', 'b' => 'bar', 'c' => 'baz', 'd' => 'dog']];
try {
Executor::execute($schema, $ast, null, null, $params);
$this->fail('Expected exception not thrown');
} catch (Error $e) {
$expected = FormattedError::create('Variable "$input" got invalid value {"a":"foo","b":"bar","c":"baz","d":"dog"}.' . "\n" . 'In field "d": Expected type "ComplexScalar", found "dog".', [new SourceLocation(2, 17)]);
$this->assertEquals($expected, Error::formatError($e));
}
}