Eloquent team-based roles - laravel

I have 3 tables: Roles, Teams, Users with a pivot table between each: role_team, role_user, team_user.
I'm having a hard time leveraging Eloquent to return only the roles that a user has for a specific team.
$team = Team::find(1);
foreach($team->users as $user) {
dump($user->teamRoles); // Get the roles this user has for the team
}
While I could do $user->roles()->where('team_id', $team->id)->get(), I'd like to specify it as a relationship. I tried setting up a hasManyThrough, but it doesn't seem to work in this specific case.
The need to use this as a relationship rather than query is because I'm using Lighthouse PHP for GraphQL and would like to easily be able to query for the roles like:
teams {
id name
users {
teamPivot {
roles { id name }
}
}
}
Any help leveraging Eloquent to make this happen would be greatly appreciated.

One possible solution, though not necessarily the one I'm looking for is to use the #method directive on a field.
Imagine the following schema:
type User {
id: ID!
email: String!
teams: [Team] #belongsToMany
teamMeta: TeamUser
}
type Team {
id: ID!
name: String!
users: [User] #belongsToMany
userMeta: TeamUser
}
type Role {
id: ID!
name: String!
team: Team #belongsTo
users: [User] #belongsToMany
}
type TeamUser {
user: User!
team: Team!
roles: [Role] #method(name: "getTeamRoles")
}
Where getTeamRoles looks like:
public function getTeamRoles()
{
return $this->user->roles()->where('team_id', $this->team->id)->get();
}
This configuration will allow the following GraphQL to work as desired:
users(first: 1, input: { id: 2 }) {
email teams {
name userMeta {
contactedAt
roles { id name }
}
}
}
This is currently the solution I'm running, but it would be nice to have a "pure" Eloquent answer for this instead of having to write a custom accessor for each relation of this type.

I think that you can achieve what you want by using Many to Many relationship.
Basically you will need to define write a method that returns the result of the belongsToMany method in both User and Roles models.
It will be something like this:
In User.php
public function roles(){
return $this->belongsToMany('App\Role');
}
In Role.php
public function users(){
return $this->belongsToMany('App\User');
}
Then, you will be able to do something like:
$team = Team::find(1);
foreach($team->users as $user) {
dump($user->roles);
}
For more reference you can see the official documentation: https://laravel.com/docs/6.x/eloquent-relationships#many-to-many
I hope this helps.

Related

Nested field resolvers in GraphQL

