Giter VIP home page Giter VIP logo

Comments (6)

chrissm79 avatar chrissm79 commented on July 19, 2024

Hey @hosmelq sorry for the delay!

So the only part your missing is the Edge in your mutation. First, use Lighthouse's edge command to create a new Edge class:

$ php artisan lighthouse:edge CommentEdge

Now update the new class to fit your mutation requirements:

namespace App\Http\GraphQL\Edges;

use Nuwave\Lighthouse\Support\Interfaces\ConnectionEdge;

class CommentEdge implements ConnectionEdge
{
    /**
     * Name of edge.
     *
     * @return string
     */
    public function name()
    {
        return 'comment';
    }

    /**
     * Edge type.
     *
     * @return string
     */
    public function type()
    {
        return 'comment';
    }

    /**
     * Extract edge from payload.
     *
     * @param  mixed $payload
     * @return mixed
     */
    public function edge($payload)
    {
        return $payload['comment'];
    }

    /**
     * Resolve cursor.
     *
     * @param  mixed $payload
     * @return mixed
     */
    public function cursor($payload)
    {
        return $payload['game']->comments()->count();
    }
}

And finally, use that edge in your mutation:

use GraphQL;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Hub\Game;
use Nuwave\Lighthouse\Support\Definition\GraphQLMutation;
use Nuwave\Lighthouse\Support\Interfaces\RelayMutation;

class CreateCommentMutation extends GraphQLMutation implements RelayMutation
{
    /**
     * Attributes of mutation.
     *
     * @var array
     */
    protected $attributes = [
        'description' => '',
        'name'        => 'createComment',
    ];

    /**
     * Available arguments on mutation.
     *
     * @return array
     */
    public function args()
    {
        return [
            'body'            => [
                'name'  => 'body',
                'type'  => Type::nonNull(Type::string()),
                'rules' => ['required'],
            ],
            'parentCommentId' => [
                'name' => 'parentCommentId',
                'type' => Type::ID(),
            ],
            'subjectId'       => [
                'name'  => 'subjectId',
                'type'  => Type::nonNull(Type::ID()),
                'rules' => ['required'],
            ],
            'subjectType'     => [
                'name'  => 'subjectType',
                'type'  => Type::nonNull(Type::string()),
                'rules' => ['required'],
            ],
        ];
    }

    /**
     * List of output fields.
     *
     * @return array
     */
    public function outputFields()
    {
        return [
            'commentEdge' => GraphQL::edge(new \App\Http\GraphQL\Edges\CommentEdge)->field(),
        ];
    }

    /**
     * Resolve the mutation.
     *
     * @param  array $args
     * @param  mixed $context
     * @param  ResolveInfo $info
     * @return mixed
     */
    public function mutateAndGetPayload(array $args, $context, ResolveInfo $info)
    {
        if ($args['subjectType'] == 'Game') {
            return $this->createGameComment($args);
        }
    }

    private function createGameComment($args)
    {
        $subjectId = $this->decodeRelayId($args['subjectId']);
        $parentCommentId = $args['parentCommentId'] ? $this->decodeRelayId($args['parentCommentId']) : null;
        $game = Game::findOrFail($subjectId); 

        $comment = $game->comments()->create([
            'body'        => $args['body'],
            'parent_id'   => $parentCommentId,
            'provider_id' => auth('providers')->id(),
            'user_id'     => auth('users')->id(),
        ]);

        return ['game' => $game, 'comment' => $comment];
    }
}

That's it! Let me know if you run into any issues here though

from lighthouse.

hosmelq avatar hosmelq commented on July 19, 2024

Thank you for your reply. I got the following error.

Schema must contain unique named types but contains multiple types named "CommentEdge".

I have a CommentConnection that is used in GameType, if i comment the connection the error disapears.

from lighthouse.

chrissm79 avatar chrissm79 commented on July 19, 2024

@hosmelq it seems that you may be using CommentEdge somewhere else in your code (or there is an issue w/ Lighthouse). Are you using this edge more than once or have you manually created a type somewhere and named it CommentEdge?

from lighthouse.

hosmelq avatar hosmelq commented on July 19, 2024

@chrissm79 I'm only using it once and i don't have a CommentEdge type.

This is the connection in GameType.

'comments' => GraphQL::connection('comment')
    ->resolve(function (Game $parent, array $args) {
        return $parent->comments()->whereNull('parent_id')->getConnection($args);
    })
    ->field(),

I think the call to connection is registering a CommentEdge .

from lighthouse.

chrissm79 avatar chrissm79 commented on July 19, 2024

That's likely what it is (the connection generating a CommentEdge), so I generally prepend the name of the parent on my edges. In your case it would be gameComments, so adjust the name function your edge class like so and see if it resolves the issue:

class CommentEdge implements ConnectionEdge
{
    /**
     * Name of edge.
     *
     * @return string
     */
    public function name()
    {
        return 'gameComments';
    }

    // ...
}

Otherwise you could manually create an edge:

/**
 * List of output fields.
 *
 * @return array
 */
public function outputFields()
{
    return [
        'commentEdge' => [
            'name' => 'GameCommentsEdge',
            'description' => '',
            'fields' => [
                'node' => [
                    'type' => GraphQL::type('comment'),
                    'resolve' => function ($payload) {
                        return $payload['comment']
                    }
                ],
                'cursor' => [
                    'type' => Type::nonNull(Type::string()),
                    'resolve' => function ($payload) {
                        return $this->encodeGlobalId('arrayconnection', $payload['game']->comments()->count());
                    }
                ]
            ],
        ],
    ];
}

from lighthouse.

hosmelq avatar hosmelq commented on July 19, 2024

Thanks!

from lighthouse.

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.