Search code examples
vue.jsvuejs2graphqlapollovue-apollo

Vue Apollo - How to properly fetch data?


I'm having lot of problems with receiving movie data from this endpoint using vue-apollo.

Endpoint: http://movie-database-graphql.herokuapp.com/graphiql

And this is how I configure it (Vuetify for styling b.t.w):

main.js

import Vue from 'vue'
import App from './App.vue'

// Plugins
import './plugins/vuetify'
import VueApollo from 'vue-apollo'
import ApolloClient from 'apollo-boost'

const apolloClient = new ApolloClient({
  uri: 'http://movie-database-graphql.herokuapp.com/graphql'
});

const apolloProvider = new VueApollo({
  defaultClient: apolloClient,
})

Vue.use(VueApollo);

Vue.config.productionTip = false

new Vue({
  apolloProvider,
  render: h => h(App)
}).$mount('#app')

I believe the problem comes on how I call database through Apollo but not sure yet. I've tried many ways, following documentation and always fail.

MovieList.vue (Component with the query)

<template>
  <v-container fluid grid-list-md>
    <v-layout row wrap>
      <h4 v-if="$apollo.loading">Loading...</h4>
      <v-flex v-for="movie in movies" :key="movie.id" xs12 sm6>
          <v-card height="150px">
            <v-layout row>
              <v-avatar size="125" tile>
                <v-img :src="movie.poster_path" contain></v-img>
              </v-avatar>
              <v-flex xs12 sm6>
                <v-card-title primary-title>
                  <h3>{{ movie.title }}</h3>
                </v-card-title>
              </v-flex>
            </v-layout>
          </v-card>
      </v-flex>
    </v-layout>
  </v-container>
</template>

<script>
import gql from "graphql-tag"

export default {
  name: "MovieList",
  data: () => { return { movies: [] } },
  apollo: {
    movies: gql`
    query AllMoviesQuery {
        movies(query: "Start Wars") {
            id
            title
            poster_path
        }
    }`
  }
};
</script>

Do you understand what I'm missing? I don't receive any movie data (no errors shown...). movies array is alway empty


Solution

  • Just a typo -- the query argument in your request is:

    Start Wars
    

    instead of

    Star Wars
    

    If you use that same value inside GraphiQL, you'll also get empty results.