I am Django developer so for the first time I got my hands on vue and graphql, I don't know how exactly to deal with this error.
Here is my code, probably something is wrong in my query,
<template>
<section>
<div class="home">
<h2>hiii</h2>
<div v-for="i in allposts" :key="i.id">
<ul>
<li>
<h3>hey</h3>
<strong>{{id}}</strong>:
<span>{{title}}</span>
</li>
</ul>
</div>
</div>
</section>
</template>
<script>
import gql from "graphql-tag";
const PostQuery = gql`
query allposts {
allPosts {
id
title
}
}
`;
export default {
props: [],
data() {
return {
allposts: []
};
},
apollo: {
allposts: PostQuery
}
};
</script>
I can see the data is fetched successfully
Can anyone please guide me what I am doing wrong here?
Thanks in advance.
Came here via Google. Checked out the docs, which explain the problem, and how to solve it: https://apollo.vuejs.org/guide/apollo/queries.html#name-matching
Please note that a common beginner's mistake is to use a data name different from the field name in the query.
The example they give:
apollo: {
world: gql`query {
hello
}`
}
world
and hello
don't match, so you'd get an error.
The name has to either match, or as the docs suggest, you can provide an update
function:
apollo: {
world: {
query: gql`query {
hello
}`,
update: data => data.hello
}
}
Or, rename the field in the GraphQL document:
apollo: {
world: gql`query {
world: hello
}`
}
Okay so it turns out to be a silly mistake, API returns a result that has a key called allPosts, with a capital P, but my local data property and the Apollo property allposts, with a small p.
After making them all P everything works fine now.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With