Introduction
EverShop provides a powerful GraphQL API that allows you to query and manipulate data in your e-commerce application. The GraphQL implementation follows a modular architecture where each extension can define its own types, queries, and mutations.Architecture
Modular Type System
GraphQL types and resolvers in EverShop are organized within extensions, following this structure:Type Definition Files
Type definitions use the GraphQL Schema Definition Language (SDL) and are stored in.graphql files:
Resolver Files
Resolvers are JavaScript/TypeScript files that implement the logic for fetching and transforming data:Type Extension Pattern
EverShop supports extending existing GraphQL types using theextend type directive. This is particularly useful for adding custom fields to core types like Product.
Resolver Context
Resolvers receive three arguments:The parent object. For top-level resolvers (Query, Mutation), this is typically undefined.
The arguments passed to the field in the GraphQL query.
A shared context object available to all resolvers. Contains request information, database connections, and authentication data.
Field Resolvers
Field resolvers define how individual fields are resolved from their parent object:Field resolvers are optional. If not defined, GraphQL will use the default resolver which returns the property with the same name from the parent object.
Creating Custom Types
To create a new GraphQL type in your extension:-
Create a directory for your type:
-
Define your type schema in
[TypeName].graphql: -
Implement resolvers in
[TypeName].resolvers.js:
Data Fetching in Page Components
Page components can fetch GraphQL data using thequery export:
Best Practices
Use Strong Types
Define explicit types for all fields. Use non-null types (
!) for required fields.Avoid N+1 Queries
Consider using DataLoader or batching strategies for related data fetching.
Keep Resolvers Simple
Move complex business logic to separate service functions.
Document Your Schema
Use GraphQL descriptions to document types and fields.
Error Handling
Resolvers should throw errors when operations fail:Next Steps
GraphQL Queries
Explore available queries and learn how to fetch data
GraphQL Mutations
Learn how to create mutations for data modification