Skip to content

Commit

Permalink
feat: As user, I want to use the SDK to return all attestations that …
Browse files Browse the repository at this point in the history
…have a relationship to a specific attestation (#271)
  • Loading branch information
alainncls authored Oct 11, 2023
1 parent 2a56f53 commit c9283aa
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 8 deletions.
17 changes: 15 additions & 2 deletions sdk/examples/attestation/attestationExamples.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@ import VeraxSdk from "../../src/VeraxSdk";

export default class AttestationExamples {
private veraxSdk: VeraxSdk;

constructor(_veraxSdk: VeraxSdk) {
this.veraxSdk = _veraxSdk;
}

async run(methodName: string = "") {
if (methodName.toLowerCase() == "findOneById".toLowerCase() || methodName == "")
if (methodName.toLowerCase() == "findOneById".toLowerCase() || methodName == "") {
console.log(
await this.veraxSdk.attestation.findOneById(
"0x00000000000000000000000000000000000000000000000000000000000007b5",
),
);
if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "")
}

if (methodName.toLowerCase() == "findBy".toLowerCase() || methodName == "") {
console.log(
await this.veraxSdk.attestation.findBy({
schemaId: "0xd1664d97bd195df77e3d5fe78c1737ab3adaa38bbe52a680d1aa30fa51f186ba",
}),
);
}

if (methodName.toLowerCase() == "getRelatedAttestations".toLowerCase() || methodName == "") {
console.log(
await this.veraxSdk.attestation.getRelatedAttestations(
"0x0000000000000000000000000000000000000000000000000000000000000001",
),
);
}
}
}
8 changes: 8 additions & 0 deletions sdk/src/dataMapper/AttestationDataMapper.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BaseDataMapper from "./BaseDataMapper";
import { Attestation } from "../types";
import { Constants } from "../utils/constants";

export default class AttestationDataMapper extends BaseDataMapper<Attestation> {
typeName = "attestation";
Expand All @@ -19,4 +20,11 @@ export default class AttestationDataMapper extends BaseDataMapper<Attestation> {
schemaString
decodedData
}`;

async getRelatedAttestations(id: string) {
return this.findBy({
attestationData_contains: id,
schemaId_in: [Constants.RELATIONSHIP_SCHEMA_ID, Constants.NAMED_GRAPH_RELATIONSHIP_SCHEMA_ID],
});
}
}
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: 16 additions & 1 deletion sdk/src/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Chain, Address } from "viem";
import { Address, Chain } from "viem";

export interface Conf {
chain: Chain;
Expand Down Expand Up @@ -46,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;
5 changes: 5 additions & 0 deletions sdk/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class Constants {
static readonly RELATIONSHIP_SCHEMA_ID = "0x89bd76e17fd84df8e1e448fa1b46dd8d97f7e8e806552b003f8386a5aebcb9f0";
static readonly NAMED_GRAPH_RELATIONSHIP_SCHEMA_ID =
"0x5003a7832fa2734780a5bf6a1f3940b84c0c66a398e62dd4e7f183fdbc7da6ee";
}
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 c9283aa

Please sign in to comment.