Graphqlbundle: Syntax error while fetching data

Created on 29 Nov 2018  路  4Comments  路  Source: overblog/GraphQLBundle

| Q | A
| ---------------- | -----
| Bug report? | no
| Feature request? | no
| BC Break report? | yes
| RFC? | no
| Version/Branch | 0.11.9

I'm implementing this library in a project and for the moment I only have the User and Group entities. The User works fine, but the Group gives me a syntax error. A group is owned by an user and I think the problem could be there...

Group entity:

<?php declare(strict_types=1);

namespace App\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;

class Group
{
    /**
     * @var string
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var
     */
    private $createdAt;

    /**
     * @var
     */
    private $updatedAt;

    /**
     * @var User
     */
    private $owner;

    /**
     * @var Collection|User[]
     */
    private $users;

    public function __construct(string $name, User $user)
    {
        $this->name = $name;
        $this->owner = $user;
        $this->createdAt = new \DateTime();
        $this->updatedAt = new \DateTime();
        $this->users = new ArrayCollection();
    }

    public function getId(): string
    {
        return $this->id;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function getCreatedAt(): \DateTime
    {
        return $this->createdAt;
    }

    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    public function setUpdatedAt($updatedAt): void
    {
        $this->updatedAt = $updatedAt;
    }

    public function getOwner(): User
    {
        return $this->owner;
    }

    public function getUsers(): Collection
    {
        return $this->users;
    }
}

Mapping file for entity:

App\Entity\Group:
  type: entity
  table: group

  id:
    id:
      type: uuid
      generator:
        strategy: CUSTOM
      customIdGenerator:
        class: Ramsey\Uuid\Doctrine\UuidGenerator

  manyToOne:
    owner:
      targetEntity: App\Entity\User

  manyToMany:
    users:
      targetEntity: App\Entity\User
      mappedBy: groups

  fields:
    name:
      type: string
      nullable: false
    createdAt:
      type: datetime
      nullable: false
    updatedAt:
      type: datetime
      nullable: false

Group type:

Group:
  type: object
  config:
    description: "A group"
    resolveField: '@resolver("App\\GraphQL\\Resolver\\GroupResolver", [info, value, args])'
    fields:
      id:
        type: String!
        description: "The unique ID for the group"
      name:
        type: String!
        description: "The name of the group"
      owner:
        type: User!
        description: "The owner of the group (an User)"
      createdAt:
        type: DateTime
        description: "The creation date of the group"
      updatedAt:
        type: DateTime
        description: "The last update date of the group"
      users:
        type: "[User]"
        description: "A collection of users in the group"

GroupResolver:

<?php declare(strict_types=1);

namespace App\GraphQL\Resolver;

use App\Entity\Group;
use App\Entity\User;
use App\Repository\GroupRepository;
use GraphQL\Type\Definition\ResolveInfo;
use Overblog\GraphQLBundle\Definition\Argument;
use Overblog\GraphQLBundle\Definition\Resolver\ResolverInterface;
use Overblog\GraphQLBundle\Relay\Connection\Paginator;

class GroupResolver implements ResolverInterface
{
    /**
     * @var GroupRepository
     */
    private $groupRepository;

    public function __construct(GroupRepository $groupRepository)
    {
        $this->groupRepository = $groupRepository;
    }

    public function __invoke(ResolveInfo $info, $value, Argument $args)
    {
        $method = $info->fieldName;
        return $this->$method($value, $args);
    }

    public function resolve(string $id): Group
    {
        return $this->groupRepository->findById($id);
    }

    public function name(Group $group): string
    {
        return $group->getName();
    }

    public function owner(Group $group): User
    {
        return $group->getOwner();
    }

    public function createdAt(Group $group): \DateTime
    {
        return $group->getCreatedAt();
    }

    public function updatedAt(Group $group): \DateTime
    {
        return $group->getUpdatedAt();
    }

