Skip to content

Commit

Permalink
fix: Eager relations
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat authored and Jason Healy committed Mar 19, 2020
1 parent 9faa883 commit da07cb2
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 41 deletions.
12 changes: 6 additions & 6 deletions packages/daf-core/src/entities/claim.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ import {
Column,
BaseEntity,
ManyToOne,
JoinTable,
PrimaryColumn,
OneToMany,
ManyToMany,
BeforeInsert,
} from 'typeorm'
import { Identity } from './identity'
import { Credential } from './credential'
Expand All @@ -19,13 +15,17 @@ export class Claim extends BaseEntity {

@ManyToOne(
type => Identity,
identity => identity.issuedPresentations,
identity => identity.issuedPresentations, {
eager: true
}
)
issuer: Identity

@ManyToOne(
type => Identity,
identity => identity.receivedPresentations,
identity => identity.receivedPresentations, {
eager: true
}
)
subject: Identity

Expand Down
9 changes: 6 additions & 3 deletions packages/daf-core/src/entities/credential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import {
Column,
BaseEntity,
ManyToOne,
JoinTable,
PrimaryColumn,
OneToMany,
ManyToMany,
BeforeInsert,
} from 'typeorm'
import { Identity } from './identity'
import { Message } from './message'
Expand Down Expand Up @@ -63,6 +61,7 @@ export class Credential extends BaseEntity {
identity => identity.issuedCredentials,
{
cascade: ['insert'],
eager: true
},
)
issuer: Identity
Expand All @@ -72,6 +71,7 @@ export class Credential extends BaseEntity {
identity => identity.receivedCredentials,
{
cascade: ['insert'],
eager: true
},
)
subject: Identity
Expand Down Expand Up @@ -103,6 +103,9 @@ export class Credential extends BaseEntity {
)
presentations: Presentation[]

@ManyToMany(type => Message)
@ManyToMany(
type => Message,
message => message.credentials
)
messages: Message[]
}
11 changes: 7 additions & 4 deletions packages/daf-core/src/entities/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
ManyToOne,
ManyToMany,
PrimaryGeneratedColumn,
OneToMany,
JoinTable,
CreateDateColumn,
UpdateDateColumn,
Expand Down Expand Up @@ -72,6 +71,7 @@ export class Message extends BaseEntity {
{
nullable: true,
cascade: ['insert'],
eager: true
},
)
from?: Identity
Expand All @@ -82,6 +82,7 @@ export class Message extends BaseEntity {
{
nullable: true,
cascade: ['insert'],
eager: true
},
)
to?: Identity
Expand All @@ -99,9 +100,11 @@ export class Message extends BaseEntity {
@JoinTable()
presentations: Presentation[]

@ManyToMany(type => Credential, {
cascade: true,
})
@ManyToMany(
type => Credential,
credential => credential.messages,
{ cascade: true }
)
@JoinTable()
credentials: Credential[]

Expand Down
2 changes: 2 additions & 0 deletions packages/daf-core/src/entities/presentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export class Presentation extends BaseEntity {
identity => identity.issuedPresentations,
{
cascade: ['insert'],
eager: true
},
)
issuer: Identity
Expand All @@ -44,6 +45,7 @@ export class Presentation extends BaseEntity {
identity => identity.receivedPresentations,
{
cascade: ['insert'],
eager: true
},
)
audience: Identity
Expand Down
61 changes: 33 additions & 28 deletions packages/daf-core/src/graphql/graphql-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const messages = async (
}},
) => {
const options = {
relations: ['from', 'to'],
where: {}
}

Expand All @@ -54,7 +53,7 @@ const claims = async (
}},
) => {
const options = {
relations: ['issuer', 'subject', 'credential'],
relations: ['credential'],
where: {}
}

Expand All @@ -69,41 +68,47 @@ const claims = async (
}

export const resolvers = {
Mutation: {
saveNewMessage,
},

Query: {
messages,
message: async (_: any, { id }) => Message.findOne(id),
identity: async (_: any, { did }) => Identity.findOne(did),
credential: async (_: any, { hash }) => Credential.findOne(hash),
identities: async () => Identity.find(),
claims,
},

Identity: {
sentMessages: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['sentMessages', 'sentMessages.from', 'sentMessages.to']})).sentMessages,
receivedMessages: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['receivedMessages', 'receivedMessages.from', 'receivedMessages.to']})).receivedMessages,
issuedPresentations: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['issuedPresentations', 'issuedPresentations.issuer', 'issuedPresentations.audience']})).issuedPresentations,
receivedPresentations: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['receivedPresentations', 'receivedPresentations.issuer', 'receivedPresentations.audience']})).receivedPresentations,
issuedCredentials: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['issuedCredentials', 'issuedCredentials.issuer', 'issuedCredentials.subject']})).issuedCredentials,
receivedCredentials: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['receivedCredentials', 'receivedCredentials.issuer', 'receivedCredentials.subject']})).receivedCredentials,
issuedClaims: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['issuedClaims', 'issuedClaims.issuer', 'issuedClaims.subject']})).issuedClaims,
receivedClaims: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['receivedClaims', 'receivedClaims.issuer', 'receivedClaims.subject']})).receivedClaims,
sentMessages: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['sentMessages']})).sentMessages,
receivedMessages: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['receivedMessages']})).receivedMessages,
issuedPresentations: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['issuedPresentations']})).issuedPresentations,
receivedPresentations: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['receivedPresentations']})).receivedPresentations,
issuedCredentials: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['issuedCredentials']})).issuedCredentials,
receivedCredentials: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['receivedCredentials']})).receivedCredentials,
issuedClaims: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['issuedClaims']})).issuedClaims,
receivedClaims: async (identity: Identity) => (await Identity.findOne(identity.did, { relations: ['receivedClaims']})).receivedClaims,
},

