Comments (6)
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.
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.
@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.
@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.
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.
Thanks!
from lighthouse.
Related Issues (20)
- Generate schema fail! HOT 1
- Exception Undefined array key "schemaExtensions" HOT 1
- Clear Cache (private) HOT 1
- Segmentation fault when handling large payloads HOT 8
- Requesting local scopes can pass parameters HOT 1
- New directive that mirrors functionality of `whereHas` for relations (not `@whereHasConditions`) HOT 1
- `make setup` failed on macOS
- Access context in `FieldMiddleware` HOT 3
- CanArgs is defined twice in schema-directives.graphql
- `extend scalar X` directives are lost
- @canFind is missing "model" argument in graphql definition HOT 1
- Allow to customize the unique key for `PaginatedModelsLoader`
- artisan lighthouse:union stub appears to be incorrect HOT 1
- Problem with subscription middleware HOT 2
- An error occurs in the SubscriptionRegistry when sending a subscription event via Subscription::broadcast with Laravel Octane (Swoole) HOT 5
- Laravel v11 support dependancy missing
- Enum Support for Morph Types HOT 1
- Mutation transaction seem not work HOT 4
- Failed to find class App\GraphQl\Queries\TodoQuery in namespaces [] for directive @paginate
- @whereNull and @whereNotNull No directive found!
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lighthouse.