Skip to main content
Version: 3.x

Extend the GraphQL schema

This guide explains how to customize the GraphQL schema to add your own domain objects

When developing an e-commerce store, you might at some point need to expose new data in your unified GraphQL schema to support new features and allow frontend developers to use them. Front-Commerce’s GraphQL modules is the mechanism allowing to extend and override any part of the schema defined by other modules.

The Front-Commerce core and platform integrations (such as Magento2) are implemented as GraphQL modules. They leverage features from the GraphQL Schema Definition Language (SDL).

This page will guide you through the process of exposing a new feature in your GraphQL schema. We will create an extension with a GraphQL module that allows to maintain a counter of clicks for a product.

Create an extension

To extend the GraphQL schema and implement your own logic, you first have to create an extension. An extension can be created by adding a new folder under extensions and by creating a index.ts file (the extension definition) with the following content:

example-extensions/click-counter/index.ts
import { defineExtension } from "@front-commerce/core";

export default function clickCounter() {
return defineExtension({
name: "click-counter",
meta: import.meta,
});
}

Then you need to register the extension into your Front-Commerce project.

Add a GraphQL module within the extension

For now, the click-counter extension does not provide any feature. To extend the GraphQL schema and implement our own logic, we need to add a GraphQL module to the extension containing a schema and some resolvers.

Adding custom scalars to the schema

Since version 3.6.x

To add custom scalars to your schema, you can define them in your GraphQL module as shown below.

import { createGraphQLModule } from "@front-commerce/core/graphql";
export default createGraphQLModule({
namespace: "example-module",
typeDefs: /** GraphQL */ `
Query {
acmeEntry(identifier: AcmeScalar!): String!
}
`,
scalars: {
AcmeScalar: "string | number",
},
});