GithubHelp home page GithubHelp logo

withCount about laravel-graphql HOT 2 CLOSED

folkloreinc avatar folkloreinc commented on July 29, 2024
withCount

from laravel-graphql.

Comments (2)

dmongeau avatar dmongeau commented on July 29, 2024

I need to see your classes... the query doesn't tell how you implemented it

from laravel-graphql.

hjJunior avatar hjJunior commented on July 29, 2024

I'm with difficult to use it

User model

public function posts () {
    return $this->hasMany('App\Post');
}

UserType

    public function fields()
    {
        return [
            ...
            'posts' => [
                'type' => Type::listOf(GraphQL::type('Post')),
                'description' => 'List of post of user',
                'resolver' => function (User $user) {
                    return $user->posts()->get();
                }
            ],
            'posts_count' => [
                'type' => Type::int(),
                'description' => 'Number of post by this user',
                'resolver' => function (User $user) {
                    return $user->withCount('posts')->first()->posts_count;
                }
            ]
        ];
    }

What I'm doing wrong? I'm getting null on result

image

The relationship its okay it I use in another place, I even have done a test

class PostTest extends TestCase
{
    public function testPostCount()
    {
        $this->assertEquals(1, User::find(1)->withCount('posts')->first()->posts_count, 'Should exist 1 post');
    }
}

Update

I have found that the problem is on UsersQuery, but how can I make dynamic?
For example, if I add posts to query, graphQL executes and get list of array, but in this case of count, how can I do? The way I have done isn't dynamic

    public function resolve($root, $args)
    {
        if (isset($args['id'])) {
            return User::where('id' , $args['id'])->get();
        } else if(isset($args['email'])) {
            return User::where('email', $args['email'])->get();
        } else {
            return User::withCount('posts')->get();
        }
    }

Update 2

I got to make this work, but I'm doubt, to make this work I added to method resolve to do the query and removed from UserType the resolvers, but I'm in doubt why this worked

            'posts' => [
                'type' => Type::listOf(GraphQL::type('Post')),
                'description' => 'List of post of user',
                'resolver' => function (User $user) {
                    return $user->posts()->get();
                }
            ],

and this not worked

            'posts_count' => [
                'type' => Type::int(),
                'description' => 'Number of post by this user',
                'resolver' => function (User $user) {
                    return $user->withCount('posts')->first()->posts_count;
                }
            ]

So to make posts_count work I'm supposed to do this on UsersQuery

    public function resolve($root, $args, $context, ResolveInfo $info)
    {
        $fields = $info->getFieldSelection($depth = 3);
        $users = User::query();

        foreach ($fields as $field => $keys) {
            if ($field === 'posts_count') {
                $users->withCount('posts');
            }
            if ($field === 'posts') {
                $users->with('posts');
            }
        }

        if (isset($args['id'])) {
            return $users->where('id' , $args['id'])->get();
        } else if(isset($args['email'])) {
            return $users->where('email', $args['email'])->get();
        }
        return $users->get();
    }

from laravel-graphql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.