Skip to content

Commit

Permalink
fix: added FiterMap type
Browse files Browse the repository at this point in the history
  • Loading branch information
kolhapuresatyajeet authored and alainncls committed Oct 11, 2023
1 parent df10866 commit 9e4efca
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
4 changes: 2 additions & 2 deletions sdk/src/dataMapper/BaseDataMapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PublicClient } from "viem";
import { ApolloClient, gql } from "@apollo/client/core";
import { Conf } from "../types";
import { Conf, FilterMap } from "../types";
import { stringifyWhereClause } from "../utils/apolloClientHelper";

export default abstract class BaseDataMapper<T> {
Expand All @@ -25,7 +25,7 @@ export default abstract class BaseDataMapper<T> {
return queryResult.data;
}

async findBy(whereClause: Partial<T>) {
async findBy<T extends keyof FilterMap>(whereClause: Partial<FilterMap[T]>) {
const queryResult = await this.apolloClient.query<Array<T>>({
query: gql(`query GetBy { ${this.typeName}s(where: ${stringifyWhereClause(whereClause)}) ${this.gqlInterface} }`),
});
Expand Down
17 changes: 15 additions & 2 deletions sdk/src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export interface Conf {
export type Attestation = {
attestationId: string; // The unique identifier of the attestation.
schemaId: string; // The identifier of the schema this attestation adheres to.
schemaId_in: string[];
replacedBy: string | null; // Whether the attestation was replaced by a new one.
Address: string; // The address issuing the attestation to the subject.
Address: string; // The id of the portal that created the attestation.
Expand All @@ -23,7 +22,6 @@ export type Attestation = {
revoked: boolean; // Whether the attestation is revoked or not.
subject: string; // The ID of the attestee, EVM address, DID, URL etc.
attestationData: string; // The attestation data.
attestationData_contains: string;
};

export type Schema = {
Expand All @@ -48,3 +46,18 @@ export type Module = {
name: string; // The name of the module.
description: string; // A description of the module.
};

export type FilterMap = {
Attestation: FilterAttestation;
Module: FilterModule;
Schema: FilterSchema;
Portal: FilterPortal;
};

export type FilterAttestation = Attestation & { schemaId_in: string[]; attestationData_contains: string };

export type FilterModule = Module;

export type FilterSchema = Schema;

export type FilterPortal = Portal;
30 changes: 27 additions & 3 deletions sdk/test/dataMapper/AttestationDataMapper.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import AttestationDataMapper from "../../src/dataMapper/AttestationDataMapper";
import VeraxSdk from "../../src/VeraxSdk";
import { createPublicClient, PublicClient, http } from "viem";
import { ApolloClient, InMemoryCache, ApolloQueryResult, gql } from "@apollo/client/core";
//TODO : This is a basic test example. mock data and assertions should be more precise
import { createPublicClient, http, PublicClient } from "viem";
import { ApolloClient, ApolloQueryResult, gql, InMemoryCache } from "@apollo/client/core";
import { Constants } from "../../src/utils/constants";

describe("AttestationDataMapper", () => {
let mockApolloClient: ApolloClient<object>;
let web3Client: PublicClient;
Expand Down Expand Up @@ -66,4 +67,27 @@ describe("AttestationDataMapper", () => {
});
});
});

describe("getRelatedAttestations", () => {
it("should return the expected attestations", async () => {
const attestationId = "0x0000000000000000000000000000000000000000000000000000000000000001";
// Mock the behavior of the query method
const queryMock = jest.spyOn(mockApolloClient, "query");
queryMock.mockResolvedValueOnce({
data: {
attestations: { result: "success" },
},
} as ApolloQueryResult<unknown>);

const result = await attestationDataMapper.getRelatedAttestations(attestationId);

// Assert
expect(result).toMatchObject({ attestations: { result: "success" } });
expect(mockApolloClient.query).toHaveBeenCalledWith({
query: gql(
`query GetBy { ${typeName}s(where: {attestationData_contains:"${attestationId}",schemaId_in:["${Constants.RELATIONSHIP_SCHEMA_ID}","${Constants.NAMED_GRAPH_RELATIONSHIP_SCHEMA_ID}"]}) ${gqlInterface} }`,
),
});
});
});
});

0 comments on commit 9e4efca

Please sign in to comment.