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

View File

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

View File

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

View File

@ -11,3 +11,35 @@ export const unsubscribeNewsletter = gql`
unsubscribeNewsletter(email: $email) 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` export const loginViaEmailVerificationCode = gql`
query($optin: String!) { query($optin: String!) {
loginViaEmailVerificationCode(optin: $optin) { 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` export const transactionsQuery = gql`
query($firstPage: Int = 1, $items: Int = 25, $order: String = "DESC") { query($firstPage: Int = 1, $items: Int = 25, $order: String = "DESC") {
transactionList(firstPage: $firstPage, items: $items, order: $order) { transactionList(firstPage: $firstPage, items: $items, order: $order) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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