Skip to content

Instantly share code, notes, and snippets.

@DiederikvandenB
Last active November 15, 2017 15:13
Show Gist options
  • Save DiederikvandenB/1beee6e88d3671e72f2c639f17d10664 to your computer and use it in GitHub Desktop.
Save DiederikvandenB/1beee6e88d3671e72f2c639f17d10664 to your computer and use it in GitHub Desktop.
UnionType
{
"errors":[
{
"message":"An unknown error occurred.",
"location":[
{
"line":56,
"column":5
}
],
"class":"GraphQL\\Error\\InvariantViolation",
"debug":"Cannot return null for non-nullable field __InputValue.type."
}
]
}
<?php
namespace App\GraphQL\Query;
use GraphQL;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Definition\UnionType;
use Rebing\GraphQL\Support\Query;
use Rebing\GraphQL\Support\SelectFields;
class SearchQuery extends Query
{
/**
* The attributes that define this query.
*
* @var array
*/
protected $attributes = [
'name' => 'search',
];
/**
* This query returns a list of search results.
*
* @return GraphQL\Type\Definition\ListOfType
*/
public function type()
{
return Type::listOf(new UnionType([
'name' => 'SearchResult',
'types' => [
GraphQL::type('User'),
GraphQL::type('Deal'),
],
'resolveType' => function($value) {
if ($value->type === 'user') {
return GraphQL::type('user');
} else {
return GraphQL::type('venue');
}
}
]));
}
/**
* The arguments that are available for this query.
*
* @return array
*/
public function args(): array
{
return [
'query' => [
'name' => 'query',
'Type' => Type::string(),
],
];
}
/**
* Load the query response.
*
* @param $root
* @param array $args
* @return mixed
*/
public function resolve($root, array $args, SelectFields $fields)
{
dd($root, $args, $fields);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment