-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgraphql-mutation.html
114 lines (97 loc) · 2.68 KB
/
graphql-mutation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<link rel="import" href="graphql-query.html">
<!--
## GraphQL mutation
_Note: the current implementation for mutations is in draft, API might change in a future release._
The current implementation stays as close to the `<graphql-query>` as possible.
_Note: to use file upload via GraphQL you should import `build/apollo-client-subscription-file-upload.js`
instead of `build/apollo-client.js`:_
```html
<script src="bower_components/polymer-apollo-client/build/apollo-client-subscription-file-upload.js"></script>
```
```
<graphql-mutation id="contactMutation" variables="[[contactFormData]]" result="{{contactMutationResult}}">
mutation SubmitContactForm(
$name: String!,
$email: String!,
$phone: String!,
$subject: String!,
$message: String!
) {
createContactForm(
name: $name,
email: $email,
phone: $phone,
subject: $subject,
message: $message
) {
id
}
}
</graphql-mutation>
```
```js
//Somewhere after your button has called submit()
this.contactFormData = {} //fill the contactMutation with the correct data.
//It will give an error if not everything is filled in correctly.
this.$.contactMutation.validate()
this.$.contactMutation.execute().then((result) => {
//AMAZING RESULT, mutation has been submitted! 🎉
})
```
@group ApolloClient
@polymer
@customElement
@demo demo/full-demo.html Full demo
@demo demo/file-upload.html File upload
-->
<script>
class GraphQLMutation extends GraphQLQuery {
static get is() {
return 'graphql-mutation';
}
static get properties() {
return {
hold: {
value: true
},
hostLoading: {
type: Boolean,
value: false,
notify: true
}
};
}
/**
* Execute the query/mutation directly
* (used in combination with `hold` or with `<graphql-mutation>`).
*
* @return {Promise}
*/
execute(params) {
let validationResult = this.validate(params);
if (validationResult.error) {
console.error(validationResult.msg, this);
return;
}
this.hostLoading = true;
const mutation = this.query;
const { variables } = this;
const client = this._getClient();
if (!client) {
throw new Error(
'There is no GraphQL client available. ' +
'Initialize one on window.Apollo.client'
);
}
return client.mutate(Object.assign({ mutation, variables }, params))
.then((result) => {
this.hostLoading = false;
this.result = result.data;
})
.catch((error) => {
this._handleError(error);
});
}
}
customElements.define(GraphQLMutation.is, GraphQLMutation);
</script>