Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support mutation outside composition functions #1146

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/test-e2e-composable-vue3/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Query {
}

type Mutation {
personalizedHello (name: String!): String
addMessage (input: AddMessageInput!): Message
updateMessage (input: UpdateMessageInput!): Message
}
Expand Down Expand Up @@ -75,6 +76,7 @@ const resolvers = {
},

Mutation: {
personalizedHello: (root, { name }) => `Hello ${name}!`,
addMessage: (root, { input }) => {
const channel = channels.find(c => c.id === input.channelId)
if (!channel) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<script lang="ts">
import { apolloClient } from '@/apollo'
import { gql } from '@apollo/client/core'
import { provideApolloClient, useMutation, useResult } from '@vue/apollo-composable'
import { defineComponent, ref } from 'vue'

// Global mutation

const greeting = ref('')
provideApolloClient(apolloClient)(() => {
const { mutate } = useMutation(gql`
mutation getPersonalizedHello ($name: String!) {
greeting: personalizedHello (name: $name)
}
`)
mutate({ name: 'John' }).then(result => greeting.value = result.data.greeting)
},
)

export default defineComponent({
setup () {
return {
greeting,
}
},
})
</script>

<template>
<div class="no-setup-mutation">
{{ greeting }}
</div>
</template>
5 changes: 5 additions & 0 deletions packages/test-e2e-composable-vue3/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createRouter, createWebHistory } from 'vue-router'
import Welcome from './components/Welcome.vue'
import ChannelView from './components/ChannelView.vue'
import NoSetupQuery from './components/NoSetupQuery.vue'
import NoSetupMutation from './components/NoSetupMutation.vue'
import LazyQuery from './components/LazyQuery.vue'

export const router = createRouter({
Expand All @@ -22,6 +23,10 @@ export const router = createRouter({
path: '/no-setup-query',
component: NoSetupQuery,
},
{
path: '/no-setup-mutation',
component: NoSetupMutation,
},
{
path: '/lazy-query',
component: LazyQuery,
Expand Down
5 changes: 5 additions & 0 deletions packages/test-e2e-composable-vue3/tests/e2e/specs/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ describe('Vue 3 + Apollo Composable', () => {
cy.contains('.no-setup-query', 'Hello world!')
})

it('supports mutations outside of setup', () => {
cy.visit('/no-setup-mutation')
cy.contains('.no-setup-mutation', 'Hello John!')
})

it('useLazyQuery', () => {
cy.visit('/lazy-query')
cy.get('.list-disc').should('have.length', 0)
Expand Down
9 changes: 6 additions & 3 deletions packages/vue-apollo-composable/src/useMutation.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { DocumentNode } from 'graphql'
import { MutationOptions, OperationVariables, FetchResult, TypedDocumentNode } from '@apollo/client/core'
import { ref, onBeforeUnmount, isRef, Ref } from 'vue-demi'
import { ref, onBeforeUnmount, isRef, Ref, getCurrentInstance } from 'vue-demi'
import { useApolloClient } from './useApolloClient'
import { ReactiveFunction } from './util/ReactiveFunction'
import { useEventHook } from './util/useEventHook'
import { trackMutation } from './util/loadingTracking'
import { CurrentInstance } from './util/types'

/**
* `useMutation` options for mutations that don't require `variables`.
Expand Down Expand Up @@ -43,8 +44,10 @@ export function useMutation<
document: DocumentParameter<TResult, TVariables>,
options: OptionsParameter<TResult, TVariables> = {},
): UseMutationReturn<TResult, TVariables> {
const vm = getCurrentInstance() as CurrentInstance | null

const loading = ref<boolean>(false)
trackMutation(loading)
vm && trackMutation(loading)
const error = ref<Error | null>(null)
const called = ref<boolean>(false)

Expand Down Expand Up @@ -99,7 +102,7 @@ export function useMutation<
}
}

onBeforeUnmount(() => {
vm && onBeforeUnmount(() => {
loading.value = false
})

Expand Down