GraphQL and Nuxt

Debbie O'Brien - Apr 12 '20 - - Dev Community

We need to use the graphql-tag to be able to add our query so first we import this

import gql from 'graphql-tag'
Enter fullscreen mode Exit fullscreen mode

We can then make our query listing all the data that we want to receive and ordering by what we prefer. Here we just added an export const called workshop and make it equal to our gql tag which has a query called workshops and that queries the workshops table.

export const workshops = gql`
  query workshops {
    workshops(order_by: { date: desc }) {
      date
      title
      year
    }
  }
`
Enter fullscreen mode Exit fullscreen mode

Then we need to use apollo so that we can get our data to our template. Don't forget to first install '@nuxtjs/apollo', and then add to the modules of our next config.

apollo: {
    $loadingKey: 'loading',
    workshops: {
      query: workshops,
    },
  },
Enter fullscreen mode Exit fullscreen mode

And now we can do a v-for over all our data and print the title for example

<div v-for="(workshop, index) in workshops" :key="index" class="flex">
  <p>{{workshop.title}}</p>
</div>  
Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player