Skip to content

Commit

Permalink
Stuff Roger does when alone... (#66)
Browse files Browse the repository at this point in the history
* Refactor GraphQL schema and resolvers to add support for member lookup by email

* make fmt

* Refactor team_members.sql and team_members.sql.go to sort names case-insensitively

* Refactor GetMemberByEmail to use teamMemberRoleFromSqlTeamRole for role assignment

* Refactor generated.go and repository.graphqls to fix null edge issue in RepositoryConnection
  • Loading branch information
rbjornstad authored Oct 7, 2024
1 parent f3a2cbd commit 0581965
Show file tree
Hide file tree
Showing 8 changed files with 336 additions and 57 deletions.
306 changes: 254 additions & 52 deletions internal/v1/graphv1/gengqlv1/generated.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/v1/graphv1/schema/repository.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type RepositoryConnection {
nodes: [Repository!]!

"List of edges."
edges: [RepositoryEdge]
edges: [RepositoryEdge!]!
}

type RepositoryEdge {
Expand Down
3 changes: 3 additions & 0 deletions internal/v1/graphv1/schema/teams.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ type Team implements Node {
"The CDN bucket for the team. This value is managed by the Google CDN reconciler."
cdnBucket: String

"Get a specific member of the team."
member(email: String!): TeamMember!

"Team members."
members(
"Get the first n items in the connection. This can be used in combination with the after parameter."
Expand Down
4 changes: 4 additions & 0 deletions internal/v1/graphv1/teams.resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ func (r *removeTeamMemberPayloadResolver) Team(ctx context.Context, obj *team.Re
return team.Get(ctx, obj.TeamSlug)
}

func (r *teamResolver) Member(ctx context.Context, obj *team.Team, email string) (*team.TeamMember, error) {
return team.GetMemberByEmail(ctx, obj.Slug, email)
}

func (r *teamResolver) Members(ctx context.Context, obj *team.Team, first *int, after *pagination.Cursor, last *int, before *pagination.Cursor, orderBy *team.TeamMemberOrder) (*pagination.Connection[*team.TeamMember], error) {
page, err := pagination.ParsePage(first, after, last, before)
if err != nil {
Expand Down
17 changes: 17 additions & 0 deletions internal/v1/team/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ func ListForUser(ctx context.Context, userID uuid.UUID, page *pagination.Paginat
return pagination.NewConvertConnection(ret, page, int32(total), toGraphUserTeam), nil
}

func GetMemberByEmail(ctx context.Context, teamSlug slug.Slug, email string) (*TeamMember, error) {
q := db(ctx)

m, err := q.GetMemberByEmail(ctx, teamsql.GetMemberByEmailParams{
TeamSlug: teamSlug,
Email: email,
})
if err != nil {
return nil, err
}
return &TeamMember{
Role: teamMemberRoleFromSqlTeamRole(m.RoleName),
TeamSlug: teamSlug,
UserID: m.ID,
}, nil
}

func ListMembers(ctx context.Context, teamSlug slug.Slug, page *pagination.Pagination, orderBy *TeamMemberOrder) (*TeamMemberConnection, error) {
q := db(ctx)

Expand Down
17 changes: 15 additions & 2 deletions internal/v1/team/queries/team_members.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ WHERE
user_roles.target_team_slug = @team_slug::slug
ORDER BY
CASE
WHEN @order_by::TEXT = 'name:asc' THEN users.name
WHEN @order_by::TEXT = 'name:asc' THEN LOWER(users.name)
END ASC,
CASE
WHEN @order_by::TEXT = 'name:desc' THEN users.name
WHEN @order_by::TEXT = 'name:desc' THEN LOWER(users.name)
END DESC,
CASE
WHEN @order_by::TEXT = 'email:asc' THEN users.email
Expand Down Expand Up @@ -92,6 +92,19 @@ WHERE
AND user_roles.user_id = @user_id
;

-- name: GetMemberByEmail :one
SELECT
users.*,
user_roles.role_name
FROM
user_roles
JOIN teams ON teams.slug = user_roles.target_team_slug
JOIN users ON users.id = user_roles.user_id
WHERE
user_roles.target_team_slug = @team_slug::slug
AND users.email = @email
;

-- name: AddMember :exec
INSERT INTO
user_roles (user_id, role_name, target_team_slug)
Expand Down
1 change: 1 addition & 0 deletions internal/v1/team/teamsql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 41 additions & 2 deletions internal/v1/team/teamsql/team_members.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 0581965

Please sign in to comment.