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

How to use GraphQL UnionType?? #387

Open
caizhigang97 opened this issue Sep 20, 2018 · 2 comments
Open

How to use GraphQL UnionType?? #387

caizhigang97 opened this issue Sep 20, 2018 · 2 comments

Comments

@caizhigang97
Copy link

caizhigang97 commented Sep 20, 2018

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~

@zettamax
Copy link

zettamax commented Nov 3, 2018

Same problem, can anyone help with that?

@mfn
Copy link

mfn commented Jan 21, 2019

You can use the fragment syntax to discriminate from which of the union types you want to query the fields, e.g. see https://facebook.github.io/graphql/June2018/#sec-Unions :

union SearchResult = Photo | Person

type Person {
  name: String
  age: Int
}

type Photo {
  height: Int
  width: Int
}

type SearchQuery {
  firstSearchResult: SearchResult
}

Query:

{
  firstSearchResult {
    ... on Person {
      name
    }
    ... on Photo {
      height
    }
  }
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants