refactor: Queries to Mutations

This commit is contained in:
Moriz Wahl 2021-09-20 11:22:00 +02:00
parent db15433cc2
commit e6d386c598
17 changed files with 71 additions and 68 deletions

View File

@ -1,7 +1,7 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { Resolver, Query, Args, Arg, Authorized, Ctx, UseMiddleware } from 'type-graphql'
import { Resolver, Query, Args, Arg, Authorized, Ctx, UseMiddleware, Mutation } from 'type-graphql'
import CONFIG from '../../config'
import { CheckUsernameResponse } from '../models/CheckUsernameResponse'
import { LoginViaVerificationCode } from '../models/LoginViaVerificationCode'
@ -104,7 +104,7 @@ export class UserResolver {
return new SendPasswordResetEmailResponse(response.data)
}
@Query(() => String)
@Mutation(() => String)
async resetPassword(
@Args()
{ sessionId, email, password }: ChangePasswordArgs,
@ -122,7 +122,7 @@ export class UserResolver {
}
@Authorized()
@Query(() => UpdateUserInfosResponse)
@Mutation(() => UpdateUserInfosResponse)
async updateUserInfos(
@Args()
{

View File

@ -3,7 +3,7 @@ import LanguageSwitch from './LanguageSwitch'
const localVue = global.localVue
const updateUserInfosQueryMock = jest.fn().mockResolvedValue({
const updateUserInfosMutationMock = jest.fn().mockResolvedValue({
data: {
updateUserInfos: {
validValues: 1,
@ -28,7 +28,7 @@ describe('LanguageSwitch', () => {
locale: 'en',
},
$apollo: {
query: updateUserInfosQueryMock,
mutate: updateUserInfosMutationMock,
},
}
@ -119,7 +119,7 @@ describe('LanguageSwitch', () => {
describe('calls the API', () => {
it("with locale 'en'", () => {
wrapper.findAll('li').at(0).find('a').trigger('click')
expect(updateUserInfosQueryMock).toBeCalledWith(
expect(updateUserInfosMutationMock).toBeCalledWith(
expect.objectContaining({
variables: {
email: 'he@ho.he',
@ -131,7 +131,7 @@ describe('LanguageSwitch', () => {
it("with locale 'de'", () => {
wrapper.findAll('li').at(1).find('a').trigger('click')
expect(updateUserInfosQueryMock).toBeCalledWith(
expect(updateUserInfosMutationMock).toBeCalledWith(
expect.objectContaining({
variables: {
email: 'he@ho.he',

View File

@ -14,7 +14,7 @@
<script>
import { localeChanged } from 'vee-validate'
import locales from '../locales/'
import { updateUserInfos } from '../graphql/queries'
import { updateUserInfos } from '../graphql/mutations'
export default {
name: 'LanguageSwitch',
@ -36,8 +36,8 @@ export default {
this.setLocale(locale)
if (this.$store.state.email) {
this.$apollo
.query({
query: updateUserInfos,
.mutate({
mutation: updateUserInfos,
variables: {
email: this.$store.state.email,
locale: locale,

View File

@ -11,3 +11,35 @@ export const unsubscribeNewsletter = gql`
unsubscribeNewsletter(email: $email)
}
`
export const resetPassword = gql`
mutation($sessionId: Float!, $email: String!, $password: String!) {
resetPassword(sessionId: $sessionId, email: $email, password: $password)
}
`
export const updateUserInfos = gql`
query(
$email: String!
$firstName: String
$lastName: String
$description: String
$username: String
$password: String
$passwordNew: String
$locale: String
) {
updateUserInfos(
email: $email
firstName: $firstName
lastName: $lastName
description: $description
username: $username
password: $password
passwordNew: $passwordNew
language: $locale
) {
validValues
}
}
`

View File

@ -22,12 +22,6 @@ export const logout = gql`
}
`
export const resetPassword = gql`
query($sessionId: Float!, $email: String!, $password: String!) {
resetPassword(sessionId: $sessionId, email: $email, password: $password)
}
`
export const loginViaEmailVerificationCode = gql`
query($optin: String!) {
loginViaEmailVerificationCode(optin: $optin) {
@ -37,32 +31,6 @@ export const loginViaEmailVerificationCode = gql`
}
`
export const updateUserInfos = gql`
query(
$email: String!
$firstName: String
$lastName: String
$description: String
$username: String
$password: String
$passwordNew: String
$locale: String
) {
updateUserInfos(
email: $email
firstName: $firstName
lastName: $lastName
description: $description
username: $username
password: $password
passwordNew: $passwordNew
language: $locale
) {
validValues
}
}
`
export const transactionsQuery = gql`
query($firstPage: Int = 1, $items: Int = 25, $order: String = "DESC") {
transactionList(firstPage: $firstPage, items: $items, order: $order) {

View File

@ -7,6 +7,7 @@ import flushPromises from 'flush-promises'
const localVue = global.localVue
const apolloQueryMock = jest.fn().mockRejectedValue({ message: 'error' })
const apolloMutationMock = jest.fn()
const toasterMock = jest.fn()
const routerPushMock = jest.fn()
@ -36,6 +37,7 @@ describe('ResetPassword', () => {
}),
},
$apollo: {
mutate: apolloMutationMock,
query: apolloQueryMock,
},
}
@ -146,7 +148,7 @@ describe('ResetPassword', () => {
describe('server response with error', () => {
beforeEach(() => {
apolloQueryMock.mockRejectedValue({ message: 'error' })
apolloMutationMock.mockRejectedValue({ message: 'error' })
})
it('toasts an error message', () => {
expect(toasterMock).toHaveBeenCalledWith('error')
@ -155,14 +157,14 @@ describe('ResetPassword', () => {
describe('server response with success', () => {
beforeEach(() => {
apolloQueryMock.mockResolvedValue({
apolloMutationMock.mockResolvedValue({
data: {
resetPassword: 'success',
},
})
})
it('calls the API', () => {
expect(apolloQueryMock).toBeCalledWith(
expect(apolloMutationMock).toBeCalledWith(
expect.objectContaining({
variables: {
sessionId: 1,

View File

@ -48,7 +48,8 @@
</template>
<script>
import InputPasswordConfirmation from '../../components/Inputs/InputPasswordConfirmation'
import { resetPassword, loginViaEmailVerificationCode } from '../../graphql/queries'
import { loginViaEmailVerificationCode } from '../../graphql/queries'
import { resetPassword } from '../../graphql/mutations'
export default {
name: 'ResetPassword',
@ -71,8 +72,8 @@ export default {
methods: {
async onSubmit() {
this.$apollo
.query({
query: resetPassword,
.mutate({
mutation: resetPassword,
variables: {
sessionId: this.sessionId,
email: this.email,

View File

@ -29,7 +29,7 @@ describe('UserCard_FormUserData', () => {
error: toastErrorMock,
},
$apollo: {
query: mockAPIcall,
mutate: mockAPIcall,
},
}

View File

@ -72,7 +72,7 @@
</b-card>
</template>
<script>
import { updateUserInfos } from '../../../graphql/queries'
import { updateUserInfos } from '../../../graphql/mutations'
export default {
name: 'FormUserData',
@ -108,8 +108,8 @@ export default {
async onSubmit(event) {
event.preventDefault()
this.$apollo
.query({
query: updateUserInfos,
.mutate({
mutation: updateUserInfos,
variables: {
email: this.$store.state.email,
firstName: this.form.firstName,

View File

@ -21,7 +21,7 @@ describe('UserCard_FormUserMail', () => {
},
},
$apollo: {
query: mockAPIcall,
mutate: mockAPIcall,
},
}

View File

@ -32,7 +32,7 @@
</b-card>
</template>
<script>
import { updateUserInfos } from '../../../graphql/queries'
import { updateUserInfos } from '../../../graphql/mutations'
export default {
name: 'FormUserMail',
@ -45,8 +45,8 @@ export default {
methods: {
async onSubmit() {
this.$apollo
.query({
query: updateUserInfos,
.mutate({
mutation: updateUserInfos,
variables: {
email: this.$store.state.email,
newEmail: this.newEmail,

View File

@ -25,7 +25,7 @@ describe('UserCard_FormUserPasswort', () => {
error: toastErrorMock,
},
$apollo: {
query: changePasswordProfileMock,
mutate: changePasswordProfileMock,
},
}

View File

@ -42,7 +42,7 @@
<script>
import InputPassword from '../../../components/Inputs/InputPassword'
import InputPasswordConfirmation from '../../../components/Inputs/InputPasswordConfirmation'
import { updateUserInfos } from '../../../graphql/queries'
import { updateUserInfos } from '../../../graphql/mutations'
export default {
name: 'FormUserPasswort',
@ -73,8 +73,8 @@ export default {
},
async onSubmit() {
this.$apollo
.query({
query: updateUserInfos,
.mutate({
mutations: updateUserInfos,
variables: {
email: this.$store.state.email,
password: this.form.password,

View File

@ -35,7 +35,7 @@ describe('UserCard_FormUsername', () => {
error: toastErrorMock,
},
$apollo: {
query: mockAPIcall,
mutate: mockAPIcall,
},
}

View File

@ -67,7 +67,7 @@
</b-card>
</template>
<script>
import { updateUserInfos } from '../../../graphql/queries'
import { updateUserInfos } from '../../../graphql/mutations'
export default {
name: 'FormUsername',
@ -87,8 +87,8 @@ export default {
},
async onSubmit() {
this.$apollo
.query({
query: updateUserInfos,
.mutate({
mutation: updateUserInfos,
variables: {
email: this.$store.state.email,
username: this.form.username,

View File

@ -25,7 +25,7 @@ describe('UserCard_Language', () => {
error: toastErrorMock,
},
$apollo: {
query: mockAPIcall,
mutate: mockAPIcall,
},
}

View File

@ -63,7 +63,7 @@
</template>
<script>
import LanguageSwitchSelect from '../../../components/LanguageSwitchSelect.vue'
import { updateUserInfos } from '../../../graphql/queries'
import { updateUserInfos } from '../../../graphql/mutations'
export default {
name: 'FormUserLanguage',
@ -90,8 +90,8 @@ export default {
async onSubmit() {
this.$apollo
.query({
query: updateUserInfos,
.mutate({
mutation: updateUserInfos,
variables: {
language: this.$store.state.language,
},