Credential: {
claims: async (credential: Credential) => Claim.find({ where: { credential: credential.hash}, relations: ['issuer', 'subject', 'credential'] }),
messages: async (credential: Credential) => (await Credential.findOne(credential.hash, { relations: ['messages', 'messages.from', 'messages.to']}))?.messages,
presentations: async (credential: Credential) => (await Credential.findOne(credential.hash, { relations: ['presentations', 'presentations.issuer', 'presentations.audience']})).presentations,
claims: async (credential: Credential) => Claim.find({ where: { credential: credential.hash}, relations: ['credential'] }),
messages: async (credential: Credential) => (await Credential.findOne(credential.hash, { relations: ['messages']})).messages,
presentations: async (credential: Credential) => (await Credential.findOne(credential.hash, { relations: ['presentations']})).presentations,
},

Presentation: {
credentials: async (presentation: Presentation) => (await Presentation.findOne(presentation.hash, { relations: ['credentials', 'credentials.issuer', 'credentials.subject']})).credentials,
messages: async (presentation: Presentation) => (await Presentation.findOne(presentation.hash, { relations: ['messages', 'messages.from', 'messages.to']})).messages
credentials: async (presentation: Presentation) => (await Presentation.findOne(presentation.hash, { relations: ['credentials']})).credentials,
messages: async (presentation: Presentation) => (await Presentation.findOne(presentation.hash, { relations: ['messages']})).messages
},

Message: {
presentations: async (message: Message) => (await Message.findOne(message.id, { relations: ['presentations', 'presentations.issuer', 'presentations.audience']})).presentations,
credentials: async (message: Message) => (await Message.findOne(message.id, { relations: ['credentials', 'credentials.issuer', 'credentials.subject']})).credentials,
},
Query: {
messages,
message: async (_: any, { id }) => Message.findOne(id, { relations: ['from', 'to'] }),
identity: async (_: any, { did }) => Identity.findOne(did),
credential: async (_: any, { hash }) => Credential.findOne(hash, { relations: ['issuer', 'subject'] }),
identities: async () => Identity.find(),
claims,
},
Mutation: {
saveNewMessage,
presentations: async (message: Message) => (await Message.findOne(message.id, { relations: ['presentations']})).presentations,
credentials: async (message: Message) => (await Message.findOne(message.id, { relations: ['credentials']})).credentials,
},


}

export const typeDefs = `
Expand Down

0 comments on commit da07cb2

Please sign in to comment.