Graphql-laravel: Question about subquery args

Created on 17 Dec 2019  Â·  4Comments  Â·  Source: rebing/graphql-laravel

Versions:

  • graphql-laravel Version: 4.0.0
  • Laravel Version: latest version
  • PHP Version: 7.3.9

Question:

First time working with GraphQL, so sorry if my thinking is totally wrong.
What I'm trying to reach is:
I have a function inside a model to get some details:

    public function getMyCustomAttribute()
    {
        // Used by GraphQL
        $Details = MyDetailModel::Details($this->id);
        if(!$Details) return null;
        return  $Details;
    }

This works perfect like it should, I access it within a type, i query it with ( within a other type ):

'details' => [
   'alias'         => 'MyCustom',
    'type'          =>  Type::listOf(GraphQL::type('DetailType')),
    'selectable' => false,
    'description' => 'The details',
],

Now I want this function but with only a single detail, I tried it like this

     'detail' => [
                'args'          => [
                    'type' => [
                        'type' => Type::string(),
                    ],
                ],
                'alias'         => 'SingleDetail',
                 'selectable' => false,
                'type'          => Type::listOf(GraphQL::type('DetailType')),
                // $args are the local arguments passed to the relation
                // $query is the relation builder object
                // $ctx is the GraphQL context (can be customized by overriding `\Rebing\GraphQL\GraphQLController::queryContext`
                'query'         => function(array $args, $query, $ctx) {
                }
            ],

My question now, and my problem , how do i get the arg['type'] inside my mutator of my model? or am I thinking totally wrong?

question

All 4 comments

I _think_ maybe because you're using selectable => false, query, which is a feature of SelectFields, is not evaluated at all.

Is your plan to have $args['type'] to pass to MyDetailModel::Details(…)? Then you could do it in a resolve method (instead of query). The arguments should be the same for a regular resolver.

I am indeed trying to pass the args to my details() model function, how should I implent the resolve method?

'resolve' => function ($root, array $args) {
  return MyDetailModel::Details($args['type'])->first();
}

or something? I don't know enough details.

Alright, worked like a charm.

Thank you for your time!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

idubz33 picture idubz33  Â·  5Comments

AdrianCarreno picture AdrianCarreno  Â·  5Comments

edgarsn picture edgarsn  Â·  4Comments

drmax24 picture drmax24  Â·  6Comments

constantinosergiou picture constantinosergiou  Â·  3Comments