folkloreinc / laravel-graphql

Facebook GraphQL for Laravel 5. It supports Relay, eloquent models, validation and GraphiQL.
1.76k stars 234 forks source link

Interface and Union not working #247

Open mastito03 opened 6 years ago

mastito03 commented 6 years ago

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.

peripteraptos commented 6 years ago

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

dhlebin commented 6 years ago

try to specify keys for your types in config

MatthewHallCom commented 6 years ago

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

tylergets commented 6 years ago

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.

fikri-marhan commented 6 years ago

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
tylergets commented 6 years ago

@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

fikri-marhan commented 6 years ago

@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

mfn commented 5 years ago

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).