website/src/pages/plugins/typescript/typescript-vue-apollo-smart-ops.mdx
import { PluginApiDocs, PluginHeader } from '@/components/plugin' import { pluginGetStaticProps } from '@/lib/plugin-get-static-props' export const getStaticProps = pluginGetStaticProps(__filename, { hasOperationsNote: true })
<PluginHeader /> <PluginApiDocs />Using the generated query code.
For the given input:
query Messages($type: FeedType!) {
feed(type: $type) {
id
}
}
We can use the generated code like this:
<template>
<div>
<div v-if="loading > 0">Loading…</div>
<ul v-else>
<li v-for="message in messages">{{ message.id }}</li>
</ul>
</div>
</template>
<script lang="ts">
import { useMessagesQuery } from '../generated/graphqlOperations'
export default {
apollo: {
messages: useMessagesQuery({
// variables will be correctly typed here!
variables: {
type: 'HOT'
},
loadingKey: 'loading',
update: data => data.feed
})
},
data() {
return {
messages: null,
loading: 0
}
}
}
</script>
For the given operation:
mutation CreateMessage($text: String!) {
createMessage(text: $text) {
id
}
}
We can use the generated code like this:
<template>
<div>
<textarea v-model="text"></textarea>
<button @click="send">Send Message</button>
</div>
</template>
<script lang="ts">
import { createMessageMutation } from '../generated/graphqlOperations'
export default {
data() {
return {
text: ''
}
},
async send() {
const result = await createMessageMutation(this, {
variables: {
text: this.text
}
})
if (!result.success || !result.data) {
alert('Failed to create message')
return
}
const messageId = result.data.createMessage.id
}
}
</script>