laravel-graphql icon indicating copy to clipboard operation
laravel-graphql copied to clipboard

Interface and Union not working

Open mastito03 opened this issue 7 years ago • 8 comments

When I try to use interface or union, laravel server always die.

using interface

class Event extends BaseType
{
    
    public function interfaces() {
        return [
            GraphQL::type('BaseEvent') // error when calling this
        ];
    }

}

when using union

class UnionEvent extends UnionType
{
    
    public function tipes() {
        return [
            GraphQL::type('Event') // error when calling this
        ];
    }

}

I have try to debug and find out that call stack is looping infinitely.

mastito03 avatar Dec 14 '17 10:12 mastito03

Is there a fix for this? I'm having the same Problem.

peripteraptos avatar Feb 20 '18 19:02 peripteraptos

try to specify keys for your types in config

dhlebin avatar Feb 25 '18 18:02 dhlebin

Not working for us either. We are specifying types in our Interface.

MatthewHallCom avatar Mar 06 '18 19:03 MatthewHallCom

Also running into this problem, does anyone have a fix?

Stack trace of the loop:

#10 /home/tyler/Development/vitals/app/GraphQL/Type/ReadingType.php(15): Illuminate\\Support\\Facades\\Facade::__callStatic('type', Array)
#11 /home/tyler/Development/vitals/vendor/folklore/graphql/src/Folklore/GraphQL/Support/Type.php(91): App\\GraphQL\\Type\\ReadingType->attributes()
#12 /home/tyler/Development/vitals/vendor/folklore/graphql/src/Folklore/GraphQL/Support/InterfaceType.php(29): Folklore\\GraphQL\\Support\\Type->getAttributes()
#13 /home/tyler/Development/vitals/vendor/folklore/graphql/src/Folklore/GraphQL/Support/UnionType.php(27): Folklore\\GraphQL\\Support\\InterfaceType->getAttributes()
#14 /home/tyler/Development/vitals/vendor/folklore/graphql/src/Folklore/GraphQL/Support/Type.php(136): Folklore\\GraphQL\\Support\\UnionType->getAttributes()
#15 /home/tyler/Development/vitals/vendor/folklore/graphql/src/Folklore/GraphQL/GraphQL.php(281): Folklore\\GraphQL\\Support\\Type->__get('name')
#16 /home/tyler/Development/vitals/vendor/folklore/graphql/src/Folklore/GraphQL/GraphQL.php(184): Folklore\\GraphQL\\GraphQL->getTypeName('App\\\\GraphQL\\\\Typ...', NULL)
#17 /home/tyler/Development/vitals/vendor/folklore/graphql/src/Folklore/GraphQL/ServiceProvider.php(95): Folklore\\GraphQL\\GraphQL->addType('App\\\\GraphQL\\\\Typ...', NULL)
#18 /home/tyler/Development/vitals/vendor/folklore/graphql/src/Folklore/GraphQL/ServiceProvider.php(182): Folklore\\GraphQL\\ServiceProvider->addTypes(Object(Folklore\\GraphQL\\GraphQL))
#19 /home/tyler/Development/vitals/vendor/laravel/framework/src/Illuminate/Container/Container.php(764): Folklore\\GraphQL\\ServiceProvider->Folklore\\GraphQL\\{closure}(Object(Illuminate\\Foundation\\Application), Array)
#20 /home/tyler/Development/vitals/vendor/laravel/framework/src/Illuminate/Container/Container.php(646): Illuminate\\Container\\Container->build(Object(Closure))
#21 /home/tyler/Development/vitals/vendor/laravel/framework/src/Illuminate/Container/Container.php(601): Illuminate\\Container\\Container->resolve('graphql', Array)
#22 /home/tyler/Development/vitals/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(734): Illuminate\\Container\\Container->make('graphql', Array)
#23 /home/tyler/Development/vitals/vendor/laravel/framework/src/Illuminate/Container/Container.php(1210): Illuminate\\Foundation\\Application->make('graphql')
#24 /home/tyler/Development/vitals/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(159): Illuminate\\Container\\Container->offsetGet('graphql')
#25 /home/tyler/Development/vitals/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(128): Illuminate\\Support\\Facades\\Facade::resolveFacadeInstance('graphql')
#26 /home/tyler/Development/vitals/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(215): Illuminate\\Support\\Facades\\Facade::getFacadeRoot()
#27 /home/tyler/Development/vitals/app/GraphQL/Type/ReadingType.php(15): Illuminate\\Support\\Facades\\Facade::__callStatic('type', Array)

Pinging @dmongeau and @phatshambler on this as I really would like to get it fixed, I can help submit a PR. It appears the issue has to do with laravels service container which explains why all the unit tests still pass.

tylergets avatar Apr 09 '18 18:04 tylergets

Ran into this problem as well when declaring a union Type. Found out it kept looping when trying to get the name attribute. The workaround that I did was to add a name property to the class

use Folklore\GraphQL\Support\UnionType as BaseUnionType;
use GraphQL;

class ComponentUnionType extends BaseUnionType
{
    public $name = 'LayoutComponent'; //Add this here
 ... rest of the code

fikri-marhan avatar Apr 11 '18 06:04 fikri-marhan

@fikri-marhan thanks for debugging and sharing, shouldn't this be declared in the $attributes property? If they're the same I can work on a PR to fix this.

I also find it weird that the exampleUnionType does not include the attribute yet the tests pass.

https://github.com/Folkloreatelier/laravel-graphql/blob/develop/tests/Objects/ExampleUnionType.php

tylergets avatar Apr 11 '18 12:04 tylergets

@tylergets in my class i defined it in the attributes as well but it didnt work. I suspect the call to getAttributes has a cyclic dependency issue

I didn’t dive deeper once i figured the workaround as it was acceptable to me. But let me know if I can help with anything

fikri-marhan avatar Apr 11 '18 12:04 fikri-marhan

After hitting this issue too with my first union type, the TL;DR for basic definition is this:

class YourUnionType extends UnionType
{
  public $name = 'YourUnion';
  protected $attributes = [
    'name' => 'YourUnion,
  ];

  // ... types(), resolveType(), 

I too didn't dug deeper, the type will be named after the $name property. Seems that the value in $attributes['name'] is ignored (but still needs to be present).

mfn avatar Jan 21 '19 12:01 mfn