The goal is to use NestJS to implement a GraphQL schema using the code-first approach.
Let's say I have a Pet type in my GraphQL schema with two fields, name and age. If those two pieces of information come from different sources of truth (and I don't always want to fetch both), I could implement a PetResolver class with resolvers for each field:
#Resolver(() => Pet)
export class PetResolver {
#Query(() => Pet)
pet(): Pet {
return {};
}
#ResolveField()
name(): Promise<string> {
return Promise.resolve('Spot');
}
#ResolveField(() => Int)
age(): Promise<number> {
return Promise.resolve(2);
}
}
which could be used like this:
query GetPet {
pet {
name
}
}
This works and would ensure that the value of each field is only fetched when requested, but what if I wanted to have a pet field on my User type that I could query like this:
query GetUserWithPet {
currentUser {
email
pet {
name
}
}
}
Applying the same principle, I could create a UserResolver class like this:
#Resolver(() => User)
export class UserResolver {
#Query(() => User)
#UseGuards(AuthCognitoGuard)
currentUser(#CurrentUser() user: IdTokenPayload): User {
return {
id: user.sub,
email: user.email,
};
}
#ResolveField()
pet(#Parent() user: User): Promise<Pet> {
return petService.getPetForUserId(user.id);
}
}
but then the PetService implementation would have to be aware of which fields were requested if it only wanted to fetch relevant data.
A) Is there a way to use PetResolver within UserResolver to make use of the individual field resolution logic?
B) If not, what is the best way to determine which fields were requested in the query using NestJS code-first conventions?
C) Is this the "wrong" way to think about GraphQL queries? Do best practices dictate that I keep the separate resolver and use a query like this:
query GetUserWithPet {
currentUser {
email
}
pet {
name
}
}
User should contain some petIds [array] value (internal, DB stored field/column) ...
... making possible to resolve pets: [Pet] prop/relation - list of Pet ...
... like starshipIDs explained in https://graphql.org/learn/execution/
Notice: pets service is asked about records using pet ids.
... but of course pet can contain some ownerId (only or explicitely visible, DB stored field/column) making possible to resolve owner: User prop [reverse] relation - this way you can:
query PetWithOwner {
pet (id: "someID") {
id
name
owner {
id
email
# more pets?
pets {
id
name
# you can loop it ;)
owner {
id
email
pet.owner field resolver can return only { id: ownerId } object (partial response) ... server will try to resolve 'missing' (required by query) email prop using User (owner is User type) type resolver, passing id as an arg (check/console.log parent and args resolver args). You don't have to do it [the same] 'manually' inside pet.owner field resolver.
Query required fields ...
... [selection set] can be read from info object - 4th resolver arg - read docs/tutorial for details

How to make self resolving array of object types with Prisma and GraphQL

Maybe the title is not accurate but I really don't know how to describe it anymore. I went through multiple documentations and descriptions but still couldn't figure it out.
I want to implement a basic social media like followers/following query on my type User. I am using MySQL and for that I made a separate table called Follow as it's a many-to-many connection.
Here is a pseudo-ish representation of my tables in the database without the unnecessary columns:
Table - User
user_id primary key Int
Table - Follow
follow_er foreign_key -> User(user_id) Int
follow_ed foreign_key -> User(user_id) Int
A user could "act" as a follow_er so I can get the followed people
And a user could be follow_ed, so I can get the followers.
My prisma schema look like this:
model User {
user_id Int #id #default(autoincrement())
following Follow[] #relation("follower")
followed Follow[] #relation("followed")
}
model Follow {
follow_er Int
follower User #relation("follower", fields: [follow_er], references: [user_id])
follow_ed Int
followed User #relation("followed", fields: [follow_ed], references: [user_id])
##id([follow_er, follow_ed])
##map("follow")
}
By implementing this I can get the followers and following object attached to the root query of the user:
const resolvers = {
Query: {
user: async (parent, arg, ctx) => {
const data = await ctx.user.findUnique({
where: {
user_id: arg.id
},
include: {
following: true,
followed:true
}
})
return data
}....
Here is my GraphQL schema I tried to make:
type Query{
user(id: Int!): User
}
type User{
id: ID
following: [User]
followed: [User]
}
So I can get something like:
query {
user(id: $id) {
id
following {
id
}
followed{
id
}
}
}
}
But I couldn't make it work as even if I get the the array of objects of {follow-connections}:
[
{
follow_er:1,
follow_ed:2
},
{
follow_er:1,
follow_ed:3
},
{
follow_er:3,
follow_ed:1
},
]
I can't iterate through the array. As far as I know, I have to pass either the follow_er or follow_ed, which is a user_id to get a User object.
What am I missing? Maybe I try to solve it from a wrong direction. If anybody could help me with this, or just tell me some keywords or concepts I have to look for it would be cool. Thanks!
I would suggest creating self-relations for this structure in the following format:
model User {
id Int #id #default(autoincrement())
name String?
followedBy User[] #relation("UserFollows", references: [id])
following User[] #relation("UserFollows", references: [id])
}
And then querying as follows:
await prisma.user.findUnique({
where: { id: 1 },
include: { followedBy: true, following: true },
})
So you will get a response like this:

Laravel Lighthouse Graphql belongsToMany mutation with pivot values

I have some Laravel models that are related via a pivot table in a belongsToMany relation.
Now I try to create a mutation in Laravel Lighthouse to join the models and also fill the pivot values.
Somehow I cannot find out how to do this.
The Course model looks like this:
class Course extends Model
{
public function programs(): BelongsToMany
{
return $this->belongsToMany(\App\Models\Program::class, 'Course_Program')
->withPivot('id', 'year_id')
->withTimestamps();
}
}
My GraphQL code looks like this:
type Mutation {
createCourse(input: CreateCourseInput! #spread): Course! #create
}
type Course {
id: ID!
name: String!
programs: [ProgramWithPivot!]! #belongsToMany
}
type Program {
id: ID!
name: String!
}
type ProgramWithPivot {
id: ID!
name: String!
year_id: ID!
}
input CreateCourseInput {
name: String!
programs: CreateCourseProgramsRelation!
}
input CreateCourseProgramsRelation {
create: [CreateCourseProgramInput!]
}
input CreateCourseProgramInput {
id: ID!
year_id: ID!
}
The problem is that when I try to create programs in my course like this:
mutation {
createCourse(input: {
name: "new cours"
programs: {
create: [{
id: 1
year_id: 2
}]
}
}) {
id
programs {
id
year_id
}
}
}
Laravel Lighthouse tries to insert data into the Program table (and complains that Program.name does not have a default value).
However, I want to insert data (course_id, year_id and program_id) into the Course_Program table.
How do I tell Laravel Lighthouse to insert data in the pivot-table?
Mutating pivot data is currently not there, but I did a PR for this last week. You can follow the PR progress
In general it will be included either the way I did it, or maybe a little bit different. It was discussed in this issue
For now you can update your pivot data only using custom resolver/directive. But probably the best way will be just to wait till PR gets merged.
There is any way of doing update / upsert to pivot data via nested mutation?

Nested mutations don't seem to work in Lighthouse 3.7

I'm trying to setup a schema in Lighthouse 3.7/Laravel 5.8
What I want to achieve is this:
A User should be able to create Clists.
There is a One to Many relation between User and Clist.
And I'm trying to implement nested mutations as described here.
I have already implemented the "query" part and it works fine.
But when I test a createClist mutation in GraphQL Playground, I get this error:
"debugMessage": "Array to string conversion",
"message": "Internal server error",
"extensions": {
"category": "internal"
},
...
And I can't figure out what I'm doing wrong.
Here is my code:
type Mutation {
createClist(input: CreateClistInput! #spread): Clist #create
}
input CreateClistInput {
name: String!
description: String
starred: Boolean
user: CreateUserRelation!
ctags: CreateCtagRelation
}
input CreateUserRelation {
connect: ID!
}
input CreateCtagRelation {
create: [CreateCtagInput!]
connect: [ID!]
sync: [ID!]
}
input CreateCtagInput {
name: String!
}
And here is a screenshot of GraphQL Playground:
When using the #spread directive a typehint on the relationship in your model is required.
Taken from the docs there is the following example:
use Illuminate\Database\Eloquent\Relations\BelongsTo;
class Post extends Model
{
// WORKS
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
// DOES NOT WORK
public function comments()
{
return $this->hasMany(Comment::class);
}
}
Lighthouse uses the type hint to determine how it should handle the relationship.

Enumerating all fields from a GraphQL query

Given a GraphQL schema and resolvers for Apollo Server, and a GraphQL query, is there a way to create a collection of all requested fields (in an Object or a Map) in the resolver function?
For a simple query, it's easy to recreate this collection from the info argument of the resolver.
Given a schema:
type User {
id: Int!
username: String!
roles: [Role!]!
}
type Role {
id: Int!
name: String!
description: String
}
schema {
query: Query
}
type Query {
getUser(id: Int!): User!
}
and a resolver:
Query: {
getUser: (root, args, context, info) => {
console.log(infoParser(info))
return db.Users.findOne({ id: args.id })
}
}
with a simple recursive infoParser function like this:
function infoParser (info) {
const fields = {}
info.fieldNodes.forEach(node => {
parseSelectionSet(node.selectionSet.selections, fields)
})
return fields
}
function parseSelectionSet (selections, fields) {
selections.forEach(selection => {
const name = selection.name.value
fields[name] = selection.selectionSet
? parseSelectionSet(selection.selectionSet.selections, {})
: true
})
return fields
}
The following query results in this log:
{
getUser(id: 1) {
id
username
roles {
name
}
}
}
=> { id: true, username: true, roles: { name: true } }
Things get pretty ugly pretty soon, for example when you use fragments in the query:
fragment UserInfo on User {
id
username
roles {
name
}
}
{
getUser(id: 1) {
...UserInfo
username
roles {
description
}
}
}
GraphQL engine correctly ignores duplicates, (deeply) merges etc. queried fields on execution, but it is not reflected in the info argument. When you add unions and inline fragments it just gets hairier.
Is there a way to construct a collection of all fields requested in a query, taking in account advanced querying capabilities of GraphQL?
Info about the info argument can be found on the Apollo docs site and in the graphql-js Github repo.
I know it has been a while but in case anyone ends up here, there is an npm package called graphql-list-fields by Jake Pusareti that does this. It handles fragments and skip and include directives.
you can also check the code here.

Resources