Search code examples
vue.jsgraphqlapollo-clientquasar-frameworkvue-apollo

vue apollo 4 not returning graphql query results


In an effort to use version 4 of Vue apollo in cooperation with the composition api as shown in this tutorial we cannot seem to get any result from a standard graphql query.

A part of our setup:

// src/boot/apollo.ts
import { createHttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { ApolloClient } from 'apollo-client'
import { DefaultApolloClient } from '@vue/apollo-composable'
import { provide } from '@vue/composition-api'

const httpLink = createHttpLink({ uri: "http://localhost:5000/graphql", })
const cache = new InMemoryCache()
const apolloClient = new ApolloClient({ link: httpLink, cache, })

export default ({ app }) => {
  app.setup = () => { provide(DefaultApolloClient, apolloClient) }
}

Now trying to use a perfectly valid query that does return results in the graphql playground returns undefined for allAcounts:

// src/pages/Index.vue
<template>
  <q-page padding>
    <h2 v-if="loading">Loading...</h2>
    <h4 v-else>Done loading</h4>
    <div v-for="account in allAccounts" :key="account.accountIdentifier">
      {{ account.accountIdentifier }}
    </div>
  </q-page>
</template>

<script lang="ts">
import { defineComponent, watch } from '@vue/composition-api'
import { useQuery, useResult } from '@vue/apollo-composable'
import gql from 'graphql-tag'

export default defineComponent({
  setup() {
    const allAccountsQuery = gql`
      query allAccounts {
        accounts {
          accountIdentifier
        }
      }
    `
    const { result, loading } = useQuery(allAccountsQuery)
    console.log('loading ', loading) // false
    console.log('result ', result)   // undefined
    const allAccounts = useResult(result)

    return { onClick, profile, allAccounts, loading }
  },
})
</script>

It only returns Done loading but not a single account is returned. Although the content is correctly visible through the graphql playground:

enter image description here


Solution

  • Apparently this is a known issue with vue-apollo 4 not being compatible with any version higher than "@vue/composition-api": "0.5.0". So downgrading fixed the issue temporarily until vue-apollo 4 will merge the fix.

    yarn add @vue/composition-api@0.5.0