Expose the attribute in your GraphQL schema
This section follows the
Extend the GraphQL schema guide.
We won't go into too much detail here, so please refer to the guide if you need
more information.
We want to display the rate
of a product on the product page. To keep it
simple, it will only consist in displaying a number, which can be manually
edited by the merchant. This feature will be stored in a new extension called
ratings
.
Let's create a new extension along with its folder structure. At the root of
your repository, type the following command.
mkdir -p extensions/ratings
Create the extension
Let's create our extension :
import { defineExtension } from "@front-commerce/core";
export default function ratings() {
return defineExtension({
name: "ratings",
theme: "extensions/ratings/theme",
meta: import.meta,
});
}
And add the extension we just made to Front-Commerce. Edit the
front-commerce.config.ts
file (root folder) as shown below to do so.
import { defineConfig } from "@front-commerce/core/config";
import themeChocolatine from "@front-commerce/theme-chocolatine";
import storesConfig from "./app/config/stores";
import magento2 from "@front-commerce/magento2";
import ratings from "extensions/ratings";
export default defineConfig({
extensions: [themeChocolatine(), magento2({ storesConfig }), ratings()],
});
Extend the Product definition
First, we need to tell the server how to customise the GraphQL schema. To do so,
we will create graphQL module and extend Product
to add our rate
.
extensions/ratings/graphql/index.ts
import { createGraphQLModule } from "@front-commerce/core/graphql";
export default createGraphQLModule({
namespace: "Ratings",
typeDefs: `
extend type Product {
rate: Float
}
`,
});
Implement the resolver
We must now define what Front-Commerce should fetch when rate
is requested in
a GraphQL query; in other words we must write the code that will resolve the
queries for the rates.
Create a file named runtime.ts
and type the following code. It implements the
runtime with its resolvers.
extensions/ratings/graphql/runtime.ts
import { createGraphQLRuntime } from "@front-commerce/core/graphql";
export default createGraphQLRuntime({
resolvers: {
Product: {
rate: ({ rate }) => parseFloat(rate),
},
},
});
And load the runtime in the graphQL module :
extensions/ratings/graphql/index.ts
import { createGraphQLModule } from "@front-commerce/core/graphql";
export default createGraphQLModule({
namespace: "Ratings",
typeDefs: `
extend type Product {
rate: Float
}
`,
loadRuntime: () => import("./runtime"),
});
Declare the module
If we want our code to be taken into account when the module is loaded, we must
reference it.
import { defineExtension } from "@front-commerce/core";
export default function ratings() {
return defineExtension({
name: "ratings",
theme: "extensions/ratings/theme",
meta: import.meta,
graphql: {
modules: [ratingsModule],
},
});
}
Discover the playground
By typing yourhostname/graphql
(in our case localhost:4000/graphql
) in your
browser address bar, you can access a GraphQL playground with a nice GUI to test
your queries.
You will need to restart the application to access the updated schema in the
playground.
http://localhost:4000/graphql
{
product(sku: "yourSKUhere") {
name
rate
}
}
From this GraphQL query, you should get a JSON content that looks like this:
{
"data": {
"product": {
"name": "Your product name",
"rate":
}
}
}