youshido-php/GraphQLBundle

I'm stuck.

The-Don-Himself opened this issue · 6 comments

OK, I managed to grasp the concept of GraphQL well and I love it, integrating this Bundle with Symfony however, has been a challenge.

An example would do wonders here, because different from the detailed examples in https://github.com/youshido/graphql/ the only examples that can make sense here are ones showing how you can call other services, like Doctrine ORM, ODM or FazlandElasticaBundle (improved FOSElasticaBundle) and the JMSSerializer to expose your data.

Anyone please?

Somewhat related issue #1

@The-Don-Himself we're working on a specific detailed example... but as of now I can help if you give a specific question.. The last two examples in https://github.com/Youshido/GraphQLBundle show how to call external services from the resolve function and how to define a field. Anyway, we'll update this issue as soon as we have some updates.

@viniychuk Thanks for the response, definitely understand the time constraint and greatly appreciate the work.

Let me paste the Schema.php files that I have tried and maybe that will better server to show where I and any other person might have gone wrong.

<?php
/**
 * This class was automatically generated by GraphQL Schema generator
 */

namespace AppBundle\GraphQL;

use Youshido\GraphQL\Schema\AbstractSchema;
use Youshido\GraphQL\Config\Schema\SchemaConfig;
use Youshido\GraphQL\Type\Scalar\StringType;
use Youshido\GraphQL\Type\Object\AbstractObjectType;

use AppBundle\GraphQL\PostType;
use AppBundle\GraphQL\PostField;
use AppBundle\GraphQL\RootDirField;

use Youshido\GraphQL\Field\Field;

class Schema extends AbstractSchema
{
    public function build(SchemaConfig $config)
    {

        $config->getQuery()->addFields([
            'hello' => [
                'type'    => new PostType(),
                'resolve' => function ($value, $args, $info)  // implementing resolve function
                {
                    return [
                        "title"   => "New approach in API has been revealed",
                        "summary" => "This post describes a new approach to create and maintain APIs",
                    ];
                }
            ]
        ]);

/*
        $config->getQuery()->addFields([
            'hello' => [
                'type'    => new PostType(),
                'resolve' => function ($value, $args, $info)  // implementing resolve function
                {
                    return [
                        'title' => new PostField([
                            'type'    => new PostType(),
                            'resolve' => ['@doctrine.orm.entity_manager', "getRepository('AppBundle:Discounts')->find(1)"]
                        ]),
                        "summary" => "This post describes a new approach to create and maintain APIs",
                    ];
                }
            ]
        ]);
*/
/*
        $config->getQuery()->addField(new Field([
            'name'    => 'cacheDir',
            'type'    => new StringType(),
            'resolve' => ['@doctrine.orm.entity_manager', "getRepository('AppBundle:Discounts')->find(1)->getTitle()"]
        ]));
*/
    }

}

PostType

<?php

namespace AppBundle\GraphQL;

use Youshido\GraphQL\Type\NonNullType;
use Youshido\GraphQL\Type\Object\AbstractObjectType;
use Youshido\GraphQL\Type\Scalar\IdType;
use Youshido\GraphQL\Type\Scalar\BooleanType;
use Youshido\GraphQL\Type\Scalar\IntType;
use Youshido\GraphQL\Type\Scalar\StringType;

class PostType extends AbstractObjectType
{
    public function build($config)
    {
        $config
            ->addField('title', new NonNullType(new StringType()))
            ->addField('summary', new StringType()); 
    }

    public function getName()
    {
        return "Post";
    }

}

PostField

<?php

namespace AppBundle\GraphQL;

use Youshido\GraphQL\Type\NonNullType;
use Youshido\GraphQLBundle\Field\AbstractContainerAwareField;
use Youshido\GraphQL\Execution\ResolveInfo;
use Youshido\GraphQL\Type\Scalar\BooleanType;
use Youshido\GraphQL\Type\Scalar\IntType;
use Youshido\GraphQL\Type\Scalar\StringType;
use Youshido\GraphQL\Type\Object\AbstractObjectType;

class PostField extends AbstractContainerAwareField
{
    /**
     * @inheritdoc
     */
    public function getType()
    {
        return new AbstractObjectType();
    }

    /**
     * @inheritdoc
     */
    public function resolve($value, array $args, ResolveInfo $info)
    {
        return $em = $this->container->get('doctrine')->getManager();
    }

    /**
     * @inheritdoc
     */
    public function getName()
    {
        return 'Post';
    }

}

In the first file (Schema.php) the first block of code works, the next two block do not most of the errors I get are along the lines

Config is not valid for Youshido\GraphQL\Field\Field
Field "resolve" expected to be "type" but got "array"

After more trial and error, I think I found the solution, closing for now and will update with how I making it work for me with 24hrs.
Thanks.