I’m working on a project where I need to connect two Airtable databases through Gridsome. I have a Books table that contains an “authors” field with the ID of the person who wrote each book. The authors have their own separate table with detailed information. I want to fetch both the book data and the related author information in my GraphQL queries. How can I set up these relationships properly?
My gridsome.config.js setup:
module.exports = {
siteName: 'Book Library',
plugins: [
{
use: '@gridsome/source-airtable',
options: {
apiKey: 'keyAbc123XyZ789',
baseId: 'appDef456Uvw012',
tableName: 'Books',
typeName: 'BookItem',
}
},
{
use: '@gridsome/source-airtable',
options: {
apiKey: 'keyAbc123XyZ789',
baseId: 'appDef456Uvw012',
tableName: 'Authors',
typeName: 'Authors',
}
},
],
templates: {
Authors: '/author/:id'
}
}
Home.vue component:
<template>
<Layout>
<h1>Book Collection</h1>
<BookDisplay
v-for="book in $page.allBookItem.edges"
:key="book.node.id"
:book="book"
/>
</Layout>
</template>
<page-query>
query {
allBookItem {
edges {
node {
id
title
author
summary
genre
available
covers {
thumbnails {
large {
url
width
height
}
}
}
}
}
}
}
</page-query>
<script>
import BookDisplay from '~/components/BookDisplay';
export default {
components: {
BookDisplay
}
}
</script>
components/BookDisplay.vue:
<template>
<v-app>
<v-card
class="mx-auto my-12"
max-width="374"
>
<v-img
height="250"
:src="book.node.covers[0].thumbnails.large.url"
></v-img>
<v-card-title>{{ book.node.title }}</v-card-title>
<v-card-text>
<v-row
align="center"
class="mx-0"
>
<v-rating
:value="4.2"
color="amber"
dense
half-increments
readonly
size="14"
></v-rating>
<div class="grey--text ml-4">4.2 (285)</div>
</v-row>
<div class="my-4 subtitle-1 black--text">
{{ book.node.summary }}
</div>
<g-link to="/">View Author Details</g-link>
</v-card-text>
<v-divider class="mx-4"></v-divider>
<v-card-title>Current Status</v-card-title>
</v-card>
</v-app>
</template>
<script>
export default {
name: 'BookDisplay',
props: {
book: {
type: Object,
required: true,
},
},
}
</script>
templates/Author.vue:
<template>
<h1>{{ $page.authors.name }}</h1>
</template>
<page-query>
query Author($id: ID!) {
authors(id: $id) {
name
id
biography
}
}
</page-query>
Any guidance would be really helpful. Thanks in advance!