You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This component allows you to send mutations from your template.
4
+
5
+
Use [props](#props) to configure the component and the default [slot](#slot-props) to interact with it.
6
+
3
7
Example:
4
8
5
9
```vue
@@ -26,24 +30,276 @@ Example:
26
30
27
31
## Props
28
32
29
-
-`mutation`: GraphQL query (transformed by `graphql-tag`) or a function that receives the `gql` tag as argument and should return the transformed query
30
-
-`variables`: Object of GraphQL variables
31
-
-`optimisticResponse`: See [optimistic UI](https://www.apollographql.com/docs/react/performance/optimistic-ui/)
32
-
-`update`: See [updating cache after mutation](https://www.apollographql.com/docs/react/data/mutations/#updating-the-cache-after-a-mutation)
33
-
-`refetchQueries`: See [refetching queries after mutation](https://www.apollographql.com/docs/react/caching/advanced-topics/#updating-after-a-mutation)
34
-
-`clientId`: id of the Apollo Client used by the query (defined in ApolloProvider `clients` option)
35
-
-`tag`: String HTML tag name (default: `div`); if `undefined`, the component will be renderless (the content won't be wrapped in a tag)
36
-
-`context`: See [apollo context](https://www.apollographql.com/docs/react/api/link/apollo-link-context/)
33
+
### mutation
34
+
35
+
GraphQL query (transformed by `graphql-tag`) or a function that receives the `gql` tag as argument and should return the transformed query.
36
+
37
+
```vue
38
+
<ApolloMutation
39
+
:mutation="gql => gql`
40
+
mutation DoStuff ($name: String!) {
41
+
someWork (name: $name) {
42
+
success
43
+
timeSpent
44
+
}
45
+
}
46
+
`"
47
+
/>
48
+
```
49
+
50
+
### variables
51
+
52
+
Object of GraphQL variables.
53
+
54
+
```vue
55
+
<ApolloMutation
56
+
:variables="{
57
+
name
58
+
}"
59
+
/>
60
+
```
61
+
62
+
### optimisticResponse
63
+
64
+
Optimistic UI is a pattern that you can use to simulate the results of a mutation and update the UI even before receiving a response from the server. Once the response is received from the server, the optimistic result is thrown away and replaced with the actual result.
65
+
66
+
Optimistic UI provides an easy way to make your UI respond much faster, while ensuring that the data becomes consistent with the actual response when it arrives.
67
+
68
+
See [optimistic UI](https://www.apollographql.com/docs/react/performance/optimistic-ui/)
69
+
70
+
```vue
71
+
<ApolloMutation
72
+
:optimisticResponse="{
73
+
__typename: 'Mutation',
74
+
someWork: {
75
+
__typename: 'SomeWorkPayload',
76
+
success: true,
77
+
timeSpent: 100,
78
+
},
79
+
}"
80
+
/>
81
+
```
82
+
83
+
### update
84
+
85
+
When you execute a mutation, you modify back-end data. If that data is also present in your [Apollo Client cache](https://www.apollographql.com/docs/react/caching/cache-configuration/), you might need to update your cache to reflect the result of the mutation.
86
+
87
+
If a mutation modifies multiple entities, or if it creates or deletes entities, the Apollo Client cache is not automatically updated to reflect the result of the mutation. To resolve this, you can include an `update` function.
88
+
89
+
The purpose of an update function is to modify your cached data to match the modifications that a mutation makes to your back-end data.
90
+
91
+
See [updating cache after mutation](https://www.apollographql.com/docs/react/data/mutations/#updating-the-cache-after-a-mutation)
92
+
93
+
```vue
94
+
<template>
95
+
<ApolloMutation
96
+
:update="update"
97
+
/>
98
+
</template>
99
+
100
+
<script>
101
+
export default {
102
+
methods: {
103
+
update(cache, { data: { addTodo } }) {
104
+
cache.modify({
105
+
fields: {
106
+
todos(existingTodos = []) {
107
+
const newTodoRef = cache.writeFragment({
108
+
data: addTodo,
109
+
fragment: gql`
110
+
fragment NewTodo on Todo {
111
+
id
112
+
type
113
+
}
114
+
`,
115
+
})
116
+
return [...existingTodos, newTodoRef]
117
+
},
118
+
},
119
+
})
120
+
},
121
+
},
122
+
}
123
+
</script>
124
+
```
125
+
126
+
### refetchQueries
127
+
128
+
In some cases, just using `dataIdFromObject` is not enough for your application UI to update correctly. For example, if you want to add something to a list of objects without refetching the entire list, or if there are some objects that to which you can't assign an object identifier, Apollo Client cannot update existing queries for you. Read on to learn about the other tools at your disposal.
129
+
130
+
`refetchQueries` is the simplest way of updating the cache. With `refetchQueries` you can specify one or more queries that you want to run after a mutation is completed in order to refetch the parts of the store that may have been affected by the mutation.
131
+
132
+
See [refetching queries after mutation](https://www.apollographql.com/docs/react/caching/advanced-topics/#updating-after-a-mutation)
-`mutate(options = undefined)`: Function to call the mutation. You can override the mutation options (for example: `mutate({ variables: { foo: 'bar } })`)
41
-
-`loading`: Boolean indicating that the request is in flight
42
-
-`error`: Eventual error for the last mutation call
0 commit comments