Skip to content

Commit f3741ba

Browse files
authored
Add GQL API for deleteUser (#1443)
Summary: Previous changes added deleteUser to the profile and API gRPC services. This PR adds it to the graphql layer. Relevant Issues: #655 Type of change: /kind feature Test Plan: Unit tests Changelog Message: ```release-note Add API to delete user in GQL and gRPC. If the user being deleted is the only user in the org, the org will also be deleted. ``` --------- Signed-off-by: Michelle Nguyen <michellenguyen@pixielabs.ai>
1 parent 0ba4a1b commit f3741ba

File tree

6 files changed

+61
-4
lines changed

6 files changed

+61
-4
lines changed

src/cloud/api/controllers/schema/03_auth_schema.graphql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ extend type Mutation {
4444
DeleteAPIKey(id: ID!): Boolean!
4545
UpdateUserSettings(settings: EditableUserSettings!): UserSettings!
4646
SetUserAttributes(attributes: EditableUserAttributes!): UserAttributes!
47+
DeleteUser: Boolean!
4748
InviteUser(email: String!, firstName: String!, lastName: String!): UserInvite!
4849
UpdateUserPermissions(userID: ID!, userPermissions: EditableUserPermissions!): UserInfo!
4950
CreateOrg(orgName: String!): ID!

src/cloud/api/controllers/schema/complete/bindata.gen.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cloud/api/controllers/schema/noauth/bindata.gen.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cloud/api/controllers/user_resolver.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,23 @@ func (q *QueryResolver) SetUserAttributes(ctx context.Context, args *setUserAttr
241241

242242
return resp, nil
243243
}
244+
245+
// DeleteUser deletes the user with the current credentials.
246+
func (q *QueryResolver) DeleteUser(ctx context.Context) (bool, error) {
247+
sCtx, err := authcontext.FromContext(ctx)
248+
if err != nil {
249+
return false, err
250+
}
251+
252+
id := sCtx.Claims.GetUserClaims().UserID
253+
req := &cloudpb.DeleteUserRequest{
254+
ID: utils.ProtoFromUUIDStrOrNil(id),
255+
}
256+
257+
_, err = q.Env.UserServer.DeleteUser(ctx, req)
258+
if err != nil {
259+
return false, rpcErrorHelper(err)
260+
}
261+
262+
return true, nil
263+
}

src/cloud/api/controllers/user_resolver_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,3 +420,33 @@ func TestUserSettingsResolver_SetUserAttributes(t *testing.T) {
420420
})
421421
}
422422
}
423+
424+
func TestUserSettingsResolver_DeleteUser(t *testing.T) {
425+
t.Run("deletion succeeds", func(t *testing.T) {
426+
gqlEnv, mockClients, cleanup := gqltestutils.CreateTestGraphQLEnv(t)
427+
defer cleanup()
428+
ctx := CreateTestContext()
429+
430+
mockClients.MockUser.EXPECT().DeleteUser(gomock.Any(), &cloudpb.DeleteUserRequest{
431+
ID: utils.ProtoFromUUIDStrOrNil("6ba7b810-9dad-11d1-80b4-00c04fd430c9"),
432+
}).Return(&cloudpb.DeleteUserResponse{}, nil)
433+
434+
gqlSchema := LoadSchema(gqlEnv)
435+
gqltesting.RunTests(t, []*gqltesting.Test{
436+
{
437+
Schema: gqlSchema,
438+
Context: ctx,
439+
Query: `
440+
mutation {
441+
DeleteUser()
442+
}
443+
`,
444+
ExpectedResult: `
445+
{
446+
"DeleteUser": true
447+
}
448+
`,
449+
},
450+
})
451+
})
452+
}

src/ui/src/types/schema.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export interface GQLMutation {
5757
DeleteAPIKey: boolean;
5858
UpdateUserSettings: GQLUserSettings;
5959
SetUserAttributes: GQLUserAttributes;
60+
DeleteUser: boolean;
6061
InviteUser: GQLUserInvite;
6162
UpdateUserPermissions: GQLUserInfo;
6263
CreateOrg: string;
@@ -640,6 +641,7 @@ export interface GQLMutationTypeResolver<TParent = any> {
640641
DeleteAPIKey?: MutationToDeleteAPIKeyResolver<TParent>;
641642
UpdateUserSettings?: MutationToUpdateUserSettingsResolver<TParent>;
642643
SetUserAttributes?: MutationToSetUserAttributesResolver<TParent>;
644+
DeleteUser?: MutationToDeleteUserResolver<TParent>;
643645
InviteUser?: MutationToInviteUserResolver<TParent>;
644646
UpdateUserPermissions?: MutationToUpdateUserPermissionsResolver<TParent>;
645647
CreateOrg?: MutationToCreateOrgResolver<TParent>;
@@ -697,6 +699,10 @@ export interface MutationToSetUserAttributesResolver<TParent = any, TResult = an
697699
(parent: TParent, args: MutationToSetUserAttributesArgs, context: any, info: GraphQLResolveInfo): TResult;
698700
}
699701

702+
export interface MutationToDeleteUserResolver<TParent = any, TResult = any> {
703+
(parent: TParent, args: {}, context: any, info: GraphQLResolveInfo): TResult;
704+
}
705+
700706
export interface MutationToInviteUserArgs {
701707
email: string;
702708
firstName: string;

0 commit comments

Comments
 (0)