@symanticreative/vendure-admin-client
Version:
A TypeScript GraphQL client for Vendure Admin API to create custom dashboards
108 lines • 3.81 kB
JavaScript
/**
* Base repository implementation for GraphQL operations
* Implements common repository methods
*/
export class BaseRepository {
constructor(graphqlClient) {
this.graphqlClient = graphqlClient;
}
/**
* Find entity by ID
* @param id - Entity identifier
* @returns Promise resolving to the entity
*/
async findById(id) {
const variables = { id };
const query = this.getFindByIdQuery();
const resultPath = this.getFindByIdResultPath();
const result = await this.graphqlClient.query(query, variables);
return this.getResultByPath(result, resultPath);
}
/**
* Find all entities with optional filtering
* @param filter - Optional filter criteria
* @returns Promise resolving to array of entities
*/
async findAll(filter) {
const variables = { filter };
const query = this.getFindAllQuery();
const resultPath = this.getFindAllResultPath();
const result = await this.graphqlClient.query(query, variables);
return this.getResultByPath(result, resultPath);
}
/**
* Create a new entity
* @param entity - Entity data
* @returns Promise resolving to created entity
*/
async create(entity) {
const variables = { input: entity };
const mutation = this.getCreateMutation();
const resultPath = this.getCreateResultPath();
const result = await this.graphqlClient.mutate(mutation, variables);
return this.getResultByPath(result, resultPath);
}
/**
* Update an existing entity
* @param id - Entity identifier
* @param entity - Updated entity data
* @returns Promise resolving to updated entity
*/
async update(id, entity) {
const variables = { input: { id, ...entity } };
const mutation = this.getUpdateMutation();
const resultPath = this.getUpdateResultPath();
const result = await this.graphqlClient.mutate(mutation, variables);
return this.getResultByPath(result, resultPath);
}
/**
* Delete an entity
* @param id - Entity identifier
* @returns Promise resolving to boolean indicating success
*/
async delete(id) {
const variables = { id };
const mutation = this.getDeleteMutation();
const resultPath = this.getDeleteResultPath();
const result = await this.graphqlClient.mutate(mutation, variables);
return this.getResultByPath(result, resultPath);
}
/**
* Helper method to get result by path
* @param result - GraphQL result
* @param path - Path to result in the object
* @returns Extracted result
*/
getResultByPath(result, path) {
if (!path)
return result;
const pathParts = path.split('.');
let currentValue = result;
for (const part of pathParts) {
if (currentValue === undefined || currentValue === null) {
return null;
}
currentValue = currentValue[part];
}
return currentValue;
}
}
/**
* Base paginated repository implementation
* Extends base repository with pagination capabilities
*/
export class BasePaginatedRepository extends BaseRepository {
/**
* Find entities with pagination
* @param options - Pagination options
* @returns Promise resolving to paginated result
*/
async findWithPagination(options) {
const variables = { options };
const query = this.getFindWithPaginationQuery();
const resultPath = this.getFindWithPaginationResultPath();
const result = await this.graphqlClient.query(query, variables);
return this.getResultByPath(result, resultPath);
}
}
//# sourceMappingURL=base.repository.js.map