Adding Integration Fields
Learn how to create and use Prismic Integration Fields to connect your Front-Commerce data with Prismic CMS, enabling content managers to seamlessly reference your application's data in their content.
What are Integration Fields?
Integration fields are a Prismic feature that allows content managers to reference external data in their content. In Front-Commerce, this means they can select products, categories, or any other data from your application directly in the Prismic writing room.
Key benefits:
- Content managers can build landing pages with real product data without leaving Prismic
- Developers can access the referenced data through GraphQL, reusing existing fragments and components
- Changes in your application's data are automatically reflected in Prismic
Understanding Integration Fields
Front-Commerce provides two main ways to implement Integration Fields:
Custom Integration Fields
The IntegrationField
class is the foundation for creating custom integration
fields. It allows you to:
- Define how to fetch and paginate your data
- Transform your data into Prismic's expected format
- Add custom metadata through the
blob
property - Control pagination behavior
interface UserData {
userId: string;
name: string;
email: string;
updated_at: string;
avatar_url?: string;
}
const userIntegrationField = new IntegrationField<UserData>({
// Function to fetch paginated results from your data source
loadPullResults: async (page, shopUrl) => {
const users = await fetchUsers(page);
return {
result_size: users.total,
// By default `autoPaginate` is true, so we don't need to paginate manually
// If you prefer to paginate manually, set `autoPaginate` to false.
// Prismic is limited to 50 results per page.
results: users.items.map((user) => ({
id: user.userId,
title: user.name,
description: user.email,
image_url: user.avatar_url
? new URL(user.avatar_url, shopUrl).toString()
: undefined,
last_update: new Date(user.updated_at).valueOf(),
blob: user, // Original data available in resolveEntity
})),
};
},
// Function to resolve the entity in your GraphQL context
resolveEntity: (blob, context) => {
return context.loaders.User.load(blob.userId);
},
// Optional: control pagination behavior of results.
// By default `autoPaginate` is true, so we don't need to paginate manually
// If you prefer to paginate manually, set `autoPaginate` to false
autoPaginate: true,
});
Sitemap Integration Fields
The SitemapIntegrationField
is a specialized implementation that leverages
your existing sitemap data. See our
Customize the sitemap guide
for details. It's perfect for exposing products, categories, and other URL-based
content:
const productIntegrationField = new SitemapIntegrationField<
MagentoSitemapProduct,
Product
>({
// Name of your sitemap fetcher
sitemapFetcher: "products",
// Convert sitemap entry to Prismic's format
convertSitemapEntry: (entry, shopUrl) => {
if (!entry.data) return null;
let imageUrl = entry.images?.[0];
if (imageUrl) {
imageUrl = new URL(imageUrl, shopUrl).toString();
}
return {
id: entry.data.sku,
title: entry.data.name,
description: entry.data.short_description,
image_url: imageUrl,
last_update: new Date(entry.data.updated_at).valueOf(),
};
},
// Resolve the entity in your GraphQL context
resolveEntity: (blob, context) => {
return context.loaders.Product.load(blob.id);
},
});
Implementation Guide
Testing Your Integration
You can test your integration field endpoint using curl:
- Product
- Category
curl --user 'your-webhook-secret:' http://localhost:4000/prismic/integration/Product
curl --user 'your-webhook-secret:' http://localhost:4000/prismic/integration/Product?page=2
curl --user 'your-webhook-secret:' http://localhost:4000/prismic/integration/Category
curl --user 'your-webhook-secret:' http://localhost:4000/prismic/integration/Category?page=2
Integration fields are limited to 50 results per page. Implement proper
pagination in your loadPullResults
method if you have more data.
The Sitemap Integration Field automatically paginates for you.
Troubleshooting
If your integration isn't working:
- Enable debug logs with
DEBUG=front-commerce:prismic:integration-fields
- Verify your endpoint URL and access token
- Check the response format matches Prismic's Custom API Format
- Ensure your resolvers correctly handle the
blob
data
Next Steps
- Learn how to customize the Prismic WYSIWYG
- Explore adding content slices
- Understand using the resolver cache