    public function users(Group $group, Argument $args)
    {
        $users = $group->getUsers();
        $paginator = new Paginator(function ($offset, $limit) use ($users) {
            return array_slice($users, $offset, $limit ?? 10);
        });

        return $paginator->auto($args, count($users));
    }
}

And Query types:

Query:
  type: object
  config:
    fields:
      user:
        type: User
        args:
          id:
            type: String
        resolve: '@=resolver("App\\GraphQL\\Resolver\\UserResolver::resolve", [args["id"]])'
      group:
        type: Group
        args:
          id:
            type: String
        resolve: '@=resolver("App\\GraphQL\\Resolver\\GroupResolver::resolve", [args["id"]])'

This is the error I get:

{
  "errors": [
    {
      "debugMessage": "An exception occurred while executing 'SELECT t0.id AS id_1, t0.name AS name_2, t0.created_at AS created_at_3, t0.updated_at AS updated_at_4, t0.owner_id AS owner_id_5 FROM group t0 WHERE t0.id = ?' with params [\"d3e61925-ea1e-40db-a8bb-2d895f0af080\"]:\n\nSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group t0 WHERE t0.id = 'd3e61925-ea1e-40db-a8bb-2d895f0af080'' at line 1",
      "message": "Internal server Error",
      "category": "internal",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "group"
      ],
      "trace": [
        {
          "file": "/appdata/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php",
          "line": 184,
          "call": "Doctrine\\DBAL\\Driver\\AbstractMySQLDriver::convertException('An exception occurred while executing \\'SELECT t0.id AS id_1, t0.name AS name_2, t0.created_at AS created_at_3, t0.updated_at AS updated_at_4, t0.owner_id AS owner_id_5 FROM group t0 WHERE t0.id = ?\\' with params [\"d3e61925-ea1e-40db-a8bb-2d895f0af080\"]:\n\nSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \\'group t0 WHERE t0.id = \\'d3e61925-ea1e-40db-a8bb-2d895f0af080\\'\\' at line 1', instance of Doctrine\\DBAL\\Driver\\PDOException)"
        },
        {
          "file": "/appdata/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php",
          "line": 158,
          "call": "Doctrine\\DBAL\\DBALException::wrapException(instance of Doctrine\\DBAL\\Driver\\PDOMySql\\Driver, instance of Doctrine\\DBAL\\Driver\\PDOException, 'An exception occurred while executing \\'SELECT t0.id AS id_1, t0.name AS name_2, t0.created_at AS created_at_3, t0.updated_at AS updated_at_4, t0.owner_id AS owner_id_5 FROM group t0 WHERE t0.id = ?\\' with params [\"d3e61925-ea1e-40db-a8bb-2d895f0af080\"]:\n\nSQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \\'group t0 WHERE t0.id = \\'d3e61925-ea1e-40db-a8bb-2d895f0af080\\'\\' at line 1')"
        },
        {
          "file": "/appdata/www/vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php",
          "line": 943,
          "call": "Doctrine\\DBAL\\DBALException::driverExceptionDuringQuery(instance of Doctrine\\DBAL\\Driver\\PDOMySql\\Driver, instance of Doctrine\\DBAL\\Driver\\PDOException, 'SELECT t0.id AS id_1, t0.name AS name_2, t0.created_at AS created_at_3, t0.updated_at AS updated_at_4, t0.owner_id AS owner_id_5 FROM group t0 WHERE t0.id = ?', array(1))"
        },
        {
          "file": "/appdata/www/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php",
          "line": 733,
          "call": "Doctrine\\DBAL\\Connection::executeQuery('SELECT t0.id AS id_1, t0.name AS name_2, t0.created_at AS created_at_3, t0.updated_at AS updated_at_4, t0.owner_id AS owner_id_5 FROM group t0 WHERE t0.id = ?', array(1), array(1))"
        },
        {
          "file": "/appdata/www/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php",
          "line": 751,
          "call": "Doctrine\\ORM\\Persisters\\Entity\\BasicEntityPersister::load(array(1), null)"
        },
        {
          "file": "/appdata/www/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php",
          "line": 460,
          "call": "Doctrine\\ORM\\Persisters\\Entity\\BasicEntityPersister::loadById(array(1))"
        },
        {
          "file": "/appdata/www/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php",
          "line": 154,
          "call": "Doctrine\\ORM\\EntityManager::find('App\\Entity\\Group', array(0), null, null)"
        },
        {
          "file": "/appdata/www/src/Repository/GroupRepository.php",
          "line": 16,
          "call": "Doctrine\\ORM\\EntityRepository::find('d3e61925-ea1e-40db-a8bb-2d895f0af080')"
        },
        {
          "file": "/appdata/www/src/GraphQL/Resolver/GroupResolver.php",
          "line": 33,
          "call": "App\\Repository\\GroupRepository::findById('d3e61925-ea1e-40db-a8bb-2d895f0af080')"
        },
        {
          "file": "/appdata/www/vendor/overblog/graphql-bundle/src/Resolver/AbstractProxyResolver.php",
          "line": 38,
          "call": "App\\GraphQL\\Resolver\\GroupResolver::resolve('d3e61925-ea1e-40db-a8bb-2d895f0af080')"
        },
        {
          "file": "/appdata/www/var/cache/dev/overblog/graphql-bundle/__definitions__/QueryType.php",
          "line": 56,
          "call": "Overblog\\GraphQLBundle\\Resolver\\AbstractProxyResolver::resolve(array(2))"
        },
        {
          "file": "/appdata/www/vendor/overblog/graphql-bundle/src/Resolver/Resolver.php",
          "line": 56,
          "call": "Overblog\\GraphQLBundle\\__DEFINITIONS__\\QueryType::Overblog\\GraphQLBundle\\__DEFINITIONS__\\{closure}(null, instance of Overblog\\GraphQLBundle\\Definition\\Argument(1), instance of ArrayObject(1), instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/appdata/www/vendor/webonyx/graphql-php/src/Executor/Executor.php",
          "line": 781,
          "call": "Overblog\\GraphQLBundle\\Resolver\\Resolver::Overblog\\GraphQLBundle\\Resolver\\{closure}(null, array(1), instance of ArrayObject(1), instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/appdata/www/vendor/webonyx/graphql-php/src/Executor/Executor.php",
          "line": 744,
          "call": "GraphQL\\Executor\\Executor::resolveOrError(instance of GraphQL\\Type\\Definition\\FieldDefinition, instance of GraphQL\\Language\\AST\\FieldNode, instance of Closure, null, instance of ArrayObject(1), instance of GraphQL\\Type\\Definition\\ResolveInfo)"
        },
        {
          "file": "/appdata/www/vendor/webonyx/graphql-php/src/Executor/Executor.php",
          "line": 473,
          "call": "GraphQL\\Executor\\Executor::resolveField(GraphQLType: Query, null, instance of ArrayObject(1), array(1))"
        },
        {
          "file": "/appdata/www/vendor/webonyx/graphql-php/src/Executor/Executor.php",
          "line": 349,
          "call": "GraphQL\\Executor\\Executor::executeFields(GraphQLType: Query, null, array(0), instance of ArrayObject(1))"
        },
        {
          "file": "/appdata/www/vendor/webonyx/graphql-php/src/Executor/Executor.php",
          "line": 309,
          "call": "GraphQL\\Executor\\Executor::executeOperation(instance of GraphQL\\Language\\AST\\OperationDefinitionNode, null)"
        },
        {
          "file": "/appdata/www/vendor/webonyx/graphql-php/src/Executor/Promise/Adapter/SyncPromiseAdapter.php",
          "line": 59,
          "call": "GraphQL\\Executor\\Executor::GraphQL\\Executor\\{closure}(array(2), array(2))"
        },
        {
          "file": "/appdata/www/vendor/webonyx/graphql-php/src/Executor/Executor.php",
          "line": 310,
          "call": "GraphQL\\Executor\\Promise\\Adapter\\SyncPromiseAdapter::create(instance of Closure)"
        },
        {
          "file": "/appdata/www/vendor/webonyx/graphql-php/src/Executor/Executor.php",
          "line": 166,
          "call": "GraphQL\\Executor\\Executor::doExecute()"
        },
        {
          "file": "/appdata/www/vendor/webonyx/graphql-php/src/GraphQL.php",
          "line": 155,
          "call": "GraphQL\\Executor\\Executor::promiseToExecute(instance of GraphQL\\Executor\\Promise\\Adapter\\SyncPromiseAdapter, instance of Overblog\\GraphQLBundle\\Definition\\Type\\ExtensibleSchema, instance of GraphQL\\Language\\AST\\DocumentNode, null, instance of ArrayObject(1), null, null, null)"
        },
        {
          "file": "/appdata/www/vendor/overblog/graphql-bundle/src/Executor/Executor.php",
          "line": 22,
          "call": "GraphQL\\GraphQL::promiseToExecute(instance of GraphQL\\Executor\\Promise\\Adapter\\SyncPromiseAdapter, instance of Overblog\\GraphQLBundle\\Definition\\Type\\ExtensibleSchema, 'query {\n  group(id: \"d3e61925-ea1e-40db-a8bb-2d895f0af080\") {\n    id\n  }\n}', null, instance of ArrayObject(1), null, null)"
        },
        {
          "file": "/appdata/www/vendor/overblog/graphql-bundle/src/Request/Executor.php",
          "line": 148,
          "call": "Overblog\\GraphQLBundle\\Executor\\Executor::execute(instance of Overblog\\GraphQLBundle\\Definition\\Type\\ExtensibleSchema, 'query {\n  group(id: \"d3e61925-ea1e-40db-a8bb-2d895f0af080\") {\n    id\n  }\n}', null, instance of ArrayObject(1), null, null)"
        },
        {
          "file": "/appdata/www/vendor/overblog/graphql-bundle/src/Controller/GraphController.php",
          "line": 159,
          "call": "Overblog\\GraphQLBundle\\Request\\Executor::execute(null, array(3))"
        },
        {
          "file": "/appdata/www/vendor/overblog/graphql-bundle/src/Controller/GraphController.php",
          "line": 119,
          "call": "Overblog\\GraphQLBundle\\Controller\\GraphController::processNormalQuery(instance of Symfony\\Component\\HttpFoundation\\Request, null)"
        },
        {
          "file": "/appdata/www/vendor/overblog/graphql-bundle/src/Controller/GraphController.php",
          "line": 88,
          "call": "Overblog\\GraphQLBundle\\Controller\\GraphController::processQuery(instance of Symfony\\Component\\HttpFoundation\\Request, null, false)"
        },
        {
          "file": "/appdata/www/vendor/overblog/graphql-bundle/src/Controller/GraphController.php",
          "line": 59,
          "call": "Overblog\\GraphQLBundle\\Controller\\GraphController::createResponse(instance of Symfony\\Component\\HttpFoundation\\Request, null, false)"
        },
        {
          "file": "/appdata/www/vendor/symfony/http-kernel/HttpKernel.php",
          "line": 149,
          "call": "Overblog\\GraphQLBundle\\Controller\\GraphController::endpointAction(instance of Symfony\\Component\\HttpFoundation\\Request, null)"
        },
        {
          "file": "/appdata/www/vendor/symfony/http-kernel/HttpKernel.php",
          "line": 66,
          "call": "Symfony\\Component\\HttpKernel\\HttpKernel::handleRaw(instance of Symfony\\Component\\HttpFoundation\\Request, 1)"
        },
        {
          "file": "/appdata/www/vendor/symfony/http-kernel/Kernel.php",
          "line": 188,
          "call": "Symfony\\Component\\HttpKernel\\HttpKernel::handle(instance of Symfony\\Component\\HttpFoundation\\Request, 1, true)"
        },
        {
          "file": "/appdata/www/public/index.php",
          "line": 25,
          "call": "Symfony\\Component\\HttpKernel\\Kernel::handle(instance of Symfony\\Component\\HttpFoundation\\Request)"
        }
      ]
    }
  ],
  "data": {
    "group": null
  }
}

I will appreciate a lot any help with this or provide more info is needed :)

Thanks in advance

Most helpful comment

group is a keyword in mysql. If you use it as table name you need to quote it. I suggest renaming it the table to user_group.

Anyway this has absolutely nothing to do with GraphQL or this package.

All 4 comments

group is a keyword in mysql. If you use it as table name you need to quote it. I suggest renaming it the table to user_group.

Anyway this has absolutely nothing to do with GraphQL or this package.

The user_group is actually the join table for both
image

EDITED:
You are right. So I think I can rename the table to groups to avoid this kind of errors

@JuanWilde Personally I don't like plural in table names so I'd recommend user, user_group and user_group_user.

Makes sense. Actually I always use singular for table names. But I though this could be an exception. I'll refactor this thing and keep migrating :)

Was this page helpful?
0 / 5 - 0 ratings