From edfb0faff96c4fe082763ef64200b973f81822c9 Mon Sep 17 00:00:00 2001 From: Ravi Gairola Date: Mon, 20 Nov 2023 18:55:01 -0800 Subject: [PATCH] Add agent & group api --- index.js | 28 ++++++++++++++++++++++++++-- schema.js | 54 ++++++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 74 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index fb624a5..62c5008 100644 --- a/index.js +++ b/index.js @@ -37,11 +37,11 @@ export async function initialize(api) { app.register(mercurius, { schema, - context: (request, reply) => { + context: () => { return { api, pubSub: pubSub } }, subscription: { - onConnect: (connectionParams, webSocket, context) => { + onConnect: () => { return { api, pubSub: pubSub } }, }, @@ -57,8 +57,32 @@ export async function initialize(api) { }) api.comms.on('all', (msg) => { + api.log.trace( + 'Received message from system, sending to GraphQL subs', + msg.toObject() + ) pubSub.publish('MESSAGE_SENT', { messageSent: msg.toObject(), }) }) + + api.on('groupCreated', (group) => { + api.log.debug( + 'Received groupCreated event from system, sending to GraphQL subs', + group + ) + pubSub.publish('GROUP_CREATED', { + groupCreated: group, + }) + }) + + api.on('agentCreated', (agent) => { + api.log.debug( + 'Received agentCreated event from system, sending to GraphQL subs', + agent + ) + pubSub.publish('AGENT_CREATED', { + agentCreated: agent, + }) + }) } diff --git a/schema.js b/schema.js index 33f8d17..405a2eb 100644 --- a/schema.js +++ b/schema.js @@ -8,30 +8,45 @@ export const typeDefs = gql` messages: [Message] } type Agent { - name: String - driver: Driver + name: String! + config: AgentConfig! + driver: Driver! + } + type AgentConfig { + type: String! + description: String + creator: Boolean + isolate: Boolean + driver: DriverConfig! + } + type DriverConfig { + type: String! } type Driver { - type: String + type: String! } type Group { - name: String + name: String! } type Message { id: Float type: String source: String - target: String + target: String! timestamp: String - content: String + content: String! } type Mutation { sendMessage(message: String!, target: String!): Message + createGroup(name: String!): String + createAgent(name: String!, driver: String!): Agent } type Subscription { messageSent: Message + groupCreated: String + agentCreated: Agent } ` @@ -86,6 +101,23 @@ export const resolvers = { context.api.comms.emit(msg) return msg }, + createGroup: (parent, args, context) => { + context.api.log.trace('GraphQL Received Request to create Group', args) + context.api.createGroup(args.name) + return args.name + }, + createAgent: (parent, args, context) => { + context.api.log.trace('GraphQL Received Request to create Agent', args) + + return context.api.createAgent(args.name, { + description: 'Created by GraphQL', + creator: false, + isolate: false, + driver: { + type: args.driver, + }, + }) + }, }, Subscription: { messageSent: { @@ -93,5 +125,15 @@ export const resolvers = { return pubSub.asyncIterator(['MESSAGE_SENT']) }, }, + groupCreated: { + subscribe: (_, __, { pubSub }) => { + return pubSub.asyncIterator(['GROUP_CREATED']) + }, + }, + agentCreated: { + subscribe: (_, __, { pubSub }) => { + return pubSub.asyncIterator(['AGENT_CREATED']) + }, + }, }, }