diff --git a/docs/Docs.md b/docs/Docs.md index a46cf9c5e..16aaaa8ef 100644 --- a/docs/Docs.md +++ b/docs/Docs.md @@ -652,6 +652,8 @@ console.log(identity.receivedClaims) // [Claim(type: 'name', value: 'Alice'), .. ```graphql query { identity(did: "did:example:123") { + name: latestClaimValue(type: "name") + profilePicture: latestClaimValue(type: "profilePicture") receivedClaims { type value diff --git a/packages/daf-core/src/entities/identity.ts b/packages/daf-core/src/entities/identity.ts index a4f44428f..1ead0dbc2 100644 --- a/packages/daf-core/src/entities/identity.ts +++ b/packages/daf-core/src/entities/identity.ts @@ -69,4 +69,28 @@ export class Identity extends BaseEntity { claim => claim.subject, ) receivedClaims: Claim[] + + /** + * Convenience method + * + * const name = await identity.getLatestClaimValue({type: 'name'}) + * + * @param where + */ + async getLatestClaimValue(where: { type: string }): Promise { + const claim = await Claim.findOne({ + where: { + ...where, + subject: this.did, + }, + order: { + issuanceDate: 'DESC', + }, + }) + return claim?.value + } + + shortDid() { + return `${this.did.slice(0, 15)}...${this.did.slice(-4)}` + } } diff --git a/packages/daf-core/src/graphql/graphql-core.ts b/packages/daf-core/src/graphql/graphql-core.ts index e3847d46d..ed0d75360 100644 --- a/packages/daf-core/src/graphql/graphql-core.ts +++ b/packages/daf-core/src/graphql/graphql-core.ts @@ -151,8 +151,9 @@ export const resolvers = { }, Identity: { - shortId: async (identity: Identity) => `${identity.did.slice(0, 15)}...${identity.did.slice(-4)}`, - profileImage: async (identity: Identity) => null, + shortDid: async (identity: Identity) => (await Identity.findOne(identity.did)).shortDid(), + latestClaimValue: async (identity: Identity, args: { type: string }) => + (await Identity.findOne(identity.did)).getLatestClaimValue({ type: args.type }), sentMessages: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['sentMessages'] })).sentMessages, receivedMessages: async (identity: Identity) => @@ -263,8 +264,8 @@ export const typeDefs = ` extend type Identity { - shortId: String! - profileImage: String + shortDid: String! + latestClaimValue(type: String): String sentMessages: [Message] receivedMessages: [Message] issuedPresentations: [Presentation]