2759825251
* * created vuepress docs * chore: upgrade deps * docs: patreon link * Docs changes
4.5 KiB
4.5 KiB
Components
Query components
::: warning WIP
You can use the ApolloQuery (or apollo-query) component to make watched Apollo queries directly in your template:
:::
<ApolloQuery
:query="require('../graphql/HelloWorld.gql')"
:variables="{ name }"
>
<template slot-scope="{ result: { loading, error, data } }">
<!-- Loading -->
<div v-if="loading" class="loading apollo">Loading...</div>
<!-- Error -->
<div v-else-if="error" class="error apollo">An error occured</div>
<!-- Result -->
<div v-else-if="data" class="result apollo">{{ data.hello }}</div>
<!-- No result -->
<div v-else class="no-result apollo">No result :(</div>
</template>
</ApolloQuery>
Props:
query: GraphQL query (transformed bygraphql-tag)variables: Object of GraphQL variablesfetchPolicy: See apollo fetchPolicypollInterval: See apollo pollIntervalnotifyOnNetworkStatusChange: See apollo notifyOnNetworkStatusChangecontext: See apollo contextskip: Boolean disabling query fetchingclientId: Used to resolve the Apollo Client used (defined in ApolloProvider)deep: Boolean to use deep Vue watcherstag: String HTML tag name (default:div)
Scoped slot props:
result: Apollo Query resultresult.data: Data returned by the queryresult.loading: Boolean indicating that a request is in flightresult.error: Eventual error for the current resultresult.networkStatus: See apollo networkStatusresult.times: number of times the result was updated
query: Smart Query associated with the componentisLoading: Smart Query loading stategqlError: first GraphQL error if any
Events:
result(resultObject)error(errorObject)
::: warning WIP
You can subscribe to more data with the ApolloSubscribeToMore (or apollo-subscribe-to-more) component:
:::
<template>
<ApolloQuery :query="...">
<ApolloSubscribeToMore
:document="require('../gql/MessageAdded.gql')"
:variables="{ channel }"
:updateQuery="onMessageAdded"
/>
<!-- ... -->
</ApolloQuery>
</template>
<script>
export default {
data () {
return {
channel: 'general',
}
},
methods: {
onMessageAdded (previousResult, { subscriptionData }) {
// The previous result is immutable
const newResult = {
messages: [...previousResult.messages],
}
// Add the question to the list
newResult.messages.push(subscriptionData.data.messageAdded)
return newResult
},
},
}
</script>
You can put as many of those as you want inside a <ApolloQuery> component.
Mutation component
::: warning WIP
You can use the ApolloMutation (or apollo-mutation) component to call Apollo mutations directly in your template:
:::
<ApolloMutation
:mutation="require('@/graphql/userLogin.gql')"
:variables="{
email,
password,
}"
@done="onDone"
>
<template slot-scope="{ mutate, loading, error }">
<button :disabled="loading" @click="mutate()">Click me</button>
<p v-if="error">An error occured: {{ error }}</p>
</template>
</ApolloMutation>
Props:
mutation: GraphQL query (transformed bygraphql-tag)variables: Object of GraphQL variablesoptimisticResponse: See optimistic UIupdate: See updating cache after mutationrefetchQueries: See refetching queries after mutationtag: String HTML tag name (default:div)
Scoped slot props:
mutate(options = undefined): Function to call the mutation. You can override the mutation options (for example:mutate({ variables: { foo: 'bar } }))loading: Boolean indicating that the request is in flighterror: Eventual error for the last mutation callgqlError: first GraphQL error if any
Events:
done(resultObject)error(errorObject)