Tina Docs
Introduction
Overview
Introduction To TinaCMS
Getting Started
Using the Tina Editor
FAQ
Core Concepts
Content Modeling
Data Fetching
Visual Editing
Querying Content
Overview
Writing custom queries
Editing
Overview
Markdown & MDX
Block-based editing
Single Document Collections
Customizing Tina
Overview
Validation
Custom Field Components
Custom List Rendering
Format and Parse Input
Filename Customization
Before Submit function
Going To Production
Overview
Tina Cloud
Self-Hosted
Drafts
Overview
Draft Fields
Editorial Workflow
Guides
Overview
Framework Guides
Separate Content Repo
Querying Tina Content at Runtime
Internationalization
Migrating From Forestry
Further Reference
Overview
Config
Schema
The "tina" folder
The TinaCMS CLI
Media
Search
Content API
Tina's edit state
The "tinaField" helper
Self-Hosted Components

For most simple cases, you should be able to leverage the Tina Client's generated queries to do your data fetching:

const myPost = await client.queries.post({ relativePath: 'HelloWorld.md' })

In more advanced cases, you may want more control on the underlying GraphQL query. In these cases you have two options:

  • Extending the auto-generated client.queries by writing a custom query.
  • Use client.request, and write an inline query.

Extending the auto-generated client.queries

The client can be extended to perform more advanced queries (including querying multiple root collections at once), by adding queries to the tina/queries directory. All files that end in gql or graphql in this directory will be added to the client.

Tina generates custom GraphQL fragments to be used in custom queries so that the queries will not have to be updated when the schema is updated.

Note: These fragments can be seen in tina/__generated__/frags.gql

Example of tina/queries/postWithNav.gql:

query postWithNav($relativePath: String!) {
nav(relativePath: "nav.json") {
...NavParts
}
post(relativePath: $relativePath) {
...PostParts
}
}

To get autocomplete in your GraphQL files add a graphql.config.js that points to tina/__generated__/schema.gql and tina/__generated__/frags.gql.

Note: fragments are named by using the collection name (capitalized) followed by "Parts"

We are using a PostParts & NavParts Fragment here. For each collection, this fragment is generated and updated when the schema is updated. You can view any available Fragments for your schema in /tina/__generated__/frags.gql.

Once the query is added, the client will have its types updated so that it can be used to request the new query.

import { client } from '../[pathToTina]/tina/__generated__/client'
// Use the client to perform data fetching
// Here, it fetches a single "post" item
const myPost = await client.queries.postWithNav({
relativePath: 'HelloWorld.md',
})
console.log(myPost.title)
console.log(myPost.nav.items)

Writing inline queries

If you want to avoid using the types on the client altogether, you can also just write an inline query using the client.request function.

import { client } from '../[pathToTina]/tina/__generated__/client'
// Use the client to perform data fetching
// Here, it fetches a single "post" item
const myPost = await client.request({
query: `query getPost($relativePath: String!) {
post(relativePath: $relativePath) {
title
body
}
`,
variables: { relativePath: 'hello-world.md' },
})
console.log(myPost.title)

For more information on writing custom queries, check out the querying reference docs.

Last Edited: August 13, 2024

Previous
Querying content

Product

Showcase
TinaCloud
Introduction
How Tina Works
Roadmap

Resources

Blog
Examples
Support
Media

Whats New
TinaCMS
TinaCloud
Use Cases
Agencies
Documentation
Teams
Jamstack CMS
Benefits
MDX
Markdown
Git
Editorial Workflow
Customization
SEO
Comparisons
TinaCMS vs Storyblok
TinaCMS vs Sanity
TinaCMS vs DecapCMS
TinaCMS vs Contentful
TinaCMS vs Builder.io
TinaCMS vs Strapi
Integrations
Astro
Hugo
NextJS
Jekyll