laravel-graphql
laravel-graphql copied to clipboard
Support shorthand notation
Hi, just started using this lib. At the graphql-php docs here it says it supports a shorthand notation for defining fields for types. But when i try to use them, i get the following error:
Cannot use object of type GraphQL\\Type\\Definition\\NonNull as array
Here's the class type that uses the shorthand notation:
<?php
namespace App\GraphQL\Type;
use GraphQL\Type\Definition\Type;
use Folklore\GraphQL\Support\Type as GraphQLType;
class UserType extends GraphQLType
{
protected $attributes = [
'name' => 'User',
'description' => 'A user'
];
/*
* Uncomment following line to make the type input object.
* http://graphql.org/learn/schema/#input-types
*/
// protected $inputObject = true;
public function fields()
{
return [
'id' => Type::nonNull(Type::string()), // <--- over here
'email' => [
'type' => Type::string(),
'description' => 'The email of user'
],
'name' => [
'type' => Type::string(),
],
];
}
// If you want to resolve the field yourself, you can declare a method
// with the following format resolve[FIELD_NAME]Field()
protected function resolveEmailField($root, $args)
{
return strtolower($root->email);
}
protected function resolveNameField($root, $args)
{
return $root->name;
}
}
I'd check the resolve function in the query type. If you think the problem is the id in UserType, you can do:
'id' => [
'type' => Type::nonNull(Type::string()),
'resolve' => function ($root, $args) {
return 'foo';
}
],
that will force the id field to return foo as string
@faiverson, using 'id' => [ 'type' => Type::nonNull(Type::string())]
solves the problem, but the main ideia here is the convenience of using the shorthant notation supported by the upstream library.
You might want to check this pull request that was merged today https://github.com/Folkloreatelier/laravel-graphql/pull/281
I'm not sure that's quite what he's asking for. I would actually like this as well. When defining args or fields, it'd be nice to have to only pass a type (Type::string()
), instead of an array ['type' => Type::string()]
.
Thus,
'id' => Type::string(),
would be equivalent to
'id' => [
'type' => Type::string(),
],
@adamgoose, yep that's what I've meant.
It's an easy workaround though, e.g.
public function fields()
{
$array = [
'id' => Type::nonNull(Type::string()),
'email' => Type::string(),
'name' => Type::string(),
'posts' => Type::listOf(GraphQL::type('Post')),
'followers' => Type::listOf(GraphQL::type('User')),
'followees' => Type::listOf(GraphQL::type('User'))
];
$array = array_map(function($el) {
if (is_array($el)) {
return $el;
}
return [
'type' => $el,
'resolve' => [$this, 'resolveField']
];
},$array);
return $array;
}
`
Yes, I agree. While working with this gem, we need to write a lot of code, definitely comparing with graphql implementations of Rails. Would be great to decrease it a bit.
Would not be better to take it directly from the models? $visible, $hidden, relations...
This could be somehow done writing some annotations on the models, routes and controllers. As nd approach to this, this library does it very good,
Exactly, in that case we just need to define it once