-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #294 from us3r-network/F-composites-bufan
F-composites-bufan
- Loading branch information
Showing
12 changed files
with
741 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
164 changes: 164 additions & 0 deletions
164
packages/client/dashboard/src/api/compositeSDK/S3CompositeModel.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
Oops, something went wrong.