Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: resolve all pivot attributes #2464

Merged
merged 7 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ You can find and compare releases at the [GitHub release page](https://github.co

## Unreleased

### Added

- Resolve all pivot attributes https://github.com/nuwave/lighthouse/pull/2464

## v6.32.0

### Added
Expand Down
21 changes: 20 additions & 1 deletion docs/master/api-reference/directives.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ type RoleUserPivot {
```

When using the `type` argument with pagination style `CONNECTION`, you may create your own [edge type](https://facebook.github.io/relay/graphql/connections.htm#sec-Edge-Types)
that contains the attributes of the intermediate table.
that either contains the attributes of the intermediate table or contains a `pivot` field with the corresponding type.

The custom edge type must contain at least the following two fields:

Expand All @@ -441,6 +441,25 @@ type RoleEdge {
}
```

As an alternative, you can also expose the `pivot` field on the edge:

```graphql
type User {
roles: [Role!]! @belongsToMany(type: CONNECTION)
}

type UserRole {
meta: String
}

type RoleEdge {
node: Role!
cursor: String!
pivot: UserRole
}
```


## @broadcast

```graphql
Expand Down
4 changes: 4 additions & 0 deletions src/Pagination/ConnectionField.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ public function edgeResolver(Paginator $paginator, array $args, GraphQLContext $
$data['node'] = $item;
break;

case 'pivot':
$data['pivot'] = $item->pivot;
break;

default:
// All other fields on the return type are assumed to be part
// of the edge, so we try to locate them in the pivot attribute
spawnia marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
54 changes: 54 additions & 0 deletions tests/Integration/Schema/Directives/BelongsToManyDirectiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,60 @@ public function testQueryBelongsToManyRelayConnectionWithCustomEdgeUsingDirectiv
])->assertJsonCount(2, 'data.user.roles.edges');
}

public function testQueryBelongsToManyRelayConnectionWithCustomEdgeHavingPivotFieldUsingDirective(): void
{
$this->schema = /** @lang GraphQL */ '
type User {
roles: [Role!]! @belongsToMany(type: CONNECTION, edgeType: "UserRoleEdge")
}

type Role {
id: ID!
}

type UserRole {
meta: String
}

type UserRoleEdge {
node: Role!
cursor: String!
pivot: UserRole
}

type Query {
user: User! @auth
}
';

$user = factory(User::class)->create();
assert($user instanceof User);
$this->be($user);

$roles = factory(Role::class, 3)->create();
$meta = ['meta' => 'new'];
$user->roles()->attach($roles, $meta);

$this->graphQL(/** @lang GraphQL */ '
{
user {
roles(first: 2) {
edges {
pivot {
meta
}
node {
id
}
}
}
}
}
')
->assertJsonPath('data.user.roles.edges.*.pivot', array_fill(0, 2, $meta))
->assertJsonCount(2, 'data.user.roles.edges');
}

public function testQueryBelongsToManyPivot(): void
{
$this->schema = /** @lang GraphQL */ '
Expand Down
Loading