Saleor
Published in

Saleor

Combining Headless Architectures with Saleor and Strapi

In this post, you’ll learn how Saleor and Strapi work together to create a performant and adaptable storefront. We’ll cover how you can use the power of GraphQL to combine headless architectures, such as a CMS and Commerce API, and how to combine the schemas of multiple GraphQL APIs.

What is Saleor?

Saleor Commerce

What is Strapi?

What are the benefits of combining headless architectures?

Combining Saleor and Strapi

Combining Saleor and Strapi
import { ApolloServer } from 'apollo-server-micro';
import { stitchSchemas } from '@graphql-tools/stitch';
import { delegateToSchema } from '@graphql-tools/delegate';
import { RenameTypes, RenameRootFields } from '@graphql-tools/wrap';

import createRemoteSchema from '../../utils/createRemoteExecutor';

// Configuration for Next.js API Routes
export const config = {
api: {
bodyParser: false,
},
};

// Export as a Next.js API Route
export default async (req, res) => {
// Setup subschema configurations
const productsSubschema = ``
const cmsSubschema = ``

// Build the combined schema and set up the extended schema and resolver
const schema = stitchSchemas({
subschemas: [productsSubschema, cmsSubschema],
});

// Set up the GraphQL server
const apolloServer = new ApolloServer({ schema });
const apolloServerHandler = apolloServer.createHandler({
path: '/api/graphql',
});

// Return the GraphQL endpoint
return apolloServerHandler(req, res);
};
// Setup subschema configurations
const productsSubschema = await createRemoteSchema({
url: // Your Saleor GraphQL endpoint
});

const cmsSubschema = await createRemoteSchema({
url: // Your Strapi GraphQL endpoint,
transforms: [
new RenameRootFields(
(operationName, fieldName, fieldConfig) => `strapi_${fieldName}`,
),
new RenameTypes((name) => `Strapi_${name}`),
],
});
// Build the combined schema and set up the extended schema and resolver
const schema = stitchSchemas({
subschemas: [productsSubschema, cmsSubschema],
typeDefs: `
extend type Product {
cmsMetaData: [Strapi_Products]!
}
`,
resolvers: {
Product: {
cmsMetaData: {
selectionSet: `{ id }`,
resolve(product, args, context, info) {
// Get the data for the extended type from the subschema for Strapi
return delegateToSchema({
schema: cmsSubschema,
operation: 'query',
fieldName: 'strapi_products',
args: { where: { saleorId: product.id } },
context,
info,
});
},
},
},
},
});

Summary

--

--

A GraphQL-first, headless e-commerce platform for perfectionists. Written in Python. Best served as a bespoke, high-performance e-commerce solution.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Roy Derks (@gethackteam)

Roy is an entrepreneur, speaker and author from The Netherlands. Most recently he wrote the books Fullstack GraphQL and React Projects.