So, in our project we're using Laravel 5.5 + MongoDB + GraphQL and I've realized that this connection is current not so perfect and has some problems. For notice I'm using https://github.com/jenssegers/laravel-mongodb . The problem was with using Embeded relation in mongo. I'll give an example from my code.
This is a relation in User Model:
public function images()
{
return $this->embedsMany('App\Image');
}
And this is from User Type for GraphQL:
public function fields()
{
return [
'_id' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The id of the user'
],
'name' => [
'type' => Type::string(),
'description' => 'The name of user'
],
'images' => [
'type' => Type::listOf(GraphQL::type('images'))
'description' => 'List of user`s photos'
]
];
}
I've made small research and found that in \Rebing\GraphQL\Support\SelectFields file we have a method named handleFields that checks existing of method that return foreign key in current relation Class. But mongo library has its own method in Embeded relation for this purpose - getQualifiedParentKeyName() and ofc it doesn't supported by graphql lib. So I've just put new "if else" into the handleFields and it should be something like this:
So, in our project we're using Laravel 5.5 + MongoDB + GraphQL and I've realized that this connection is current not so perfect and has some problems. For notice I'm using https://github.com/jenssegers/laravel-mongodb . The problem was with using Embeded relation in mongo. I'll give an example from my code. This is a relation in User Model:
And this is from User Type for GraphQL:
If your run some query like:
You`ll probably receive an error:
I've made small research and found that in
\Rebing\GraphQL\Support\SelectFields
file we have a method namedhandleFields
that checks existing of method that return foreign key in current relation Class. But mongo library has its own method in Embeded relation for this purpose -getQualifiedParentKeyName()
and ofc it doesn't supported by graphql lib. So I've just put new "if else" into thehandleFields
and it should be something like this:I hope that this is will be helpful for someone else too. Also, perhaps this fix will be added in future updates for better work with mongo users.