Example #1
0
    public function testExecutesUsingASchema()
    {
        $blogArticle = NULL;
        $blogImage = new ObjectType('Image', ['url' => ['type' => Type::stringType()], 'width' => ['type' => Type::intType()], 'height' => ['type' => Type::intType()]]);
        $blogAuthor = new ObjectType('Author', ['id' => ['type' => Type::stringType()], 'name' => ['type' => Type::stringType()], 'pic' => ['args' => ['width' => ['type' => Type::intType()], 'height' => ['type' => Type::intType()]], 'type' => $blogImage, 'resolve' => function ($obj, $args) {
            return $obj['pic']($args['width'], $args['height']);
        }], 'recentArticle' => ['type' => function () use(&$blogArticle) {
            return $blogArticle;
        }]]);
        $blogArticle = new ObjectType('Article', ['id' => ['type' => new NonNullModifier(Type::stringType())], 'isPublished' => ['type' => Type::booleanType()], 'author' => ['type' => $blogAuthor], 'title' => ['type' => Type::stringType()], 'body' => ['type' => Type::stringType()], 'keywords' => ['type' => new ListModifier(Type::stringType())]]);
        $blogQuery = new ObjectType('Query', ['article' => ['type' => $blogArticle, 'args' => ['id' => ['type' => Type::idType()]], 'resolve' => function ($_, $args) {
            return $this->article($args['id']);
        }], 'feed' => ['type' => new ListModifier($blogArticle), 'resolve' => function () {
            return [$this->article(1), $this->article(2), $this->article(3), $this->article(4), $this->article(5), $this->article(6), $this->article(7), $this->article(8), $this->article(9), $this->article(10)];
        }]]);
        $blogSchema = new Schema($blogQuery);
        $request = '
            {
                feed {
                    id,
                    title
                },
                article(id: "1") {
                    ...articleFields,
                    author {
                        id,
                        name,
                        pic(width: 640, height: 480) {
                            url,
                            width,
                            height
                        },
                        recentArticle {
                            ...articleFields,
                            keywords
                        }
                    }
                }
            }

            fragment articleFields on Article {
                id,
                isPublished,
                title,
                body,
                hidden,
                notdefined
            }
        ';
        $expected = ['data' => ['feed' => [['id' => '1', 'title' => 'My Article 1'], ['id' => '2', 'title' => 'My Article 2'], ['id' => '3', 'title' => 'My Article 3'], ['id' => '4', 'title' => 'My Article 4'], ['id' => '5', 'title' => 'My Article 5'], ['id' => '6', 'title' => 'My Article 6'], ['id' => '7', 'title' => 'My Article 7'], ['id' => '8', 'title' => 'My Article 8'], ['id' => '9', 'title' => 'My Article 9'], ['id' => '10', 'title' => 'My Article 10']], 'article' => ['id' => '1', 'isPublished' => TRUE, 'title' => 'My Article 1', 'body' => 'This is a post', 'author' => ['id' => '123', 'name' => 'John Smith', 'pic' => ['url' => 'cdn://123', 'width' => 640, 'height' => 480], 'recentArticle' => ['id' => '1', 'isPublished' => TRUE, 'title' => 'My Article 1', 'body' => 'This is a post', 'keywords' => ['foo', 'bar', '1', 'true', NULL]]]]]];
        $parser = new Parser();
        $this->assertEquals($expected, Executor::execute($blogSchema, NULL, $parser->parse(new Source($request)), '', []));
    }
Example #2
0
 /**
  * @dataProvider coercesOutputIntProvider
  *
  * @param mixed $input
  * @param int|null $expected
  */
 public function testCoercesOutputInt($input, $expected)
 {
     $this->assertSame($expected, Type::intType()->coerce($input));
 }
Example #3
0
 public function prohibitsPuttingNonObjcetTypesInUnionsProvider()
 {
     return [[Type::intType()], [new NonNullModifier(Type::intType())], [new ListModifier(Type::intType())], [new InterfaceType('Interface')], [new UnionType('Union')], [new EnumType('Enum')], [new InputObjectType('InputObject')]];
 }
Example #4
0
 /**
  * Re-populate static properties when de-serializing.
  */
 public function __wakeup()
 {
     Type::intType();
     Type::booleanType();
     Type::floatType();
     Type::idType();
     Type::stringType();
 }
Example #5
0
 protected function getSchema()
 {
     $dataType = new ObjectType('DataType', ['list' => ['type' => new ListModifier(Type::intType())], 'listOfNonNull' => ['type' => new ListModifier(new NonNullModifier(Type::intType()))], 'nonNullList' => ['type' => new NonNullModifier(new ListModifier(Type::intType()))], 'nonNullListOfNonNull' => ['type' => new NonNullModifier(new ListModifier(new NonNullModifier(Type::intType())))], 'listContainsNull' => ['type' => new ListModifier(Type::intType())], 'listOfNonNullContainsNull' => ['type' => new ListModifier(new NonNullModifier(Type::intType()))], 'nonNullListContainsNull' => ['type' => new NonNullModifier(new ListModifier(Type::intType()))], 'nonNullListOfNonNullContainsNull' => ['type' => new NonNullModifier(new ListModifier(new NonNullModifier(Type::intType())))], 'listReturnsNull' => ['type' => new ListModifier(Type::intType())], 'listOfNonNullReturnsNull' => ['type' => new ListModifier(new NonNullModifier(Type::intType()))], 'nonNullListReturnsNull' => ['type' => new NonNullModifier(new ListModifier(Type::intType()))], 'nonNullListOfNonNullReturnsNull' => ['type' => new NonNullModifier(new ListModifier(new NonNullModifier(Type::intType())))], 'nest' => ['type' => function () use(&$dataType) {
         return $dataType;
     }]]);
     $schema = new Schema($dataType);
     return $schema;
 }
Example #6
0
 public function testCorrectlyThreadsArguments()
 {
     $document = '
         query Example {
             b(numArg: 123, stringArg: "foo")
         }
     ';
     $gotHere = FALSE;
     $parser = new Parser();
     $ast = $parser->parse(new Source($document));
     $schema = new Schema(new ObjectType('Type', ['b' => ['args' => ['numArg' => ['type' => Type::intType()], 'stringArg' => ['type' => Type::stringType()]], 'type' => Type::stringType(), 'resolve' => function ($_, $args) use(&$gotHere) {
         $this->assertEquals(123, $args['numArg']);
         $this->assertEquals('foo', $args['stringArg']);
         $gotHere = TRUE;
     }]]));
     Executor::execute($schema, NULL, $ast, 'Example', []);
     $this->assertSame($gotHere, TRUE);
 }