Migrate to the recommended plugin
How to migrate from the 'gatsby-source-prismic-graphql' plugin to the officially supported 'gatsby-source-prismic' plugin.
Both plugins work with Prismic, but the setup and usage are different. Lets quickly review the differences:
- Fetches data using Prismic's REST
- Can be used along with Gatsby Cloud
- The Link Resolver and the HTML Serializer are added inside the plugin configuration
gatsby-source-prismic-graphql
- Fetches data using Prismic's GraphQL API
- The Link Resolver and the HTML Serializer are added manually to the Rich Text field.
First, delete the cache and node_modules directory; either manually or by running the following command:
rm -rf node_modules/ .cache
Then install the new plugin using the following command:
- npm
- yarn
npm install gatsby-source-prismic
yarn add gatsby-source-prismic
Open the gatsby-config.js file, replace the plugin config and configure it according to your project. Here is an example of what the config should look like:
⚠️ Replace the example config
Remember that you will need to update this with the repository name, access token, Link Resolver, etc., to match your project. This configuration is mere, for example, purposes.
const linkResolver = require('./example-route-to-linkResolver')
module.exports = {
plugins: [
{
resolve: 'gatsby-source-prismic',
options: {
repositoryName: 'example-repository-name',
accessToken: 'example-access-token',
releaseID: 'example-id',
linkResolver: () => (doc) => linkResolver(doc),
schemas: {
page: require('./custom_types/example_type.json'),
},
lang: '*',
prismicToolbar: true,
},
},
]
}
✔️ Test the queries
Open the GraphQL playground tool at http://localhost:8000/__graphql after launching your project to test and discover the types and properties of your GraphQL schema.
Take a look at the differences in the Graphql query syntax; in both cases, we call all documents of the type Page, filtering them by their UID and language. We retrieve UID, notes, and one Slice with the name "Embed".
gatsby-source-prismic:
query MyPages($uid: String, $lang: String) {
allPrismicPage(uid: {eq: $uid}, lang: {eq: $lang}) {
edges {
node {
uid
data {
example_text {
html
text
}
body {
...on PrismicPageBodyExampleSlice {
slice_type
}
}
}
}
}
}
}
gatsby-source-prismic-graphql:
query MyPages($uid: String, $lang: String) {
prismic {
allPages(uid: $uid, lang: $lang) {
edges {
node {
_meta {
uid
}
example_text
body {
... on PRISMIC_PageBodyExampleSlice {
type
}
}
}
}
}
}
}
In the gatsby-source-prismic plugin, you'll often need to go one level deep after the field's API ID.
Some fields remain the same in both plugins. Here are the fields that require no changes:
- Date
- Timestamp
- Color
- Key Text
- Select
- Number
Fields that have a different structure are:
- Title
- Rich Text
- Content Relationship
- Link and Link to media
- Embed
- Geopoint
- Group
- Image
- and general document metadata fields.
Take a look at the differences listed below so that you know what to change in your code.
Example API IDs
Note: In this example, the fields' API ID is the same as the field itself with the prefix example_ appended at the beginning. e.g., Embed field = example_embed .
gatsby-source-prismic: To query a doc's metadata, add the items at the top level of the node key.
allPrismicPost {
edges {
node {
id
uid
type
lang
}
}
}
gatsby-source-prismic-graphql: Go one level down to the _meta key.
prismic {
allPosts {
edges {
node {
_meta {
id
uid
type
lang
}
}
}
}
}
gatsby-source-prismic: Specify the values you need: text, html, and raw. (you'll usually only use raw).
example_title {
text
html
raw {...}
}
gatsby-source-prismic-graphql: Add the API ID of the field.
example_title
gatsby-source-prismic: Specify each value you're going to use; in this case, we're calling url, alt, dimensions, and the responsive thumbnail views of the image.
example_image {
url
alt
dimensions {
width
height
}
thumbnail {...}
}
gatsby-source-prismic-graphql: Add the API ID of the field.
example_image
In this example, we query a Content Relationship field that links to a doc of the type event, and we're retrieving its uid, type, and lang.
gatsby-source-prismic:
example_content_relationship {
document {
... on PrismicExampleType {
uid
type
lang
}
}
}
gatsby-source-prismic-graphql:
example_content_relationship {
...on PRISMIC_ExampleType {
_meta{
uid
type
lang
}
}
}
example_link {
link_type
url
}
gatsby-source-prismic-graphql: Use a Union type in order to specify which fields we need for each link type.
example_link {
__typename
...on PRISMIC__ExternalLink {
url
}
...on PRISMIC__ImageLink {
url
size
}
...on PRISMIC_Event {
title
_meta {
uid
id
type
}
}
}
The embed field has numerous values that can vary depending on the type of embed we add.
gatsby-source-prismic: Specify each of the available values that you want to retrieve.
example_embed {
type
url
{...}
}
gatsby-source-prismic-graphql: Add the API ID of the field.
example_embed
example_geopoint {
latitude
longitude
}
gatsby-source-prismic-graphql: Add the API ID of the field.
example_geopoint
The field group's usage remains almost the same in both plugins; you still need to call the necessary fields that the group contains. In this example, the group has a Title field.
gatsby-source-prismic:
example_group {
example_title {
html
text
}
}
gatsby-source-prismic-graphql:
example_group {
example_title
}
Example of how you'd access the retrieved data for each plugin.
gatsby-source-prismic:
const data = data.allPrismicPage.edges[0]
gatsby-source-prismic-graphql:
const data = data.prismic.allPages.edges[0]