Skip to content

Commit

Permalink
Merge pull request #294 from us3r-network/F-composites-bufan
Browse files Browse the repository at this point in the history
F-composites-bufan
  • Loading branch information
sin-bufan authored Jan 25, 2024
2 parents ca5351e + 6bd9bfa commit df3034c
Show file tree
Hide file tree
Showing 12 changed files with 741 additions and 121 deletions.
18 changes: 9 additions & 9 deletions packages/client/dashboard/src/api/composite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ApiResp } from '.'
import { APP_API_URL } from '../constants'
import {
ClientDApp,
CompositeInput,
DappCompositeDto,
Network,
} from '../types.d'
Expand Down Expand Up @@ -36,7 +37,7 @@ export function createDappComposites({
}: {
did: string
dapp: ClientDApp
data: { name: string, gqlSchema: PassedSchema, composite: Composite, runtimeDefinition: RuntimeCompositeDefinition }
data: CompositeInput
}) {
let host = APP_API_URL
return axios({
Expand All @@ -45,7 +46,7 @@ export function createDappComposites({
headers: {
'did-session': did,
},
data: { graphql: data.gqlSchema.code, name: data.name, composite: data.composite, runtimeDefinition: data.runtimeDefinition },
data,
})
}

Expand Down Expand Up @@ -97,12 +98,14 @@ export function getComposites({
pageSize = PAGE_SIZE,
pageNumber = 1,
network,
published = true,
}: {
name?: string
did?: string
pageSize?: number
pageNumber?: number
network?: Network
published?:boolean
}): AxiosPromise<ApiResp<DappCompositeDto[]>> {
let host = APP_API_URL

Expand All @@ -112,6 +115,7 @@ export function getComposites({
data: {
pageSize,
pageNumber,
published,
},
headers: {
'did-session': did || '',
Expand Down Expand Up @@ -141,12 +145,10 @@ export function postComposite({
did,
id,
data,
name,
}: {
did: string
id: string
data: string
name: string
id: number
data: CompositeInput
}) {
let host = APP_API_URL
return axios({
Expand All @@ -155,7 +157,7 @@ export function postComposite({
headers: {
'did-session': did,
},
data: { graphql: data, name },
data,
})
}

Expand All @@ -175,5 +177,3 @@ export function deleteComposite({
},
})
}

export { PAGE_SIZE }
164 changes: 164 additions & 0 deletions packages/client/dashboard/src/api/compositeSDK/S3CompositeModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/**
* How to use this model:
*
* export const CERAMIC_TESTNET_HOST = "https://gcp-ceramic-testnet-dev.s3.xyz";
* const compositeModel = new S3CompositeModel(CERAMIC_TESTNET_HOST);
*
* // auth with didSession
* compositeModel.authComposeClient(didSession);
*
* // createNew
* const resp = await compositeModel.createComposite({...});
*
* // update
* const resp = await compositeModel.updateComposite({...});
*
* // queryList
* const resp = await compositeModel.queryCompositeIndex({first: 100, after: ""});
*
* // queryWithId
* const resp = await compositeModel.queryCompositeWithId("...");
*
*/

import { ComposeClient } from "@composedb/client";
import { RuntimeCompositeDefinition } from "@composedb/types";
import { DIDSession } from "did-session";
import type { CeramicApi } from "@ceramicnetwork/common";
import { DID } from "dids";
import { Page } from "@ceramicnetwork/common";

import { definition } from "./runtime-composite";

import {
Composite,
CreateCompositeInput,
CreateCompositePayload,
UpdateCompositeInput,
UpdateCompositePayload,
Scalars,
} from "./graphql";

export class S3CompositeModel {
composeClient: ComposeClient;

constructor(ceramic: CeramicApi | string) {
this.composeClient = new ComposeClient({
ceramic: ceramic,
definition: definition as unknown as RuntimeCompositeDefinition,
});
}

public authComposeClient(session: DIDSession) {
if (!session || (session.hasSession && session.isExpired)) {
throw new Error("Please login with wallet first!");
}
this.composeClient.setDID(session.did);
}

public resetComposeClient() {
const did = new DID();
this.composeClient.setDID(did);
}

async createComposite(input: CreateCompositeInput) {
const mutation = `
mutation createComposite($input: CreateCompositeInput!) {
createComposite(input: $input) {
document {
id
}
}
}
`;
const resp = await this.composeClient.executeQuery<{
createComposite: CreateCompositePayload;
}>(mutation, {
input: {
content: {
...input.content,
},
},
});
return resp;
}

async updateComposite(input: UpdateCompositeInput) {
const mutation = `
mutation($input: UpdateCompositeInput!) {
updateComposite(input: $input) {
document {
id
}
}
}
`;
const resp = await this.composeClient.executeQuery<{
updateComposite: UpdateCompositePayload;
}>(mutation, {
input: {
id: input.id,
content: {
...input.content,
},
},
});

return resp;
}

async queryCompositeIndex({
first = 100,
after = "",
}: {
first: number;
after?: string;
}) {
const query = `
query {
compositeIndex(first: ${first}, after: "${after}") {
edges {
node {
id
# other fields
}
}
pageInfo {
hasNextPage
hasPreviousPage
endCursor
startCursor
}
}
}
`;

const resp = await this.composeClient.executeQuery<{
compositeIndex: Page<Composite>;
}>(query);

return resp;
}

async queryCompositeWithId(id: Scalars["ID"]["input"]) {
const query = `
query($id: ID!) {
node(id: $id) {
... on Composite {
id
# other fields
}
}
}
`;

const resp = await this.composeClient.executeQuery<{
node: Composite;
}>(query, {
id,
});

return resp;
}
}

Loading

0 comments on commit df3034c

Please sign in to comment.