Graphweaver allows users to connect many data sources together. Many datasources are included as a part of our open source repo and a few are a part of our enterprise repo. You may need to create your own data integration and extend the Graphweaver’s functionality. In this case, follow these instructions:
Create a Data Provider
The base provider defines how data providers interact with Graphweaver. You will need to extend this class to implement your new provider. CRUD methods are defined here, and you will need to implement them in your class.
src/packages/core/src/base-provider.ts
export class Provider<D, G> implements BackendProvider<D, G> {
constructor(readonly backendId: string) {
if (!backendId) throw new Error('BackendId must be defined');
}
// READ METHODS
public async find(
filter: Filter<G>,
pagination?: PaginationOptions,
additionalOptionsForBackend?: any
): Promise<D[]> {
throw new Error('Find not implemented.');
}
Your provider will accept the generic DataEntity and GraphQLEntity (from graphweaver/core). It will include a private property of your base entity for the service you’re integrating with. A constructor method with the backendId calls the provider class with the entity that is passed in. You will implement the CRUD methods in the Graphweaver Base Provider. Those methods will call the service that interfaces with the new data source you’re integrating with.
export class YourBackendProvider<
D extends BaseDataEntity,
G extends GraphQLEntity<D>
> extends Provider<D, G> {
private entity: YourEntity<D>;
constructor(backendId: string, entity: YourEntity<D>) {
super(backendId);
this.entity = entity;
}
public async findOne(filter: Filter<G>): Promise<D | null> {
const yourService = new YourService(this.entity);
return yourService.getEntity(String(filter.id));
}
Base Provider Methods
The following methods are available on the BaseProvider. These are defined in src/packages/core/src/base-provider.ts
. Depending on your use case, you may implement all of some of these:
Read
find
find(
filter: Filter<G>,
pagination?: PaginationOptions,
additionalOptionsForBackend?: any
): Promise<D[]>;
find
accepts arguments for filtering, pagination, and additionalOptionalForBackend. find
returns an array of the entity you’re looking for.
findOne
-findOne(filter: Filter<G>): Promise<D | null>;
- Accepts a filter with the id of the entity you’re looking for and returns a single entity.findByRelatedId
findByRelatedId(
entity: any,
relatedField: string,
relatedIds: readonly string[],
filter?: Filter<G>
): Promise<D[]>;
findByRelatedId
gets entities by id that are related to the parent entity. This should call find
with the ids of the related entities you’re looking for.
Update
updateOne
-updateOne(id: string | number, updateArgs: Partial<G>): Promise<D>;
- Update an entity via the id with a specified payload.
updateMany
-updateMany(entities: (Partial<G> & WithId)[]): Promise<D[]>;
- Update multiple entities via an array of ids with a specified payload.
createOrUpdateMany
-createOrUpdateMany(entities: Partial<G>[]): Promise<D[]>;
- Create new entities or update existing ones from an array of entities. Graphweaver follows a pattern where if an id is provided you are updating that entity, and if it is not, you are creating that entity.
Create
createOne
-createOne(entity: Partial<G>): Promise<D>;
- Create an entity with the given payload, returns the entity.
createMany
-createMany(entities: Partial<G>[]): Promise<D[]>
- Create multiple entities from an array of entity payloads, returns the entities.
Delete
deleteOne
-deleteOne(filter: Filter<G>): Promise<boolean>;
- Delete the entity that fits the filter. Returns a boolean.
deleteMany
-deleteMany(ids: string[]): Promise<boolean>
- Delete the entities by matching ids. Returns a boolean.
Helper Methods
getByRelatedId
-getRelatedEntityId(entity: any, relatedIdField: string): string
- Gets the related id of an entity. Returns a string.
isCollection
-isCollection(entity: unknown): boolean
- Returns a boolean for determining if the given entity is a collection.