I’m working on a project using Gridsome with Airtable as my data source. I have two separate tables in Airtable - one for books and another for authors. The books table contains an “author” field that stores the ID of the author from the authors table.
I need help figuring out how to create a GraphQL query that can fetch both the book information and the related author details using that ID connection. Right now I can only get data from one table at a time.
Here’s my current setup:
gridsome.config.js
module.exports = {
siteName: 'Book Library',
plugins: [
{
use: '@gridsome/source-airtable',
options: {
apiKey: 'keyAbc123XYZ789',
baseId: 'appDef456GHI012',
tableName: 'Books',
typeName: 'Book',
}
},
{
use: '@gridsome/source-airtable',
options: {
apiKey: 'keyAbc123XYZ789',
baseId: 'appDef456GHI012',
tableName: 'Authors',
typeName: 'Author',
}
},
],
templates: {
Author: '/author/:id'
}
}
pages/Index.vue
<template>
<Layout>
<h1>My Book Collection</h1>
<BookItem
v-for="book in $page.allBook.edges"
:key="book.node.id"
:book="book"
/>
</Layout>
</template>
<page-query>
query {
allBook {
edges {
node {
id
title
author
summary
genre
available
covers {
thumbnails {
large {
url
width
height
}
}
}
}
}
}
}
</page-query>
<script>
import BookItem from '~/components/BookItem'
export default {
components: {
BookItem
}
}
</script>
components/BookItem.vue
<template>
<div class="book-card">
<img :src="book.node.covers[0].thumbnails.large.url" alt="Book cover" />
<h3>{{ book.node.title }}</h3>
<p>{{ book.node.summary }}</p>
<g-link to="/">View Author Details</g-link>
</div>
</template>
<script>
export default {
name: 'BookItem',
props: {
book: {
type: Object,
required: true,
},
},
}
</script>
templates/Author.vue
<template>
<div>
<h1>{{ $page.author.name }}</h1>
<p>{{ $page.author.biography }}</p>
</div>
</template>
<page-query>
query Author($id: ID!) {
author(id: $id) {
name
id
biography
}
}
</page-query>
Any suggestions would be really helpful!