Youshido\Tests\Schema\ProcessorTest::testTypedFragment PHP Метод

testTypedFragment() публичный Метод

public testTypedFragment ( )
    public function testTypedFragment()
    {
        $object1 = new ObjectType(['name' => 'Object1', 'fields' => ['id' => ['type' => 'int', 'cost' => 13]]]);
        $object2 = new ObjectType(['name' => 'Object2', 'fields' => ['name' => ['type' => 'string']]]);
        $object3 = new ObjectType(['name' => 'Object3', 'fields' => ['name' => ['type' => 'string']]]);
        $union = new UnionType(['name' => 'TestUnion', 'types' => [$object1, $object2], 'resolveType' => function ($object) use($object1, $object2) {
            if (isset($object['id'])) {
                return $object1;
            }
            return $object2;
        }]);
        $invalidUnion = new UnionType(['name' => 'TestUnion', 'types' => [$object1, $object2], 'resolveType' => function ($object) use($object3) {
            return $object3;
        }]);
        $processor = new Processor(new Schema(['query' => new ObjectType(['name' => 'RootQuery', 'fields' => ['union' => ['type' => $union, 'args' => ['type' => ['type' => 'string']], 'cost' => 10, 'resolve' => function ($value, $args) {
            if ($args['type'] == 'object1') {
                return ['id' => 43];
            } else {
                return ['name' => 'name resolved'];
            }
        }], 'invalidUnion' => ['type' => $invalidUnion, 'resolve' => function () {
            return ['name' => 'name resolved'];
        }]]])]));
        $processor->processPayload('{ union(type: "object1") { ... on Object2 { id } } }');
        $this->assertEquals(['data' => ['union' => []]], $processor->getResponseData());
        $processor->processPayload('{ union(type: "object1") { ... on Object1 { name } } }');
        $this->assertEquals(['data' => ['union' => null], 'errors' => [['message' => 'Field "name" not found in type "Object1"', 'locations' => [['line' => 1, 'column' => 45]]]]], $processor->getResponseData());
        $processor->getExecutionContext()->clearErrors();
        $processor->processPayload('{ union(type: "object1") { ... on Object1 { id } } }');
        $this->assertEquals(['data' => ['union' => ['id' => 43]]], $processor->getResponseData());
        $processor->processPayload('{ union(type: "asd") { ... on Object2 { name } } }');
        $this->assertEquals(['data' => ['union' => ['name' => 'name resolved']]], $processor->getResponseData());
        $processor->processPayload('{ invalidUnion { ... on Object2 { name } } }');
        $this->assertEquals(['data' => ['invalidUnion' => null], 'errors' => [['message' => 'Type "Object3" not exist in types of "TestUnion"']]], $processor->getResponseData());
        $visitor = new MaxComplexityQueryVisitor(1000);
        // arbitrarily high cost
        $processor->processPayload('{ union(type: "object1") { ... on Object1 { id } } }', [], [$visitor]);
        $this->assertEquals(10 + 13, $visitor->getMemo());
        $visitor = new MaxComplexityQueryVisitor(1000);
        // arbitrarily high cost
        $processor->processPayload('{ union(type: "object1") { ... on Object1 { id }, ... on Object2 { name } } }', [], [$visitor]);
        $this->assertEquals(10 + 13 + 1, $visitor->getMemo());
        // planning phase currently has no knowledge of what types the union will resolve to, this will have the same score as above
        $visitor = new MaxComplexityQueryVisitor(1000);
        // arbitrarily high cost
        $processor->processPayload('{ union(type: "object2") { ... on Object1 { id }, ... on Object2 { name } } }', [], [$visitor]);
        $this->assertEquals(10 + 13 + 1, $visitor->getMemo());
    }