apollo: I guess that SSR doesn't work

versions

nuxt: 2.10.0 @nuxtjs/apollo: 4.0.0-rc15

On my Nuxt page component, i wrote following codes

import gql from 'graphql-tag'
import axios from 'axios'
import formatData from '../my/format/func'

export default {
  apollo: {
    forms: {
      query: gql`
        Myquery
      `,
      fetchPolicy: 'cache-first',
      manual: true,
      result({ data, loading }) {
        console.log('log: result')
        if (!loading) {
          console.log('log: result not loading')
          this.myData = formatData(data)
        }
      }
    }
  },
  async asyncData({ app, params, error }) {
    console.log('log: asyncData')
    const { data } = await axios.get('my.api.com/service', { id: params.id })
    if (!data) {
      error({statusCode: 404, message: 'NotFound'})
    }
    return {
      service: data
    }
  },
  data() {
    return {
      myData: null
    }
  },
  created() {
    console.log('log: created')
  },
  mounted() {
    console.log('log: mounted')
  },
}

On the server side log, i expected log: result is printed before log: created like following logs:

log: asyncData
log: result
log: result not loading
log: created

But i got:

log: asyncData
log: created
log: result
log: result not loading

And on the client side browser console, i expected log: result is not printed. But i got:

log: result // i think it is cache log
log: result not loading // i think it is cache log
log: created
log: mounted
log: result
log: result not loading

I don’t know what is wrong. I guess apollo smart query is always excuted on the client side. Did i use wrong fetchPolicy?

<div align="right">This question is available on Nuxt community (#c255)</div>

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 1
  • Comments: 15

Most upvoted comments

@kieusonlam Just so I know if this is supposed to work how I think it should:

apollo: {
    itineraries: {
      prefetch: true,
      query: gql`
        query {
          itineraries {
            name
            hotel
            checkIn
            checkOut
          }
        }
      `
    }
  }

With prefetch: true, should the SSR block until the query completes and the data is set?

I’m having trouble understanding what the difference between prefetch: true or using asyncData to run a query is, or when they should be used.

In fact, prefetch is working properly on initial load. But not when navigating to a page using nuxt-link Could there be an issue on nuxt-link?

I’m having the same issue, can’t make prefetch work, so I’m using asyncData now.

nuxt: 2.14.0 vue: 2.6.12 vue-server-renderer: 2.6.12 vue-template-compiler: 2.6.12 @nuxtjs/apollo: 4.0.1-rc.3

I’m still seeing the same issues as noted above for this, with smart queries working on initial load but not navigation. Is a bug or just how smart queries with Apollo?

Same issue here, is there any way to use smart query with SSR by using normal syntax? #352 showing that 4.0.1-rc1 is working, will try it works.