Ecodev/graphql-doctrine

Documentation generation error

pawel-zawadzki opened this issue · 3 comments

I've encountered a problem with automatic documentation generation when using generic filters. Filtering in queries is working fine but I get a following error when trying to fetch docs:

Error: TypeFilterGroupJoin fields must be an object with field names as keys or a function which returns such an object.
    at invariant (chrome-extension://fkkiamalmpiidkljmicmjfbieiclmeij/dist/chromeiql.js:14605:11)
    at GraphQLInputObjectType._defineFieldMap (chrome-extension://fkkiamalmpiidkljmicmjfbieiclmeij/dist/chromeiql.js:14084:56)
    at GraphQLInputObjectType.getFields (chrome-extension://fkkiamalmpiidkljmicmjfbieiclmeij/dist/chromeiql.js:14075:49)
    at typeMapReducer (chrome-extension://fkkiamalmpiidkljmicmjfbieiclmeij/dist/chromeiql.js:16618:26)
    at chrome-extension://fkkiamalmpiidkljmicmjfbieiclmeij/dist/chromeiql.js:16621:20
    at Array.forEach (<anonymous>)
    at typeMapReducer (chrome-extension://fkkiamalmpiidkljmicmjfbieiclmeij/dist/chromeiql.js:16619:28)
    at chrome-extension://fkkiamalmpiidkljmicmjfbieiclmeij/dist/chromeiql.js:16621:20
    at Array.forEach (<anonymous>)
    at typeMapReducer (chrome-extension://fkkiamalmpiidkljmicmjfbieiclmeij/dist/chromeiql.js:16619:28)

I'm using ChromeiQL (same thing happens in GraphiQL Feen). When I comment out args providing generic filters documentation generates without an error. Since this is happening when using two different clients is seems it may be a backend problem.

Here's my code:

$types = new Types($this->objectManager, $this->container);

        $schema = new Schema([
            'query' => new ObjectType([
                'name' => 'productList',
                'description' => 'Fetches product list',
                'fields' => [
                    'products' => [
                        'type' => Type::listOf($types->getOutput(Product::class)), // Use automated ObjectType for output
                        'args' => [
                            [
                                'name' => 'filter',
                                'type' => $types->getFilter(Product::class), // Use automated filtering options
                            ],
                            [
                                'name' => 'sorting',
                                'type' => $types->getSorting(Product::class), // Use automated sorting options
                            ],
                        ],
                        'resolve' => function ($root, $args) use ($types) {
                            $queryBuilder = $types->createFilteredQueryBuilder(Product::class, $args['filter'] ?? [], $args['sorting'] ?? []);

                            return $queryBuilder->getQuery()->execute();
                        },
                    ]
                ]
            ])
        ]);
        $server = new StandardServer([
            'schema' => $schema,
            'queryBatching' => true,
            'debug' => true,
            'fieldResolver' => new DefaultFieldResolver()
        ]);

Btw, I'm using Zend Expressive and the entity mapping is very simple (Product with couple of fields and ManyToOne association to Type entity).

First of all I suggest you cover your schema with unit test. That way you don't have to test it in external tools. With PHPUnit it could look like:

    public function testSchemaIsValid(): void
    {
        $schema = new Schema();
        $schema->assertValid();

        self::assertTrue(true, 'schema passes validation');
    }

Second your code is not enough to reproduce the case. Can you isolate the issue by simplifying as much as possible your models and posting it here, so we have a proper Minimal, Complete, and Verifiable example.

Also what version of the lib are you using ?

Validating schema did help me to solve the problem. It seems like unidirectional ManyToOne associations are not supported (that was the case with Product - ProductType association). Changed it to bidirectional association and docs are generating fine.

seems like unidirectional ManyToOne associations are not supported

This is incorrect, they should be supported. \GraphQLTests\Doctrine\Blog\Model\User::$manager in our unit tests is an example of unidirectional ManyToOne relation. There may have been something else that was wrong (in the mapping or in the lib). It should be investigated further if you stumble on it again in the future.