Skip to content
This repository was archived by the owner on Feb 10, 2019. It is now read-only.
This repository was archived by the owner on Feb 10, 2019. It is now read-only.

How to use GraphQL UnionType?? #387

Open
@caizhigang97

Description

@caizhigang97

Hello everyone! Please look at the code:

<?php
namespace App\GraphQL\Query;

use Folklore\GraphQL\Support\Query;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use GraphQL;
class RecommandsQuery extends Query
{
    protected $attributes = [
        'name' => 'RecommandsQuery',
    ];
    public function type()
    { 
        return GraphQL::type('RecommandUnion');
    }
    public function args()
    { 
        return [
            'filter'                => [
                'name' => 'filter', 
                'type' => GraphQL::type('FollowFilter'),
            ],
        ]; 
    }
    public function resolve($root, $args, $context, ResolveInfo $info)
    {
        $qb = \App\Follow::orderBy('id', 'desc');

        $qb->when(isset($args['filter']), function ($q) use ($args) {
            switch ($args['filter']) {
                case 'CATEGORY':
                    return $q->where('followed_type', 'categories');
                case 'COLLECTION':
                    return $q->where('followed_type', 'collections');
                default:
                    break;
            }
        });
        return $qb->first()->followed;
    }
}
<?php

namespace App\GraphQL\Type;
use GraphQL;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\UnionType as BaseUnionType;

class RecommandUnionType extends BaseUnionType
{
    protected $attributes = [
        'name' => 'RecommandUnion',
        'description' => 'union'
    ];

    public function types()
    {
        return [
            GraphQL::type('Category'),
            GraphQL::type('Collection'),
        ];
    }

    public function resolveType($root)
    {
    	if($root->followed_type == 'categories'){
    		return GraphQL::type('Category');
    	}
       	return GraphQL::type('Collection');
    }
}

image

Can not run,How to define multi-type result?
Thanks~

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions