Skip to content

Commit

Permalink
feat: GQL queries WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Mar 18, 2020
1 parent 40041bb commit 698aca4
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 73 deletions.
18 changes: 9 additions & 9 deletions packages/daf-cli/src/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,23 @@ program
Daf.Gql.baseTypeDefs,
Daf.Gql.Core.typeDefs,
Daf.Gql.IdentityManager.typeDefs,
DataGql.typeDefs,
TG.Gql.typeDefs,
W3c.Gql.typeDefs,
SRD.Gql.typeDefs,
// DataGql.typeDefs,
// TG.Gql.typeDefs,
// W3c.Gql.typeDefs,
// SRD.Gql.typeDefs,
],
resolvers: merge(
Daf.Gql.Core.resolvers,
Daf.Gql.IdentityManager.resolvers,
DataGql.resolvers,
TG.Gql.resolvers,
W3c.Gql.resolvers,
SRD.Gql.resolvers,
// DataGql.resolvers,
// TG.Gql.resolvers,
// W3c.Gql.resolvers,
// SRD.Gql.resolvers,
),
context: () => ({ dataStore, core }),
introspection: true,
})
await core.setupServices()
// await core.setupServices()
const info = await server.listen({ port: cmd.port })
console.log(`🚀 Server ready at ${info.url}`)

Expand Down
4 changes: 2 additions & 2 deletions packages/daf-cli/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ export const initializeDb = async () => {
type: 'sqlite',
database: defaultPath + 'database-v2.sqlite',
synchronize: true,
logging: false,
logging: true,
entities: [...Daf.Entities],
})
}
export const dataStore = new DataStore()

core.on(Daf.EventTypes.validatedMessage, async (message: Daf.Message) => {
debug('New message %O', message)
await message.save()
// await message.save()
})
19 changes: 19 additions & 0 deletions packages/daf-core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,25 @@ export class Core extends EventEmitter {
return Promise.reject(unsupportedMessageTypeError)
}

public async saveNewMessage(input: { raw: string, metaDataType?: string, metaDataValue?: string }): Promise<Message> {
try {
const message = await this.validateMessage(
new Message({
raw: input.raw,
meta:{
type: input.metaDataType,
value: input.metaDataValue
}
})
)
await message.save()
return message
} catch (error) {
this.emit(EventTypes.error, error)
return Promise.reject(error)
}
}

public async handleAction(action: Action): Promise<any> {
if (!this.actionHandler) {
return Promise.reject('Action handler not provided')
Expand Down
71 changes: 63 additions & 8 deletions packages/daf-core/src/graphql/graphql-base-type-defs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,82 @@ export const baseTypeDefs = `
type Mutation
scalar Date
type Key {
kid: ID!
type: String!
publicKeyHex: String!
}
type Identity {
did: ID!
provider: String
controllerKeyId: String
keys: [Key]
}
type Message {
id: ID!
saveDate: Date!
updateDate: Date!
createdAt: Date
expiresAt: Date
threadId: String
rowId: String!
type: String!
sender: Identity
receiver: Identity
raw: String!
raw: String
data: String
timestamp: Int
metaData: [MessageMetaData]
thread: [Message]
replyTo: [String]
replyUrl: [String]
from: Identity
to: Identity
metaData: [MetaData]
presentations: [Presentation]
credentials: [Credential]
}
type MessageMetaData {
type MetaData {
type: String!
value: String
}
type Presentation {
hash: ID!
raw: String!
issuer: Identity!
audience: Identity!
issuanceDate: Date!
expirationDate: Date
context: [String]
type: [String]
credentials: [Credential]
messages: [Message]
}
scalar CredentialSubject
type Credential {
hash: ID!
raw: String!
issuer: Identity!
subject: Identity!
issuanceDate: Date!
expirationDate: Date
context: [String]
type: [String]
credentialSubject: CredentialSubject
claims: [Claim]
presentations: [Presentation]
messages: [Message]
}
type Claim {
hash: ID!
issuer: Identity!
subject: Identity!
credential: Credential!
type: String!
value: String!
isObj: Boolean
}
`
114 changes: 60 additions & 54 deletions packages/daf-core/src/graphql/graphql-core.ts
Original file line number Diff line number Diff line change
@@ -1,89 +1,95 @@
import { Core } from '../core'
import { LastMessageTimestampForInstance } from '../service/service-manager'

import { Message } from '../entities/message'
import { In, getRepository } from 'typeorm'
import { Presentation } from '../entities/presentation'
import { Credential } from '../entities/credential'
import { Claim } from '../entities/claim'

export interface Context {
core: Core
}

const newMessage = async (
const saveNewMessage = async (
_: any,
args: { raw: string; sourceType: string; sourceId?: string },
args: { raw: string; metaDataType?: string; metaDataValue?: string },
ctx: Context,
) => {
const message = await ctx.core.validateMessage(
new Message({
raw: args.raw,
meta: {
type: args.sourceType,
value: args.sourceId,
},
}),
)

const res = {
...message,
sender: message.from,
receiver: message.to,
data: JSON.stringify(message.data),
id: message.id,
raw: message.raw,
__typename: 'Message',
}
return res
return await ctx.core.saveNewMessage(args)
}

const serviceMessagesSince = async (
const findMessages = async (
_: any,
args: { ts: LastMessageTimestampForInstance[] },
args: {
options?:{ take?: number, skip?: number },
from?: string[],
to?: string[],
type?: string,
threadId?: string,
},
ctx: Context,
) => {
const res = await ctx.core.getMessagesSince(args.ts)
return [] // FIXME
// return res.map(msg => ({
// ...msg,
// id: msg.id,
// data: JSON.stringify(msg.data),
// raw: msg.raw,
// metaData: msg.allMeta,
// }))
const options = { where: {} }

if (args.from) options.where['from'] = In(args.from)
if (args.to) options.where['to'] = In(args.to)
if (args.type) options.where['type'] = args.type
if (args.threadId) options.where['threadId'] = args.threadId
if (args.options?.skip) options['skip'] = args.options.skip
if (args.options?.take) options['take'] = args.options.take

return await Message.find(options)
}


export const resolvers = {
Credential: {
claims: async (credential: Credential, args, ctx) => {
return getRepository(Claim).find({ where: { credential: credential.hash}, relations: ['issuer', 'subject'] })
}
},
Presentation: {
credentials: async (presentation: Presentation, args, ctx) => {
return getRepository(Credential).find({ where: { presentations: presentation.hash}, relations: ['issuer', 'subject'] })
}
},
Message: {
presentations: async (message: Message, args, ctx) => {
return getRepository(Presentation).find({ where: { messages: message.id}, relations: ['issuer', 'audience'] })
},
credentials: async (message: Message, args, ctx) => {
return getRepository(Credential).find({ where: { messages: message.id}, relations: ['issuer', 'subject'] })
}
},
Query: {
serviceMessagesSince,
findMessages,
},
Mutation: {
newMessage,
saveNewMessage,
},
}

export const typeDefs = `
input LastMessageTimestampForInstance {
timestamp: Int!
did: String!
type: String!
id: String!
input FindOptions {
take: Int
skip: Int
orderField: String
orderDirection: String
}
type ServiceMessage {
id: String!
threadId: String,
timestamp: Int,
sender: String,
receiver: String,
type: String,
raw: String,
data: String,
metaData: [MessageMetaData]
input FindMessagesInput {
from: [String]
to: [String]
type: String
threadId: String
options: FindOptions
}
extend type Query {
serviceMessagesSince(ts: [LastMessageTimestampForInstance]!): [ServiceMessage]
findMessages(input: FindMessagesInput): [Message]
}
extend type Mutation {
newMessage(raw: String!, sourceType: String!, sourceId: String): Message
saveNewMessage(raw: String!, metaDataType: String, metaDataValue: String): Message
}
`

0 comments on commit 698aca4

Please sign in to comment.