Skip to content

Commit

Permalink
Merge pull request #2244 from flacial/2243-add-the-ability-to-unlink-…
Browse files Browse the repository at this point in the history
…a-discord-account

Create unlinkDiscord resolver
  • Loading branch information
flacial authored Sep 3, 2022
2 parents a0c41a7 + e69d189 commit 7335130
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 1 deletion.
95 changes: 95 additions & 0 deletions graphql/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export type Mutation = {
reqPwReset: SuccessResponse
setStar: SuccessResponse
signup?: Maybe<AuthResponse>
unlinkDiscord?: Maybe<User>
updateChallenge?: Maybe<Array<Maybe<Lesson>>>
updateExercise: Exercise
updateLesson: Array<Lesson>
Expand Down Expand Up @@ -1059,6 +1060,13 @@ export type SignupMutation = {
} | null
}

export type UnlinkDiscordMutationVariables = Exact<{ [key: string]: never }>

export type UnlinkDiscordMutation = {
__typename?: 'Mutation'
unlinkDiscord?: { __typename?: 'User'; id: number } | null
}

export type UpdateChallengeMutationVariables = Exact<{
lessonId: Scalars['Int']
order: Scalars['Int']
Expand Down Expand Up @@ -1654,6 +1662,11 @@ export type MutationResolvers<
'email' | 'firstName' | 'lastName' | 'password' | 'username'
>
>
unlinkDiscord?: Resolver<
Maybe<ResolversTypes['User']>,
ParentType,
ContextType
>
updateChallenge?: Resolver<
Maybe<Array<Maybe<ResolversTypes['Lesson']>>>,
ParentType,
Expand Down Expand Up @@ -4588,6 +4601,86 @@ export type SignupMutationOptions = Apollo.BaseMutationOptions<
SignupMutation,
SignupMutationVariables
>
export const UnlinkDiscordDocument = gql`
mutation unlinkDiscord {
unlinkDiscord {
id
}
}
`
export type UnlinkDiscordMutationFn = Apollo.MutationFunction<
UnlinkDiscordMutation,
UnlinkDiscordMutationVariables
>
export type UnlinkDiscordProps<
TChildProps = {},
TDataName extends string = 'mutate'
> = {
[key in TDataName]: Apollo.MutationFunction<
UnlinkDiscordMutation,
UnlinkDiscordMutationVariables
>
} & TChildProps
export function withUnlinkDiscord<
TProps,
TChildProps = {},
TDataName extends string = 'mutate'
>(
operationOptions?: ApolloReactHoc.OperationOption<
TProps,
UnlinkDiscordMutation,
UnlinkDiscordMutationVariables,
UnlinkDiscordProps<TChildProps, TDataName>
>
) {
return ApolloReactHoc.withMutation<
TProps,
UnlinkDiscordMutation,
UnlinkDiscordMutationVariables,
UnlinkDiscordProps<TChildProps, TDataName>
>(UnlinkDiscordDocument, {
alias: 'unlinkDiscord',
...operationOptions
})
}

/**
* __useUnlinkDiscordMutation__
*
* To run a mutation, you first call `useUnlinkDiscordMutation` within a React component and pass it any options that fit your needs.
* When your component renders, `useUnlinkDiscordMutation` returns a tuple that includes:
* - A mutate function that you can call at any time to execute the mutation
* - An object with fields that represent the current status of the mutation's execution
*
* @param baseOptions options that will be passed into the mutation, supported options are listed on: https://www.apollographql.com/docs/react/api/react-hooks/#options-2;
*
* @example
* const [unlinkDiscordMutation, { data, loading, error }] = useUnlinkDiscordMutation({
* variables: {
* },
* });
*/
export function useUnlinkDiscordMutation(
baseOptions?: Apollo.MutationHookOptions<
UnlinkDiscordMutation,
UnlinkDiscordMutationVariables
>
) {
const options = { ...defaultOptions, ...baseOptions }
return Apollo.useMutation<
UnlinkDiscordMutation,
UnlinkDiscordMutationVariables
>(UnlinkDiscordDocument, options)
}
export type UnlinkDiscordMutationHookResult = ReturnType<
typeof useUnlinkDiscordMutation
>
export type UnlinkDiscordMutationResult =
Apollo.MutationResult<UnlinkDiscordMutation>
export type UnlinkDiscordMutationOptions = Apollo.BaseMutationOptions<
UnlinkDiscordMutation,
UnlinkDiscordMutationVariables
>
export const UpdateChallengeDocument = gql`
mutation updateChallenge(
$lessonId: Int!
Expand Down Expand Up @@ -5281,6 +5374,7 @@ export type MutationKeySpecifier = (
| 'reqPwReset'
| 'setStar'
| 'signup'
| 'unlinkDiscord'
| 'updateChallenge'
| 'updateExercise'
| 'updateLesson'
Expand Down Expand Up @@ -5310,6 +5404,7 @@ export type MutationFieldPolicy = {
reqPwReset?: FieldPolicy<any> | FieldReadFunction<any>
setStar?: FieldPolicy<any> | FieldReadFunction<any>
signup?: FieldPolicy<any> | FieldReadFunction<any>
unlinkDiscord?: FieldPolicy<any> | FieldReadFunction<any>
updateChallenge?: FieldPolicy<any> | FieldReadFunction<any>
updateExercise?: FieldPolicy<any> | FieldReadFunction<any>
updateLesson?: FieldPolicy<any> | FieldReadFunction<any>
Expand Down
9 changes: 9 additions & 0 deletions graphql/queries/unlinkDiscord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { gql } from '@apollo/client'

export const UNLINK_DISCORD = gql`
mutation unlinkDiscord {
unlinkDiscord {
id
}
}
`
4 changes: 3 additions & 1 deletion graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { changeAdminRights } from './resolvers/adminController'
import { createLesson, updateLesson } from './resolvers/lessonsController'
import { getPreviousSubmissions } from './resolvers/getPreviousSubmissions'
import { deleteComment } from './resolvers/deleteComment'
import { unlinkDiscord } from './resolvers/unlinkDiscord'
import {
addModule,
modules,
Expand Down Expand Up @@ -78,6 +79,7 @@ export default {
addComment,
deleteComment,
flagExercise,
removeExerciseFlag
removeExerciseFlag,
unlinkDiscord
}
}
26 changes: 26 additions & 0 deletions graphql/resolvers/unlinkDiscord.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import prismaMock from '../../__tests__/utils/prismaMock'
import { unlinkDiscord } from './unlinkDiscord'

const mockUser = {
username: 'noob',
id: 1
}

const ctx = { req: { user: { id: 1 } } }
const ctxWithError = { req: { user: { id: null } } }

describe('unlinkDiscord resolver', () => {
it('should unlink discord successfully', async () => {
prismaMock.user.update.mockResolvedValue(mockUser)

expect(unlinkDiscord(null, null, ctx)).resolves.toStrictEqual(mockUser)
})

it('should throw error', async () => {
prismaMock.user.update.mockResolvedValue(mockUser)

expect(unlinkDiscord(null, null, ctxWithError)).rejects.toThrow(
'Invalid user'
)
})
})
20 changes: 20 additions & 0 deletions graphql/resolvers/unlinkDiscord.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import prisma from '../../prisma'
import { Context } from '../../@types/helpers'

export const unlinkDiscord = async (_parent: void, _: any, ctx: Context) => {
const userId = ctx.req.user?.id

if (!userId) throw new Error('Invalid user')

return await prisma.user.update({
where: {
id: userId
},
data: {
discordAccessToken: '',
discordAccessTokenExpires: null,
discordId: '',
discordRefreshToken: ''
}
})
}
1 change: 1 addition & 0 deletions graphql/typeDefs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export default gql`
title: String!
id: Int!
): [Lesson]
unlinkDiscord: User
}
type AuthResponse {
Expand Down

1 comment on commit 7335130

@vercel
Copy link

@vercel vercel bot commented on 7335130 Sep 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.