overblog / GraphQLBundle

This bundle provides tools to build a complete GraphQL API server in your Symfony App.
MIT License
783 stars 221 forks source link

Attribute Only Mapping Throws InvalidConfigurationException #1080

Closed Bastianowicz closed 1 year ago

Bastianowicz commented 1 year ago
Q A
Bug report? yes
Feature request? no
BC Break report? might be
RFC? ?
Version/Branch 0.14.1

Reproduction

Expected

Experienced

Stack Trace ``` Symfony\Component\Config\Definition\Exception\InvalidConfigurationException: The path "overblog_graphql_types.RootQuery._object_config.fields" should have at least 1 element(s) defined. in webupdater/vendor/symfony/config/Definition/PrototypedArrayNode.php:184 Code: 0 Stack trace: #0 webupdater/vendor/symfony/config/Definition/BaseNode.php (455): Symfony\Component\Config\Definition\PrototypedArrayNode->finalizeValue(Array(0)) #1 webupdater/vendor/symfony/config/Definition/ArrayNode.php (245): Symfony\Component\Config\Definition\BaseNode->finalize(Array(0)) #2 webupdater/vendor/symfony/config/Definition/BaseNode.php (455): Symfony\Component\Config\Definition\ArrayNode->finalizeValue(Array(3)) #3 webupdater/vendor/symfony/config/Definition/ArrayNode.php (245): Symfony\Component\Config\Definition\BaseNode->finalize(Array(3)) #4 webupdater/vendor/symfony/config/Definition/BaseNode.php (455): Symfony\Component\Config\Definition\ArrayNode->finalizeValue(Array(5)) #5 webupdater/vendor/symfony/config/Definition/PrototypedArrayNode.php (177): Symfony\Component\Config\Definition\BaseNode->finalize(Array(3)) #6 webupdater/vendor/symfony/config/Definition/BaseNode.php (455): Symfony\Component\Config\Definition\PrototypedArrayNode->finalizeValue(Array(3)) #7 webupdater/vendor/symfony/config/Definition/Processor.php (36): Symfony\Component\Config\Definition\BaseNode->finalize(Array(3)) #8 webupdater/vendor/symfony/config/Definition/Processor.php (46): Symfony\Component\Config\Definition\Processor->process(Object: Symfony\Component\Config\Definition\PrototypedArrayNode, Array(1)) #9 webupdater/vendor/overblog/graphql-bundle/src/DependencyInjection/Compiler/ConfigParserPass.php (79): Symfony\Component\Config\Definition\Processor->processConfiguration(Object: Overblog\GraphQLBundle\DependencyInjection\TypesConfiguration, Array(1)) #10 webupdater/vendor/overblog/graphql-bundle/src/DependencyInjection/Compiler/ConfigParserPass.php (73): Overblog\GraphQLBundle\DependencyInjection\Compiler\ConfigParserPass->processConfiguration(Array(1)) #11 webupdater/vendor/symfony/dependency-injection/Compiler/Compiler.php (82): Overblog\GraphQLBundle\DependencyInjection\Compiler\ConfigParserPass->process(Object: Symfony\Component\DependencyInjection\ContainerBuilder) #12 webupdater/vendor/symfony/dependency-injection/ContainerBuilder.php (757): Symfony\Component\DependencyInjection\Compiler\Compiler->compile(Object: Symfony\Component\DependencyInjection\ContainerBuilder) #13 webupdater/vendor/symfony/http-kernel/Kernel.php (546): Symfony\Component\DependencyInjection\ContainerBuilder->compile() #14 webupdater/vendor/symfony/http-kernel/Kernel.php (787): Symfony\Component\HttpKernel\Kernel->initializeContainer() #15 webupdater/vendor/symfony/http-kernel/Kernel.php (190): Symfony\Component\HttpKernel\Kernel->preBoot() #16 webupdater/vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php (35): Symfony\Component\HttpKernel\Kernel->handle(Object: Symfony\Component\HttpFoundation\Request) #17 webupdater/vendor/autoload_runtime.php (35): Symfony\Component\Runtime\Runner\Symfony\HttpKernelRunner->run() #18 apiv2/public/index.php (9): require_once("/srv/www/vhosts/webroot/html/webupdater/vendor/autoload_runtime.php") ```

Code

/config/packages/graphql.yaml

overblog_graphql:
  definitions:
    schema:
      query: RootQuery
    mappings:
      types:
        - type: attribute
          dir: "%kernel.project_dir%/src/GraphQL"
          suffix: ~

/config/bundles.php

    // ...
    Overblog\GraphQLBundle\OverblogGraphQLBundle::class => ['all' => true],
    Overblog\GraphiQLBundle\OverblogGraphiQLBundle::class => ['dev' => true],

/config/services.yaml

// ...
com\our_namespace\api\v2\GraphQL\RootQuery:
    public: true

/src/GraphQL/RootQuery.php

<?php declare(strict_types=1);

namespace com\our_namespace\GraphQL;

use Overblog\GraphQLBundle\Annotation as GQL;

#[GQL\Type]
class RootQuery
{
    // empty as it is in the docs
}

/src/GraphQL/TestType.php

<?php declare(strict_types=1);

namespace com\our_namespace\api\v2\GraphQL\;

use Overblog\GraphQLBundle\Annotation as GQL;

#[GQL\Type]
class TestType
{
    #[GQL\Field(type: "Int")]
    public $myField = 1;
}

Further Information

Maybe this is not a bug but simply misunderstanding or misleading docs. We appreciate every kind of help here.

Bastianowicz commented 1 year ago

Well it was definitely our issue not yours. We were being helped in the symfony devs channel. The Problem for us was that we had not yet defined any providers and thus no types were being merged in the RootQuery. So what finally helped us was the following snippet. Something comparable would've helped us in the docs but maybe we just followed the wrong path.

#[GQL\Provider]
class MeResolver implements QueryInterface
{
    public function __construct(private TokenStorageInterface $tokenStorage)
    {
    }

    #[GQL\Query(name: 'me', type: 'User')]
    public function __invoke(): ?User
    {
        return $this->tokenStorage->getToken()?->getUser();
    }
}
aat-antoine commented 11 months ago

I get same error and no solution work. I guess doc should be updated to be easily use with php attributes or annotations.