From 7d2010eba71e8a80051e750a66dbd8937d40b7e0 Mon Sep 17 00:00:00 2001 From: gilex-dev Date: Sat, 18 Nov 2023 15:51:10 +0100 Subject: [PATCH] rename user role operations. add role manager permission. update gopls to v0.14.2 --- database/main.go | 4 +- database/role.go | 43 ++- database/user.go | 46 ++- example.graphql | 48 ++- go.sum | 12 + gqlgen.yaml | 4 + graph/generated.go | 794 ++++++++++++++++++++++++++++++++++---- graph/model/models_gen.go | 29 +- graph/schema.graphqls | 26 +- graph/schema.resolvers.go | 33 +- 10 files changed, 930 insertions(+), 109 deletions(-) diff --git a/database/main.go b/database/main.go index 54a2d4a..c6055b5 100644 --- a/database/main.go +++ b/database/main.go @@ -79,7 +79,7 @@ func (db CustomDB) createSQLite3Tables() error { }{ {"User", "userId INTEGER PRIMARY KEY NOT NULL, userName VARCHAR NOT NULL UNIQUE CHECK(length(userName)!=0), passwordHash VARCHAR NOT NULL CHECK(length(passwordHash)!=0), fullName VARCHAR CHECK(length(fullName)!=0)"}, {"Todo", "todoId INTEGER PRIMARY KEY NOT NULL, text VARCHAR NOT NULL CHECK(length(text)!=0), IS_done BOOL NOT NULL DEFAULT false, FK_User_userId INTEGER NOT NULL, FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"}, - {"R_User_Role", "relationId INTEGER PRIMARY KEY NOT NULL, FK_Role_roleId INTEGER NOT NULL, FK_User_userId INTEGER NOT NULL, UNIQUE(FK_Role_roleId, FK_User_userId), FOREIGN KEY(FK_Role_roleId) REFERENCES Role(roleId) ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"}, + {"R_User_Role", "relationId INTEGER PRIMARY KEY NOT NULL, FK_Role_roleId INTEGER NOT NULL, FK_User_userId INTEGER NOT NULL, IS_roleManager BOOL NOT NULL, UNIQUE(FK_Role_roleId, FK_User_userId), FOREIGN KEY(FK_Role_roleId) REFERENCES Role(roleId) ON UPDATE CASCADE ON DELETE CASCADE, FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"}, {"Role", "roleId INTEGER PRIMARY KEY NOT NULL, roleName VARCHAR NOT NULL UNIQUE CHECK(length(roleName)!=0), IS_admin BOOL NOT NULL, IS_userCreator BOOL NOT NULL"}, {"RefreshToken", "tokenId INTEGER PRIMARY KEY NOT NULL, FK_User_userId INTEGER NOT NULL, selector VARCHAR NOT NULL CHECK(length(selector)!=0) UNIQUE, tokenHash VARCHAR NOT NULL CHECK(length(tokenHash)!=0), expiryDate INTEGER NOT NULL, tokenName VARCHAR CHECK(length(tokenName)!=0), FOREIGN KEY(FK_User_userId) REFERENCES User(userId) ON UPDATE CASCADE ON DELETE CASCADE"}, } @@ -109,7 +109,7 @@ func (db CustomDB) CreateInitialAdmin(initialAdminName string, initialAdminPassw if err != nil { return err } - _, err = db.AddRole(user.ID, role.ID) + _, err = db.AddUserRole(user.ID, role.ID, true) if err != nil { return err } diff --git a/database/role.go b/database/role.go index 02f2580..1b6f0a0 100644 --- a/database/role.go +++ b/database/role.go @@ -68,12 +68,12 @@ func (db CustomDB) GetRole(role *model.Role) (*model.Role, error) { return role, nil } -func (db CustomDB) GetRolesFrom(userId string) ([]*model.Role, error) { +func (db CustomDB) GetRolesFrom(userId string) ([]*model.RelationUserRole, error) { numUserId, err := strconv.Atoi(userId) if err != nil { return nil, errors.New("invalid userId") } - statement, err := db.connection.Prepare("SELECT Role.roleId, Role.roleName, Role.IS_admin, Role.IS_userCreator FROM Role INNER JOIN R_User_Role ON R_User_Role.FK_Role_roleId = Role.roleId WHERE R_User_Role.FK_User_userId = ?") + statement, err := db.connection.Prepare("SELECT Role.roleId, Role.roleName, Role.IS_admin, Role.IS_userCreator, R_User_Role.IS_roleManager FROM Role INNER JOIN R_User_Role ON R_User_Role.FK_Role_roleId = Role.roleId WHERE R_User_Role.FK_User_userId = ?") if err != nil { return nil, err } @@ -85,13 +85,46 @@ func (db CustomDB) GetRolesFrom(userId string) ([]*model.Role, error) { defer rows.Close() - var all []*model.Role + var all []*model.RelationUserRole for rows.Next() { role := model.Role{} - if err := rows.Scan(&role.ID, &role.RoleName, &role.IsAdmin, &role.IsUserCreator); err != nil { + relation := model.RelationUserRole{Role: &role} + if err := rows.Scan(&role.ID, &role.RoleName, &role.IsAdmin, &role.IsUserCreator, &relation.UserIsRoleManager); err != nil { return nil, err } - all = append(all, &role) + all = append(all, &relation) + } + return all, nil +} + +func (db CustomDB) GetRoleMembers(roleId string) ([]*model.RelationRoleUser, error) { + numRoleId, err := strconv.Atoi(roleId) + if err != nil { + return nil, errors.New("invalid roleId") + } + statement, err := db.connection.Prepare("SELECT FK_User_userId, IS_roleManager FROM R_User_Role WHERE FK_Role_roleId = ?") + if err != nil { + return nil, err + } + + rows, err := statement.Query(numRoleId) + if err != nil { + return nil, err + } + + defer rows.Close() + + var all []*model.RelationRoleUser + for rows.Next() { + relation := model.RelationRoleUser{User: &model.User{}} + if err := rows.Scan(&relation.User.ID, &relation.UserIsRoleManager); err != nil { + return nil, err + } + relation.User, err = db.GetUser(relation.User) + if err != nil { + return nil, err + } + all = append(all, &relation) } return all, nil } diff --git a/database/user.go b/database/user.go index e070b92..b1f72f4 100644 --- a/database/user.go +++ b/database/user.go @@ -185,7 +185,7 @@ func (db CustomDB) DeleteUser(userId string) (*string, error) { return &userId, nil } -func (db CustomDB) AddRole(userId string, roleId string) (relationId string, err error) { +func (db CustomDB) AddUserRole(userId string, roleId string, isRoleManager bool) (relationId string, err error) { encUserId, err := strconv.Atoi(userId) if err != nil { return "", errors.New("invalid userId") @@ -195,12 +195,12 @@ func (db CustomDB) AddRole(userId string, roleId string) (relationId string, err return "", errors.New("invalid roleId") } - statement, err := db.connection.Prepare("INSERT INTO R_User_Role (FK_User_userId, FK_Role_roleId) VALUES (?, ?)") + statement, err := db.connection.Prepare("INSERT INTO R_User_Role (FK_User_userId, FK_Role_roleId, IS_roleManager) VALUES (?, ?, ?)") if err != nil { return "", err } - rows, err := statement.Exec(encUserId, encRoleId) + rows, err := statement.Exec(encUserId, encRoleId, isRoleManager) if err != nil { return "", err } @@ -223,7 +223,45 @@ func (db CustomDB) AddRole(userId string, roleId string) (relationId string, err return strconv.FormatInt(insertId, 10), nil } -func (db CustomDB) RemoveRole(userId string, roleId string) (relationId string, err error) { +func (db CustomDB) UpdateUserRole(userId string, roleId string, isRoleManager bool) (relationId string, err error) { + encUserId, err := strconv.Atoi(userId) + if err != nil { + return "", errors.New("invalid userId") + } + encRoleId, err := strconv.Atoi(roleId) + if err != nil { + return "", errors.New("invalid roleId") + } + + statement, err := db.connection.Prepare("UPDATE R_User_Role SET IS_roleManager = ? WHERE FK_User_userId = ? AND FK_Role_roleId = ?") + if err != nil { + return "", err + } + + rows, err := statement.Exec(isRoleManager, encUserId, encRoleId) + if err != nil { + return "", err + } + + num, err := rows.RowsAffected() + if err != nil { + return "", err + } + + if num < 1 { + return "", errors.New("no rows affected") + } + + insertId, err := rows.LastInsertId() + if err != nil { + return "", err + } + + RevokeAccessToken(&AccessToken{UserId: userId, ExpiryDate: int(time.Now().Add(accessTokenLifetime).Unix())}) + return strconv.FormatInt(insertId, 10), nil +} + +func (db CustomDB) RemoveUserRole(userId string, roleId string) (relationId string, err error) { encUserId, err := strconv.Atoi(userId) if err != nil { return "", errors.New("invalid userId") diff --git a/example.graphql b/example.graphql index c9fc075..2453509 100644 --- a/example.graphql +++ b/example.graphql @@ -11,10 +11,13 @@ query getUser { done } roles { - id - roleName - isAdmin - isUserCreator + role { + id + roleName + isAdmin + isUserCreator + } + userIsRoleManager } } } @@ -113,6 +116,11 @@ query getRoles { roles { id roleName + roleMembers { + user { + id + } + } } } @@ -120,6 +128,11 @@ query getRole { role(id: 2) { id roleName + roleMembers { + user { + id + } + } } } @@ -135,14 +148,29 @@ mutation deleteRole { deleteRole(id: 2) } -mutation addRole { - addRole(userId: 1, roleId: 2) { - roleName +mutation addUserRole { + addUserRole(userId: 1, roleId: 2, userIsRoleManager: false) { + role { + roleName + } + userIsRoleManager } } -mutation removeRole { - removeRole(userId: 2, roleId: 1) { - roleName +mutation updateUserRole { + UpdateUserRole(userId: 1, roleId: 2, userIsRoleManager: true) { + role { + roleName + } + userIsRoleManager + } +} + +mutation removeUserRole { + removeUserRole(userId: 2, roleId: 1) { + role { + roleName + } + userIsRoleManager } } diff --git a/go.sum b/go.sum index a468f38..8332c74 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,7 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym github.com/agnivade/levenshtein v1.1.1 h1:QY8M92nrzkmr798gCo3kmMyqXFzdQVpxLlGPRBij0P8= github.com/agnivade/levenshtein v1.1.1/go.mod h1:veldBMzWxcCG2ZvUTKD2kJNRdCk5hVbJomOvKkmgYbo= github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ= +github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0 h1:jfIu9sQUG6Ig+0+Ap1h4unLjW6YQJpKZVmUzxsD4E/Q= github.com/arbovm/levenshtein v0.0.0-20160628152529-48b4e1c0c4d0/go.mod h1:t2tdKJDJF9BV14lnkjHmOQgcvEKgtqs5a1N3LNdJhGE= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= @@ -58,6 +59,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= +github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48 h1:fRzb/w+pyskVMQ+UbP35JkH8yB7MYb4q/qhBarqZE6g= github.com/dgryski/trifles v0.0.0-20200323201526-dd97f9abfb48/go.mod h1:if7Fbed8SFyPtHLHbg49SI7NAdJiC5WIA09pe59rfAA= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -67,6 +69,7 @@ github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5y github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk= github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY= +github.com/frankban/quicktest v1.14.4/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0= github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA= github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM= github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE= @@ -111,6 +114,7 @@ github.com/google/go-cmp v0.5.1/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= +github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= github.com/google/martian/v3 v3.1.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -149,9 +153,11 @@ github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+o github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY= github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-sqlite3 v1.14.18 h1:JL0eqdCOq6DJVNPSvArO/bIV9/P7fbGrV00LZHc+5aI= @@ -164,9 +170,11 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= +github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sagikazarmark/locafero v0.3.0 h1:zT7VEGWC2DTflmccN/5T1etyKvxSxpHsjb9cJvm4SvQ= @@ -174,6 +182,7 @@ github.com/sagikazarmark/locafero v0.3.0/go.mod h1:w+v7UsPNFwzF1cHuOajOOzoq4U7v/ github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= +github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= github.com/sosodev/duration v1.2.0 h1:pqK/FLSjsAADWY74SyWDCjOcd5l7H8GSnnOGEB9A1Us= github.com/sosodev/duration v1.2.0/go.mod h1:RQIBBX0+fMLc/D9+Jb/fwvVmo0eZvDDEERAikUR6SDg= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= @@ -318,6 +327,7 @@ golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -513,11 +523,13 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/gqlgen.yaml b/gqlgen.yaml index 4b077a1..856a635 100644 --- a/gqlgen.yaml +++ b/gqlgen.yaml @@ -110,3 +110,7 @@ models: resolver: true roles: resolver: true + Role: + fields: + roleMembers: + resolver: true diff --git a/graph/generated.go b/graph/generated.go index bbc3c6f..042fe29 100644 --- a/graph/generated.go +++ b/graph/generated.go @@ -24,6 +24,7 @@ import ( // NewExecutableSchema creates an ExecutableSchema from the ResolverRoot interface. func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { return &executableSchema{ + schema: cfg.Schema, resolvers: cfg.Resolvers, directives: cfg.Directives, complexity: cfg.Complexity, @@ -31,6 +32,7 @@ func NewExecutableSchema(cfg Config) graphql.ExecutableSchema { } type Config struct { + Schema *ast.Schema Resolvers ResolverRoot Directives DirectiveRoot Complexity ComplexityRoot @@ -39,6 +41,7 @@ type Config struct { type ResolverRoot interface { Mutation() MutationResolver Query() QueryResolver + Role() RoleResolver Todo() TodoResolver User() UserResolver } @@ -48,7 +51,7 @@ type DirectiveRoot struct { type ComplexityRoot struct { Mutation struct { - AddRole func(childComplexity int, userID string, roleID string) int + AddUserRole func(childComplexity int, userID string, roleID string, userIsRoleManager bool) int CreateRefreshToken func(childComplexity int, input model.NewRefreshToken) int CreateRole func(childComplexity int, input model.NewRole) int CreateTodo func(childComplexity int, input model.NewTodo) int @@ -57,11 +60,12 @@ type ComplexityRoot struct { DeleteRole func(childComplexity int, id string) int DeleteTodo func(childComplexity int, id string) int DeleteUser func(childComplexity int, id string) int - RemoveRole func(childComplexity int, userID string, roleID string) int + RemoveUserRole func(childComplexity int, userID string, roleID string) int UpdateRefreshToken func(childComplexity int, id string, changes model.UpdateRefreshToken) int UpdateRole func(childComplexity int, id string, changes model.UpdateRole) int UpdateTodo func(childComplexity int, id string, changes model.UpdateTodo) int UpdateUser func(childComplexity int, id string, changes model.UpdateUser) int + UpdateUserRole func(childComplexity int, userID string, roleID string, userIsRoleManager bool) int } Query struct { @@ -84,10 +88,21 @@ type ComplexityRoot struct { UserID func(childComplexity int) int } + RelationRoleUser struct { + User func(childComplexity int) int + UserIsRoleManager func(childComplexity int) int + } + + RelationUserRole struct { + Role func(childComplexity int) int + UserIsRoleManager func(childComplexity int) int + } + Role struct { ID func(childComplexity int) int IsAdmin func(childComplexity int) int IsUserCreator func(childComplexity int) int + RoleMembers func(childComplexity int) int RoleName func(childComplexity int) int } @@ -120,8 +135,9 @@ type MutationResolver interface { DeleteTodo(ctx context.Context, id string) (*string, error) DeleteRole(ctx context.Context, id string) (*string, error) DeleteRefreshToken(ctx context.Context, id string) (*string, error) - AddRole(ctx context.Context, userID string, roleID string) ([]*model.Role, error) - RemoveRole(ctx context.Context, userID string, roleID string) ([]*model.Role, error) + AddUserRole(ctx context.Context, userID string, roleID string, userIsRoleManager bool) ([]*model.RelationUserRole, error) + UpdateUserRole(ctx context.Context, userID string, roleID string, userIsRoleManager bool) ([]*model.RelationUserRole, error) + RemoveUserRole(ctx context.Context, userID string, roleID string) ([]*model.RelationUserRole, error) } type QueryResolver interface { Todos(ctx context.Context) ([]*model.Todo, error) @@ -133,21 +149,28 @@ type QueryResolver interface { Role(ctx context.Context, id string) (*model.Role, error) RefreshToken(ctx context.Context, id string) (*model.RefreshToken, error) } +type RoleResolver interface { + RoleMembers(ctx context.Context, obj *model.Role) ([]*model.RelationRoleUser, error) +} type TodoResolver interface { User(ctx context.Context, obj *model.Todo) (*model.User, error) } type UserResolver interface { Todos(ctx context.Context, obj *model.User) ([]*model.Todo, error) - Roles(ctx context.Context, obj *model.User) ([]*model.Role, error) + Roles(ctx context.Context, obj *model.User) ([]*model.RelationUserRole, error) } type executableSchema struct { + schema *ast.Schema resolvers ResolverRoot directives DirectiveRoot complexity ComplexityRoot } func (e *executableSchema) Schema() *ast.Schema { + if e.schema != nil { + return e.schema + } return parsedSchema } @@ -156,17 +179,17 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in _ = ec switch typeName + "." + field { - case "Mutation.addRole": - if e.complexity.Mutation.AddRole == nil { + case "Mutation.addUserRole": + if e.complexity.Mutation.AddUserRole == nil { break } - args, err := ec.field_Mutation_addRole_args(context.TODO(), rawArgs) + args, err := ec.field_Mutation_addUserRole_args(context.TODO(), rawArgs) if err != nil { return 0, false } - return e.complexity.Mutation.AddRole(childComplexity, args["userId"].(string), args["roleId"].(string)), true + return e.complexity.Mutation.AddUserRole(childComplexity, args["userId"].(string), args["roleId"].(string), args["userIsRoleManager"].(bool)), true case "Mutation.createRefreshToken": if e.complexity.Mutation.CreateRefreshToken == nil { @@ -264,17 +287,17 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Mutation.DeleteUser(childComplexity, args["id"].(string)), true - case "Mutation.removeRole": - if e.complexity.Mutation.RemoveRole == nil { + case "Mutation.removeUserRole": + if e.complexity.Mutation.RemoveUserRole == nil { break } - args, err := ec.field_Mutation_removeRole_args(context.TODO(), rawArgs) + args, err := ec.field_Mutation_removeUserRole_args(context.TODO(), rawArgs) if err != nil { return 0, false } - return e.complexity.Mutation.RemoveRole(childComplexity, args["userId"].(string), args["roleId"].(string)), true + return e.complexity.Mutation.RemoveUserRole(childComplexity, args["userId"].(string), args["roleId"].(string)), true case "Mutation.updateRefreshToken": if e.complexity.Mutation.UpdateRefreshToken == nil { @@ -324,6 +347,18 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Mutation.UpdateUser(childComplexity, args["id"].(string), args["changes"].(model.UpdateUser)), true + case "Mutation.UpdateUserRole": + if e.complexity.Mutation.UpdateUserRole == nil { + break + } + + args, err := ec.field_Mutation_UpdateUserRole_args(context.TODO(), rawArgs) + if err != nil { + return 0, false + } + + return e.complexity.Mutation.UpdateUserRole(childComplexity, args["userId"].(string), args["roleId"].(string), args["userIsRoleManager"].(bool)), true + case "Query.refreshToken": if e.complexity.Query.RefreshToken == nil { break @@ -442,6 +477,34 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.RefreshToken.UserID(childComplexity), true + case "RelationRoleUser.user": + if e.complexity.RelationRoleUser.User == nil { + break + } + + return e.complexity.RelationRoleUser.User(childComplexity), true + + case "RelationRoleUser.userIsRoleManager": + if e.complexity.RelationRoleUser.UserIsRoleManager == nil { + break + } + + return e.complexity.RelationRoleUser.UserIsRoleManager(childComplexity), true + + case "RelationUserRole.role": + if e.complexity.RelationUserRole.Role == nil { + break + } + + return e.complexity.RelationUserRole.Role(childComplexity), true + + case "RelationUserRole.userIsRoleManager": + if e.complexity.RelationUserRole.UserIsRoleManager == nil { + break + } + + return e.complexity.RelationUserRole.UserIsRoleManager(childComplexity), true + case "Role.id": if e.complexity.Role.ID == nil { break @@ -463,6 +526,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in return e.complexity.Role.IsUserCreator(childComplexity), true + case "Role.roleMembers": + if e.complexity.Role.RoleMembers == nil { + break + } + + return e.complexity.Role.RoleMembers(childComplexity), true + case "Role.roleName": if e.complexity.Role.RoleName == nil { break @@ -635,14 +705,14 @@ func (ec *executionContext) introspectSchema() (*introspection.Schema, error) { if ec.DisableIntrospection { return nil, errors.New("introspection disabled") } - return introspection.WrapSchema(parsedSchema), nil + return introspection.WrapSchema(ec.Schema()), nil } func (ec *executionContext) introspectType(name string) (*introspection.Type, error) { if ec.DisableIntrospection { return nil, errors.New("introspection disabled") } - return introspection.WrapTypeFromDef(parsedSchema, parsedSchema.Types[name]), nil + return introspection.WrapTypeFromDef(ec.Schema(), ec.Schema().Types[name]), nil } //go:embed "schema.graphqls" @@ -665,7 +735,7 @@ var parsedSchema = gqlparser.MustLoadSchema(sources...) // region ***************************** args.gotpl ***************************** -func (ec *executionContext) field_Mutation_addRole_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { +func (ec *executionContext) field_Mutation_UpdateUserRole_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { var err error args := map[string]interface{}{} var arg0 string @@ -686,6 +756,48 @@ func (ec *executionContext) field_Mutation_addRole_args(ctx context.Context, raw } } args["roleId"] = arg1 + var arg2 bool + if tmp, ok := rawArgs["userIsRoleManager"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("userIsRoleManager")) + arg2, err = ec.unmarshalNBoolean2bool(ctx, tmp) + if err != nil { + return nil, err + } + } + args["userIsRoleManager"] = arg2 + return args, nil +} + +func (ec *executionContext) field_Mutation_addUserRole_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { + var err error + args := map[string]interface{}{} + var arg0 string + if tmp, ok := rawArgs["userId"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("userId")) + arg0, err = ec.unmarshalNID2string(ctx, tmp) + if err != nil { + return nil, err + } + } + args["userId"] = arg0 + var arg1 string + if tmp, ok := rawArgs["roleId"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("roleId")) + arg1, err = ec.unmarshalNID2string(ctx, tmp) + if err != nil { + return nil, err + } + } + args["roleId"] = arg1 + var arg2 bool + if tmp, ok := rawArgs["userIsRoleManager"]; ok { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("userIsRoleManager")) + arg2, err = ec.unmarshalNBoolean2bool(ctx, tmp) + if err != nil { + return nil, err + } + } + args["userIsRoleManager"] = arg2 return args, nil } @@ -809,7 +921,7 @@ func (ec *executionContext) field_Mutation_deleteUser_args(ctx context.Context, return args, nil } -func (ec *executionContext) field_Mutation_removeRole_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { +func (ec *executionContext) field_Mutation_removeUserRole_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) { var err error args := map[string]interface{}{} var arg0 string @@ -1221,6 +1333,8 @@ func (ec *executionContext) fieldContext_Mutation_createRole(ctx context.Context return ec.fieldContext_Role_isAdmin(ctx, field) case "isUserCreator": return ec.fieldContext_Role_isUserCreator(ctx, field) + case "roleMembers": + return ec.fieldContext_Role_roleMembers(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type Role", field.Name) }, @@ -1487,6 +1601,8 @@ func (ec *executionContext) fieldContext_Mutation_updateRole(ctx context.Context return ec.fieldContext_Role_isAdmin(ctx, field) case "isUserCreator": return ec.fieldContext_Role_isUserCreator(ctx, field) + case "roleMembers": + return ec.fieldContext_Role_roleMembers(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type Role", field.Name) }, @@ -1782,8 +1898,8 @@ func (ec *executionContext) fieldContext_Mutation_deleteRefreshToken(ctx context return fc, nil } -func (ec *executionContext) _Mutation_addRole(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { - fc, err := ec.fieldContext_Mutation_addRole(ctx, field) +func (ec *executionContext) _Mutation_addUserRole(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Mutation_addUserRole(ctx, field) if err != nil { return graphql.Null } @@ -1796,7 +1912,7 @@ func (ec *executionContext) _Mutation_addRole(ctx context.Context, field graphql }() resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Mutation().AddRole(rctx, fc.Args["userId"].(string), fc.Args["roleId"].(string)) + return ec.resolvers.Mutation().AddUserRole(rctx, fc.Args["userId"].(string), fc.Args["roleId"].(string), fc.Args["userIsRoleManager"].(bool)) }) if err != nil { ec.Error(ctx, err) @@ -1808,12 +1924,12 @@ func (ec *executionContext) _Mutation_addRole(ctx context.Context, field graphql } return graphql.Null } - res := resTmp.([]*model.Role) + res := resTmp.([]*model.RelationUserRole) fc.Result = res - return ec.marshalNRole2ᚕᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRoleᚄ(ctx, field.Selections, res) + return ec.marshalNRelationUserRole2ᚕᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRelationUserRoleᚄ(ctx, field.Selections, res) } -func (ec *executionContext) fieldContext_Mutation_addRole(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { +func (ec *executionContext) fieldContext_Mutation_addUserRole(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { fc = &graphql.FieldContext{ Object: "Mutation", Field: field, @@ -1821,16 +1937,12 @@ func (ec *executionContext) fieldContext_Mutation_addRole(ctx context.Context, f IsResolver: true, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { switch field.Name { - case "id": - return ec.fieldContext_Role_id(ctx, field) - case "roleName": - return ec.fieldContext_Role_roleName(ctx, field) - case "isAdmin": - return ec.fieldContext_Role_isAdmin(ctx, field) - case "isUserCreator": - return ec.fieldContext_Role_isUserCreator(ctx, field) + case "role": + return ec.fieldContext_RelationUserRole_role(ctx, field) + case "userIsRoleManager": + return ec.fieldContext_RelationUserRole_userIsRoleManager(ctx, field) } - return nil, fmt.Errorf("no field named %q was found under type Role", field.Name) + return nil, fmt.Errorf("no field named %q was found under type RelationUserRole", field.Name) }, } defer func() { @@ -1840,15 +1952,15 @@ func (ec *executionContext) fieldContext_Mutation_addRole(ctx context.Context, f } }() ctx = graphql.WithFieldContext(ctx, fc) - if fc.Args, err = ec.field_Mutation_addRole_args(ctx, field.ArgumentMap(ec.Variables)); err != nil { + if fc.Args, err = ec.field_Mutation_addUserRole_args(ctx, field.ArgumentMap(ec.Variables)); err != nil { ec.Error(ctx, err) return fc, err } return fc, nil } -func (ec *executionContext) _Mutation_removeRole(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { - fc, err := ec.fieldContext_Mutation_removeRole(ctx, field) +func (ec *executionContext) _Mutation_UpdateUserRole(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Mutation_UpdateUserRole(ctx, field) if err != nil { return graphql.Null } @@ -1861,7 +1973,7 @@ func (ec *executionContext) _Mutation_removeRole(ctx context.Context, field grap }() resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { ctx = rctx // use context from middleware stack in children - return ec.resolvers.Mutation().RemoveRole(rctx, fc.Args["userId"].(string), fc.Args["roleId"].(string)) + return ec.resolvers.Mutation().UpdateUserRole(rctx, fc.Args["userId"].(string), fc.Args["roleId"].(string), fc.Args["userIsRoleManager"].(bool)) }) if err != nil { ec.Error(ctx, err) @@ -1873,12 +1985,12 @@ func (ec *executionContext) _Mutation_removeRole(ctx context.Context, field grap } return graphql.Null } - res := resTmp.([]*model.Role) + res := resTmp.([]*model.RelationUserRole) fc.Result = res - return ec.marshalNRole2ᚕᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRoleᚄ(ctx, field.Selections, res) + return ec.marshalNRelationUserRole2ᚕᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRelationUserRoleᚄ(ctx, field.Selections, res) } -func (ec *executionContext) fieldContext_Mutation_removeRole(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { +func (ec *executionContext) fieldContext_Mutation_UpdateUserRole(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { fc = &graphql.FieldContext{ Object: "Mutation", Field: field, @@ -1886,16 +1998,12 @@ func (ec *executionContext) fieldContext_Mutation_removeRole(ctx context.Context IsResolver: true, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { switch field.Name { - case "id": - return ec.fieldContext_Role_id(ctx, field) - case "roleName": - return ec.fieldContext_Role_roleName(ctx, field) - case "isAdmin": - return ec.fieldContext_Role_isAdmin(ctx, field) - case "isUserCreator": - return ec.fieldContext_Role_isUserCreator(ctx, field) + case "role": + return ec.fieldContext_RelationUserRole_role(ctx, field) + case "userIsRoleManager": + return ec.fieldContext_RelationUserRole_userIsRoleManager(ctx, field) } - return nil, fmt.Errorf("no field named %q was found under type Role", field.Name) + return nil, fmt.Errorf("no field named %q was found under type RelationUserRole", field.Name) }, } defer func() { @@ -1905,7 +2013,68 @@ func (ec *executionContext) fieldContext_Mutation_removeRole(ctx context.Context } }() ctx = graphql.WithFieldContext(ctx, fc) - if fc.Args, err = ec.field_Mutation_removeRole_args(ctx, field.ArgumentMap(ec.Variables)); err != nil { + if fc.Args, err = ec.field_Mutation_UpdateUserRole_args(ctx, field.ArgumentMap(ec.Variables)); err != nil { + ec.Error(ctx, err) + return fc, err + } + return fc, nil +} + +func (ec *executionContext) _Mutation_removeUserRole(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Mutation_removeUserRole(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return ec.resolvers.Mutation().RemoveUserRole(rctx, fc.Args["userId"].(string), fc.Args["roleId"].(string)) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.([]*model.RelationUserRole) + fc.Result = res + return ec.marshalNRelationUserRole2ᚕᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRelationUserRoleᚄ(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Mutation_removeUserRole(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Mutation", + Field: field, + IsMethod: true, + IsResolver: true, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "role": + return ec.fieldContext_RelationUserRole_role(ctx, field) + case "userIsRoleManager": + return ec.fieldContext_RelationUserRole_userIsRoleManager(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type RelationUserRole", field.Name) + }, + } + defer func() { + if r := recover(); r != nil { + err = ec.Recover(ctx, r) + ec.Error(ctx, err) + } + }() + ctx = graphql.WithFieldContext(ctx, fc) + if fc.Args, err = ec.field_Mutation_removeUserRole_args(ctx, field.ArgumentMap(ec.Variables)); err != nil { ec.Error(ctx, err) return fc, err } @@ -2069,6 +2238,8 @@ func (ec *executionContext) fieldContext_Query_roles(ctx context.Context, field return ec.fieldContext_Role_isAdmin(ctx, field) case "isUserCreator": return ec.fieldContext_Role_isUserCreator(ctx, field) + case "roleMembers": + return ec.fieldContext_Role_roleMembers(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type Role", field.Name) }, @@ -2313,6 +2484,8 @@ func (ec *executionContext) fieldContext_Query_role(ctx context.Context, field g return ec.fieldContext_Role_isAdmin(ctx, field) case "isUserCreator": return ec.fieldContext_Role_isUserCreator(ctx, field) + case "roleMembers": + return ec.fieldContext_Role_roleMembers(ctx, field) } return nil, fmt.Errorf("no field named %q was found under type Role", field.Name) }, @@ -2784,6 +2957,206 @@ func (ec *executionContext) fieldContext_RefreshToken_userId(ctx context.Context return fc, nil } +func (ec *executionContext) _RelationRoleUser_user(ctx context.Context, field graphql.CollectedField, obj *model.RelationRoleUser) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_RelationRoleUser_user(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.User, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(*model.User) + fc.Result = res + return ec.marshalNUser2ᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐUser(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_RelationRoleUser_user(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "RelationRoleUser", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "id": + return ec.fieldContext_User_id(ctx, field) + case "userName": + return ec.fieldContext_User_userName(ctx, field) + case "fullName": + return ec.fieldContext_User_fullName(ctx, field) + case "todos": + return ec.fieldContext_User_todos(ctx, field) + case "roles": + return ec.fieldContext_User_roles(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type User", field.Name) + }, + } + return fc, nil +} + +func (ec *executionContext) _RelationRoleUser_userIsRoleManager(ctx context.Context, field graphql.CollectedField, obj *model.RelationRoleUser) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_RelationRoleUser_userIsRoleManager(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.UserIsRoleManager, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(bool) + fc.Result = res + return ec.marshalNBoolean2bool(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_RelationRoleUser_userIsRoleManager(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "RelationRoleUser", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Boolean does not have child fields") + }, + } + return fc, nil +} + +func (ec *executionContext) _RelationUserRole_role(ctx context.Context, field graphql.CollectedField, obj *model.RelationUserRole) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_RelationUserRole_role(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.Role, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(*model.Role) + fc.Result = res + return ec.marshalNRole2ᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRole(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_RelationUserRole_role(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "RelationUserRole", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "id": + return ec.fieldContext_Role_id(ctx, field) + case "roleName": + return ec.fieldContext_Role_roleName(ctx, field) + case "isAdmin": + return ec.fieldContext_Role_isAdmin(ctx, field) + case "isUserCreator": + return ec.fieldContext_Role_isUserCreator(ctx, field) + case "roleMembers": + return ec.fieldContext_Role_roleMembers(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type Role", field.Name) + }, + } + return fc, nil +} + +func (ec *executionContext) _RelationUserRole_userIsRoleManager(ctx context.Context, field graphql.CollectedField, obj *model.RelationUserRole) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_RelationUserRole_userIsRoleManager(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return obj.UserIsRoleManager, nil + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.(bool) + fc.Result = res + return ec.marshalNBoolean2bool(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_RelationUserRole_userIsRoleManager(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "RelationUserRole", + Field: field, + IsMethod: false, + IsResolver: false, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + return nil, errors.New("field of type Boolean does not have child fields") + }, + } + return fc, nil +} + func (ec *executionContext) _Role_id(ctx context.Context, field graphql.CollectedField, obj *model.Role) (ret graphql.Marshaler) { fc, err := ec.fieldContext_Role_id(ctx, field) if err != nil { @@ -2960,6 +3333,56 @@ func (ec *executionContext) fieldContext_Role_isUserCreator(ctx context.Context, return fc, nil } +func (ec *executionContext) _Role_roleMembers(ctx context.Context, field graphql.CollectedField, obj *model.Role) (ret graphql.Marshaler) { + fc, err := ec.fieldContext_Role_roleMembers(ctx, field) + if err != nil { + return graphql.Null + } + ctx = graphql.WithFieldContext(ctx, fc) + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = graphql.Null + } + }() + resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) { + ctx = rctx // use context from middleware stack in children + return ec.resolvers.Role().RoleMembers(rctx, obj) + }) + if err != nil { + ec.Error(ctx, err) + return graphql.Null + } + if resTmp == nil { + if !graphql.HasFieldError(ctx, fc) { + ec.Errorf(ctx, "must not be null") + } + return graphql.Null + } + res := resTmp.([]*model.RelationRoleUser) + fc.Result = res + return ec.marshalNRelationRoleUser2ᚕᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRelationRoleUserᚄ(ctx, field.Selections, res) +} + +func (ec *executionContext) fieldContext_Role_roleMembers(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { + fc = &graphql.FieldContext{ + Object: "Role", + Field: field, + IsMethod: true, + IsResolver: true, + Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { + switch field.Name { + case "user": + return ec.fieldContext_RelationRoleUser_user(ctx, field) + case "userIsRoleManager": + return ec.fieldContext_RelationRoleUser_userIsRoleManager(ctx, field) + } + return nil, fmt.Errorf("no field named %q was found under type RelationRoleUser", field.Name) + }, + } + return fc, nil +} + func (ec *executionContext) _Todo_id(ctx context.Context, field graphql.CollectedField, obj *model.Todo) (ret graphql.Marshaler) { fc, err := ec.fieldContext_Todo_id(ctx, field) if err != nil { @@ -3357,9 +3780,9 @@ func (ec *executionContext) _User_roles(ctx context.Context, field graphql.Colle } return graphql.Null } - res := resTmp.([]*model.Role) + res := resTmp.([]*model.RelationUserRole) fc.Result = res - return ec.marshalNRole2ᚕᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRoleᚄ(ctx, field.Selections, res) + return ec.marshalNRelationUserRole2ᚕᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRelationUserRoleᚄ(ctx, field.Selections, res) } func (ec *executionContext) fieldContext_User_roles(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) { @@ -3370,16 +3793,12 @@ func (ec *executionContext) fieldContext_User_roles(ctx context.Context, field g IsResolver: true, Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) { switch field.Name { - case "id": - return ec.fieldContext_Role_id(ctx, field) - case "roleName": - return ec.fieldContext_Role_roleName(ctx, field) - case "isAdmin": - return ec.fieldContext_Role_isAdmin(ctx, field) - case "isUserCreator": - return ec.fieldContext_Role_isUserCreator(ctx, field) + case "role": + return ec.fieldContext_RelationUserRole_role(ctx, field) + case "userIsRoleManager": + return ec.fieldContext_RelationUserRole_userIsRoleManager(ctx, field) } - return nil, fmt.Errorf("no field named %q was found under type Role", field.Name) + return nil, fmt.Errorf("no field named %q was found under type RelationUserRole", field.Name) }, } return fc, nil @@ -5579,16 +5998,23 @@ func (ec *executionContext) _Mutation(ctx context.Context, sel ast.SelectionSet) out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) { return ec._Mutation_deleteRefreshToken(ctx, field) }) - case "addRole": + case "addUserRole": out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) { - return ec._Mutation_addRole(ctx, field) + return ec._Mutation_addUserRole(ctx, field) }) if out.Values[i] == graphql.Null { out.Invalids++ } - case "removeRole": + case "UpdateUserRole": out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) { - return ec._Mutation_removeRole(ctx, field) + return ec._Mutation_UpdateUserRole(ctx, field) + }) + if out.Values[i] == graphql.Null { + out.Invalids++ + } + case "removeUserRole": + out.Values[i] = ec.OperationContext.RootResolverMiddleware(innerCtx, func(ctx context.Context) (res graphql.Marshaler) { + return ec._Mutation_removeUserRole(ctx, field) }) if out.Values[i] == graphql.Null { out.Invalids++ @@ -5897,6 +6323,94 @@ func (ec *executionContext) _RefreshToken(ctx context.Context, sel ast.Selection return out } +var relationRoleUserImplementors = []string{"RelationRoleUser"} + +func (ec *executionContext) _RelationRoleUser(ctx context.Context, sel ast.SelectionSet, obj *model.RelationRoleUser) graphql.Marshaler { + fields := graphql.CollectFields(ec.OperationContext, sel, relationRoleUserImplementors) + + out := graphql.NewFieldSet(fields) + deferred := make(map[string]*graphql.FieldSet) + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("RelationRoleUser") + case "user": + out.Values[i] = ec._RelationRoleUser_user(ctx, field, obj) + if out.Values[i] == graphql.Null { + out.Invalids++ + } + case "userIsRoleManager": + out.Values[i] = ec._RelationRoleUser_userIsRoleManager(ctx, field, obj) + if out.Values[i] == graphql.Null { + out.Invalids++ + } + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch(ctx) + if out.Invalids > 0 { + return graphql.Null + } + + atomic.AddInt32(&ec.deferred, int32(len(deferred))) + + for label, dfs := range deferred { + ec.processDeferredGroup(graphql.DeferredGroup{ + Label: label, + Path: graphql.GetPath(ctx), + FieldSet: dfs, + Context: ctx, + }) + } + + return out +} + +var relationUserRoleImplementors = []string{"RelationUserRole"} + +func (ec *executionContext) _RelationUserRole(ctx context.Context, sel ast.SelectionSet, obj *model.RelationUserRole) graphql.Marshaler { + fields := graphql.CollectFields(ec.OperationContext, sel, relationUserRoleImplementors) + + out := graphql.NewFieldSet(fields) + deferred := make(map[string]*graphql.FieldSet) + for i, field := range fields { + switch field.Name { + case "__typename": + out.Values[i] = graphql.MarshalString("RelationUserRole") + case "role": + out.Values[i] = ec._RelationUserRole_role(ctx, field, obj) + if out.Values[i] == graphql.Null { + out.Invalids++ + } + case "userIsRoleManager": + out.Values[i] = ec._RelationUserRole_userIsRoleManager(ctx, field, obj) + if out.Values[i] == graphql.Null { + out.Invalids++ + } + default: + panic("unknown field " + strconv.Quote(field.Name)) + } + } + out.Dispatch(ctx) + if out.Invalids > 0 { + return graphql.Null + } + + atomic.AddInt32(&ec.deferred, int32(len(deferred))) + + for label, dfs := range deferred { + ec.processDeferredGroup(graphql.DeferredGroup{ + Label: label, + Path: graphql.GetPath(ctx), + FieldSet: dfs, + Context: ctx, + }) + } + + return out +} + var roleImplementors = []string{"Role"} func (ec *executionContext) _Role(ctx context.Context, sel ast.SelectionSet, obj *model.Role) graphql.Marshaler { @@ -5911,23 +6425,59 @@ func (ec *executionContext) _Role(ctx context.Context, sel ast.SelectionSet, obj case "id": out.Values[i] = ec._Role_id(ctx, field, obj) if out.Values[i] == graphql.Null { - out.Invalids++ + atomic.AddUint32(&out.Invalids, 1) } case "roleName": out.Values[i] = ec._Role_roleName(ctx, field, obj) if out.Values[i] == graphql.Null { - out.Invalids++ + atomic.AddUint32(&out.Invalids, 1) } case "isAdmin": out.Values[i] = ec._Role_isAdmin(ctx, field, obj) if out.Values[i] == graphql.Null { - out.Invalids++ + atomic.AddUint32(&out.Invalids, 1) } case "isUserCreator": out.Values[i] = ec._Role_isUserCreator(ctx, field, obj) if out.Values[i] == graphql.Null { - out.Invalids++ + atomic.AddUint32(&out.Invalids, 1) } + case "roleMembers": + field := field + + innerFunc := func(ctx context.Context, fs *graphql.FieldSet) (res graphql.Marshaler) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + } + }() + res = ec._Role_roleMembers(ctx, field, obj) + if res == graphql.Null { + atomic.AddUint32(&fs.Invalids, 1) + } + return res + } + + if field.Deferrable != nil { + dfs, ok := deferred[field.Deferrable.Label] + di := 0 + if ok { + dfs.AddField(field) + di = len(dfs.Values) - 1 + } else { + dfs = graphql.NewFieldSet([]graphql.CollectedField{field}) + deferred[field.Deferrable.Label] = dfs + } + dfs.Concurrently(di, func(ctx context.Context) graphql.Marshaler { + return innerFunc(ctx, dfs) + }) + + // don't run the out.Concurrently() call below + out.Values[i] = graphql.Null + continue + } + + out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) }) default: panic("unknown field " + strconv.Quote(field.Name)) } @@ -6603,6 +7153,114 @@ func (ec *executionContext) marshalNRefreshToken2ᚖsomepiᚗddnsᚗnetᚋgitea return ec._RefreshToken(ctx, sel, v) } +func (ec *executionContext) marshalNRelationRoleUser2ᚕᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRelationRoleUserᚄ(ctx context.Context, sel ast.SelectionSet, v []*model.RelationRoleUser) graphql.Marshaler { + ret := make(graphql.Array, len(v)) + var wg sync.WaitGroup + isLen1 := len(v) == 1 + if !isLen1 { + wg.Add(len(v)) + } + for i := range v { + i := i + fc := &graphql.FieldContext{ + Index: &i, + Result: &v[i], + } + ctx := graphql.WithFieldContext(ctx, fc) + f := func(i int) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = nil + } + }() + if !isLen1 { + defer wg.Done() + } + ret[i] = ec.marshalNRelationRoleUser2ᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRelationRoleUser(ctx, sel, v[i]) + } + if isLen1 { + f(i) + } else { + go f(i) + } + + } + wg.Wait() + + for _, e := range ret { + if e == graphql.Null { + return graphql.Null + } + } + + return ret +} + +func (ec *executionContext) marshalNRelationRoleUser2ᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRelationRoleUser(ctx context.Context, sel ast.SelectionSet, v *model.RelationRoleUser) graphql.Marshaler { + if v == nil { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "the requested element is null which the schema does not allow") + } + return graphql.Null + } + return ec._RelationRoleUser(ctx, sel, v) +} + +func (ec *executionContext) marshalNRelationUserRole2ᚕᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRelationUserRoleᚄ(ctx context.Context, sel ast.SelectionSet, v []*model.RelationUserRole) graphql.Marshaler { + ret := make(graphql.Array, len(v)) + var wg sync.WaitGroup + isLen1 := len(v) == 1 + if !isLen1 { + wg.Add(len(v)) + } + for i := range v { + i := i + fc := &graphql.FieldContext{ + Index: &i, + Result: &v[i], + } + ctx := graphql.WithFieldContext(ctx, fc) + f := func(i int) { + defer func() { + if r := recover(); r != nil { + ec.Error(ctx, ec.Recover(ctx, r)) + ret = nil + } + }() + if !isLen1 { + defer wg.Done() + } + ret[i] = ec.marshalNRelationUserRole2ᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRelationUserRole(ctx, sel, v[i]) + } + if isLen1 { + f(i) + } else { + go f(i) + } + + } + wg.Wait() + + for _, e := range ret { + if e == graphql.Null { + return graphql.Null + } + } + + return ret +} + +func (ec *executionContext) marshalNRelationUserRole2ᚖsomepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRelationUserRole(ctx context.Context, sel ast.SelectionSet, v *model.RelationUserRole) graphql.Marshaler { + if v == nil { + if !graphql.HasFieldError(ctx, graphql.GetFieldContext(ctx)) { + ec.Errorf(ctx, "the requested element is null which the schema does not allow") + } + return graphql.Null + } + return ec._RelationUserRole(ctx, sel, v) +} + func (ec *executionContext) marshalNRole2somepiᚗddnsᚗnetᚋgiteaᚋgilexᚑdevᚋYetAnotherToDoListᚋgraphᚋmodelᚐRole(ctx context.Context, sel ast.SelectionSet, v model.Role) graphql.Marshaler { return ec._Role(ctx, sel, &v) } diff --git a/graph/model/models_gen.go b/graph/model/models_gen.go index 7c27260..e169de4 100644 --- a/graph/model/models_gen.go +++ b/graph/model/models_gen.go @@ -32,11 +32,22 @@ type RefreshToken struct { UserID string `json:"userId"` } +type RelationRoleUser struct { + User *User `json:"user"` + UserIsRoleManager bool `json:"userIsRoleManager"` +} + +type RelationUserRole struct { + Role *Role `json:"role"` + UserIsRoleManager bool `json:"userIsRoleManager"` +} + type Role struct { - ID string `json:"id"` - RoleName string `json:"roleName"` - IsAdmin bool `json:"isAdmin"` - IsUserCreator bool `json:"isUserCreator"` + ID string `json:"id"` + RoleName string `json:"roleName"` + IsAdmin bool `json:"isAdmin"` + IsUserCreator bool `json:"isUserCreator"` + RoleMembers []*RelationRoleUser `json:"roleMembers"` } type UpdateRefreshToken struct { @@ -61,9 +72,9 @@ type UpdateUser struct { } type User struct { - ID string `json:"id"` - UserName string `json:"userName"` - FullName *string `json:"fullName,omitempty"` - Todos []*Todo `json:"todos"` - Roles []*Role `json:"roles"` + ID string `json:"id"` + UserName string `json:"userName"` + FullName *string `json:"fullName,omitempty"` + Todos []*Todo `json:"todos"` + Roles []*RelationUserRole `json:"roles"` } diff --git a/graph/schema.graphqls b/graph/schema.graphqls index 5ecdbc6..0fe6eee 100644 --- a/graph/schema.graphqls +++ b/graph/schema.graphqls @@ -29,7 +29,17 @@ type User { userName: String! fullName: String todos: [Todo!]! - roles: [Role!]! + roles: [RelationUserRole!]! +} + +type RelationUserRole { + role: Role! + userIsRoleManager: Boolean! +} + +type RelationRoleUser { + user: User! + userIsRoleManager: Boolean! } type Role { @@ -37,6 +47,7 @@ type Role { roleName: String! isAdmin: Boolean! isUserCreator: Boolean! + roleMembers: [RelationRoleUser!]! } type RefreshToken { @@ -114,6 +125,15 @@ type Mutation { deleteTodo(id: ID!): ID deleteRole(id: ID!): ID deleteRefreshToken(id: ID!): ID - addRole(userId: ID!, roleId: ID!): [Role!]! - removeRole(userId: ID!, roleId: ID!): [Role!]! + addUserRole( + userId: ID! + roleId: ID! + userIsRoleManager: Boolean! + ): [RelationUserRole!]! + UpdateUserRole( + userId: ID! + roleId: ID! + userIsRoleManager: Boolean! + ): [RelationUserRole!]! + removeUserRole(userId: ID!, roleId: ID!): [RelationUserRole!]! } diff --git a/graph/schema.resolvers.go b/graph/schema.resolvers.go index cdb8206..4d3a7b0 100644 --- a/graph/schema.resolvers.go +++ b/graph/schema.resolvers.go @@ -2,7 +2,7 @@ package graph // This file will be automatically regenerated based on the schema, any resolver implementations // will be copied through when generating and any unknown code will be moved to the end. -// Code generated by github.com/99designs/gqlgen version v0.17.37 +// Code generated by github.com/99designs/gqlgen version v0.17.40 import ( "context" @@ -95,17 +95,25 @@ func (r *mutationResolver) DeleteRefreshToken(ctx context.Context, id string) (* return globals.DB.RevokeRefreshToken(id) } -// AddRole is the resolver for the addRole field. -func (r *mutationResolver) AddRole(ctx context.Context, userID string, roleID string) ([]*model.Role, error) { - if _, err := globals.DB.AddRole(userID, roleID); err != nil { +// AddUserRole is the resolver for the addUserRole field. +func (r *mutationResolver) AddUserRole(ctx context.Context, userID string, roleID string, userIsRoleManager bool) ([]*model.RelationUserRole, error) { + if _, err := globals.DB.AddUserRole(userID, roleID, userIsRoleManager); err != nil { return nil, err } return globals.DB.GetRolesFrom(userID) } -// RemoveRole is the resolver for the removeRole field. -func (r *mutationResolver) RemoveRole(ctx context.Context, userID string, roleID string) ([]*model.Role, error) { - if _, err := globals.DB.RemoveRole(userID, roleID); err != nil { +// UpdateUserRole is the resolver for the UpdateUserRole field. +func (r *mutationResolver) UpdateUserRole(ctx context.Context, userID string, roleID string, userIsRoleManager bool) ([]*model.RelationUserRole, error) { + if _, err := globals.DB.UpdateUserRole(userID, roleID, userIsRoleManager); err != nil { + return nil, err + } + return globals.DB.GetRolesFrom(userID) +} + +// RemoveUserRole is the resolver for the RemoveUserRole field. +func (r *mutationResolver) RemoveUserRole(ctx context.Context, userID string, roleID string) ([]*model.RelationUserRole, error) { + if _, err := globals.DB.RemoveUserRole(userID, roleID); err != nil { return nil, err } return globals.DB.GetRolesFrom(userID) @@ -151,6 +159,11 @@ func (r *queryResolver) RefreshToken(ctx context.Context, id string) (*model.Ref return globals.DB.GetRefreshToken(&model.RefreshToken{ID: id}) } +// RoleMembers is the resolver for the roleMembers field. +func (r *roleResolver) RoleMembers(ctx context.Context, obj *model.Role) ([]*model.RelationRoleUser, error) { + return globals.DB.GetRoleMembers(obj.ID) +} + // User is the resolver for the user field. func (r *todoResolver) User(ctx context.Context, obj *model.Todo) (*model.User, error) { // TODO: implement dataloader @@ -163,7 +176,7 @@ func (r *userResolver) Todos(ctx context.Context, obj *model.User) ([]*model.Tod } // Roles is the resolver for the roles field. -func (r *userResolver) Roles(ctx context.Context, obj *model.User) ([]*model.Role, error) { +func (r *userResolver) Roles(ctx context.Context, obj *model.User) ([]*model.RelationUserRole, error) { return globals.DB.GetRolesFrom(obj.ID) } @@ -173,6 +186,9 @@ func (r *Resolver) Mutation() MutationResolver { return &mutationResolver{r} } // Query returns QueryResolver implementation. func (r *Resolver) Query() QueryResolver { return &queryResolver{r} } +// Role returns RoleResolver implementation. +func (r *Resolver) Role() RoleResolver { return &roleResolver{r} } + // Todo returns TodoResolver implementation. func (r *Resolver) Todo() TodoResolver { return &todoResolver{r} } @@ -181,5 +197,6 @@ func (r *Resolver) User() UserResolver { return &userResolver{r} } type mutationResolver struct{ *Resolver } type queryResolver struct{ *Resolver } +type roleResolver struct{ *Resolver } type todoResolver struct{ *Resolver } type userResolver struct{ *Resolver }