GraphQL API
Relevant source files
The following files were used as context for generating this wiki page:- packages/core/src/graphql/orders/orders.types.graphql
- packages/core/src/graphql/products/products.types.graphql
- packages/core/src/graphql/warehouses/warehouses.types.graphql
- packages/core/src/graphql/users/users.types.graphql
- packages/core/src/app.module.ts
- packages/core/graphql.config.json
- packages/shop-web-gatsby/gatsby-config.js
- packages/shop-mobile-expo/src/client/products/queries.ts
- README.md
The Ever Demand platform is a headless commerce framework that exposes a rich GraphQL API for its various front-end applications. This API serves as the primary interface for all client applications, including the Admin UI, mobile apps, and web storefronts, to interact with the backend services. It provides a flexible and efficient way to query and manipulate data related to products, orders, users, and more.
Built using NestJS and Apollo Server, the GraphQL API is a core component of the platform's real-time, reactive architecture. It aggregates data from different domains and exposes it through a single, unified endpoint. This headless approach allows for independent development and deployment of various client experiences while maintaining a consistent data access layer.
Sources: README.md:31, packages/core/README.md:1, packages/core/src/app.module.ts:153-162
Architecture and Setup
The GraphQL API is implemented within the @ever-platform/core package. It leverages the @nestjs/graphql module to integrate an Apollo Server into the NestJS application.
Sources: packages/core/src/app.module.ts:10, packages/core/package.json:96
Module Initialization
The ApplicationModule imports and configures the GraphQLModule. The configuration specifies that all .graphql files within the project structure should be loaded to build the schema. It also enables the GraphQL Playground for development and debugging, and sets up the context for each request.
// packages/core/src/app.module.ts:153-162
GraphQLModule.forRoot({
typePaths: ['./**/*.graphql'],
installSubscriptionHandlers: true,
debug: !env.isProd,
playground: true,
context: ({ req, res }) => ({
req,
}),
}),
Sources: packages/core/src/app.module.ts:153-162
Schema Generation
The server manually creates the executable GraphQL schema by loading all .graphql type definition files, merging them, and then combining them with resolvers. The loadFilesSync and mergeTypeDefs functions from @graphql-tools are used for this purpose. This process allows for a modular schema definition, where each feature (like products, orders, etc.) can define its own types, queries, and mutations in separate files.
This diagram illustrates the schema generation process on the server.
API Endpoint
The GraphQL API is exposed at a single endpoint across all environments. Client applications and development tools are configured to communicate with this endpoint for schema introspection and operations (queries/mutations).
| Property | Value | Description |
|---|---|---|
| URL | http://localhost:8443/graphql | The default local URL for the GraphQL endpoint. |
| Method | POST | The HTTP method used for requests. |
| Introspection | true | postIntrospectionQuery is enabled for IDE support and tooling. |
Sources: packages/core/graphql.config.json:5-24, packages/admin-web-angular/graphql.config.json:5-24, packages/shop-mobile-ionic/graphql.config.json:5-24
Core Data Models
The API defines several core data types that represent the main entities of the e-commerce platform.
Entity Relationship Diagram
This diagram shows the relationships between the main entities in the GraphQL schema.
Product
The Product type represents an item that can be sold.
| Field | Type | Description |
|---|---|---|
id | String! | Unique identifier for the product. |
title | [LocalizedString!] | The localized name of the product. |
description | [LocalizedString!] | The localized description of the product. |
images | [ProductImage!] | A list of images associated with the product. |
categories | [String!] | Categories the product belongs to. |
Sources: packages/core/src/graphql/products/products.types.graphql:1-17
Order
The Order type represents a customer's request to purchase products.
| Field | Type | Description |
|---|---|---|
id | String! | Unique identifier for the order. |
orderNumber | String! | A publicly visible order number. |
customerId | String! | The ID of the user who placed the order. |
products | [OrderProduct!] | A list of products included in the order. |
status | String | The current status of the order (e.g., 'CREATED', 'PAID'). |
total | Float | The total cost of the order. |
Sources: packages/core/src/graphql/orders/orders.types.graphql:1-21
User
The User type represents a customer or other user of the platform.
| Field | Type | Description |
|---|---|---|
id | String! | Unique identifier for the user. |
firstName | String | The user's first name. |
lastName | String | The user's last name. |
email | String | The user's email address. |
phone | String | The user's phone number. |
Sources: packages/core/src/graphql/users/users.types.graphql:1-15
Warehouse
The Warehouse type represents a location or merchant that stocks products.
| Field | Type | Description |
|---|---|---|
id | String! | Unique identifier for the warehouse. |
name | String! | The name of the warehouse. |
email | String | Contact email for the warehouse. |
logo | String | URL for the warehouse's logo. |
Sources: packages/core/src/graphql/warehouses/warehouses.types.graphql:1-11
Queries and Mutations
The API provides queries for fetching data and mutations for creating or modifying data.
Example Queries
| Query | Return Type | Description |
|---|---|---|
product(id: String!) | Product | Fetches a single product by its ID. |
products(findInput: ProductsFindInput) | [Product] | Fetches a list of products based on filter criteria. |
orders(findInput: OrderFindInput) | [Order] | Fetches a list of orders. |
users(findInput: UserFindInput) | [User] | Fetches a list of users. |
Sources: packages/core/src/graphql/products/products.types.graphql:32-33, packages/core/src/graphql/orders/orders.types.graphql:62, packages/core/src/graphql/users/users.types.graphql:27
Example Mutations
| Mutation | Return Type | Description |
|---|---|---|
createOrder(createInput: OrderCreateInput!) | Order | Creates a new order. |
updateProduct(id: String!, updateInput: ProductUpdateInput!) | Product | Updates an existing product. |
Sources: packages/core/src/graphql/orders/orders.types.graphql:63, packages/core/src/graphql/products/products.types.graphql:34
Client-Side Integration
Various client applications in the monorepo are configured to consume this GraphQL API.
Client Query Flow
This sequence diagram shows a mobile client fetching a list of products.
Gatsby Web Client
The Gatsby-based web shop uses the gatsby-source-graphql plugin to fetch data from the GraphQL endpoint at build time and runtime. The endpoint URL is configured via an environment variable.
// packages/shop-web-gatsby/gatsby-config.js:25-33
{
resolve: 'gatsby-source-graphql',
options: {
// This type will contain remote schema Query type
typeName: 'Ever',
// This is field under which it's accessible
fieldName: 'ever',
// Url to query from
url: process.env.GQL_ENDPOINT || 'http://localhost:8000/___graphql',
},
},
Sources: packages/shop-web-gatsby/gatsby-config.js:25-33
Expo Mobile Client
The React Native mobile app (built with Expo) uses @apollo/client to define and execute GraphQL queries. Queries are defined using the gql template literal tag.
// packages/shop-mobile-expo/src/client/products/queries.ts:6-33
export const PRODUCTS_QUERY: TypedDocumentNode = gql`
query ProductsQuery(
$existedProductsIds: [String]
$findInput: ProductsFindInput
$pagingOptions: PagingOptionsInput
) {
products(
existedProductsIds: $existedProductsIds
findInput: $findInput
pagingOptions: $pagingOptions
) {
id
title {
locale
value
}
description {
locale
value
}
# ... other fields
}
}
`;
Sources: packages/shop-mobile-expo/src/client/products/queries.ts:6-33