youshido-php / GraphQLBundle

Pure PHP implementation of GraphQL Server – Symfony Bundle
MIT License
284 stars 44 forks source link

InputObject converted as string #64

Closed carere closed 6 years ago

carere commented 6 years ago

Hello, first of all, thx for the bundle.

My problem is when i try to send a mutation which delete an object i got this error:

Object of class Youshido\GraphQL\Parser\Ast\ArgumentValue\InputObject could not be converted to string

I don't understand why the inputObject would be converted to string, this is my mutation:

mutation { deleteContent(api_key: "xxx",id: 3) }

Here is my code in PHP for that mutation:

<?php

namespace AppBundle\GraphQL\Mutation\Content;

use AppBundle\Entity\Content;
use AppBundle\GraphQL\Type\ContentType;
use Youshido\GraphQL\Config\Field\FieldConfig;
use Youshido\GraphQL\Execution\ResolveInfo;
use Youshido\GraphQL\Type\NonNullType;
use Youshido\GraphQL\Type\Scalar\StringType;
use Youshido\GraphQLBundle\Field\AbstractContainerAwareField;
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
use Youshido\GraphQL\Type\Scalar\IntType;

class DeleteContentField extends AbstractContainerAwareField
{
  public function build(FieldConfig $config)
  {
    $config->addArguments([
      'api_key' => new NonNullType(new StringType()),
      'id' => new NonNullType(new IntType()),
    ]);
  }

  public function resolve($value, array $args, ResolveInfo $info)
  {
    if ($this->container->get('AppBundle\Service\AuthenticationFlow')->authenticate(['api_key' => $args['api_key']])) {
      $emf = $this->container->get('AppBundle\Service\EntityManagerFactory');
      $content = $emf->findContent($args['id']);
      $page = $content->getPage();
      $page->removeContent($content);
      $emf->save($page, true);
      $emf->remove($content, true);
      return 'Content Removed...';
    }
    throw new AuthenticationServiceException('Poblem during authentication. Please retry', 401);
  }

  public function getType()
  {
    return new StringType();
  }

  public function getName()
  {
    return 'deleteContent';
  }
}

For info, the item is deleted from DB, but when i use Apollo in order to launch my query (in my JS app) i got this error. If i launch the same query in the graphql explorer: no issue encountered.

Did i do anything wrong ? Hope someone can explain me what happen on this use case :)

portey commented 6 years ago

Hi, @carere

Can you check is Apollo query equal to explorer one? (use network tab in dev tools

carere commented 6 years ago

Hello, actually it comes from me...

Sorry for the issue, the problem was in the JS side, the stack error isn't as good as symfony's one :)

Thx again for the bundle !!!

portey commented 6 years ago

Also my recommendation is not to use *ContainerAwareField, the better solution is to use container from ResolveInfo: $info->getContainer().

carere commented 6 years ago

Thx for the advice,

I dumped the $info in order to see what it contains, and i saw that the $container property was set to null.

How can i inject the container in the $info variable ?

UPDATE

I saw that the $container is lazy-loaded :)

I will use this way in the future :)