Skip to content

Commit

Permalink
feat: Graphql server in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
simonas-notcat committed Nov 24, 2019
1 parent 0b4342a commit 70890bd
Show file tree
Hide file tree
Showing 5 changed files with 920 additions and 42 deletions.
7 changes: 6 additions & 1 deletion packages/daf-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"watch": "tsc --watch"
},
"dependencies": {
"@types/ws": "^6.0.3",
"apollo-server": "^2.9.12",
"commander": "^4.0.1",
"console-table-printer": "^1.1.15",
"daf-core": "^0.2.0",
Expand All @@ -30,13 +30,18 @@
"daf-w3c": "^0.2.0",
"date-fns": "^2.8.1",
"debug": "^4.1.1",
"graphql": "^14.5.8",
"inquirer": "^7.0.0",
"lodash.merge": "^4.6.2",
"qrcode-terminal": "^0.12.0",
"ws": "^7.2.0"
},
"devDependencies": {
"@types/debug": "^4.1.5",
"@types/inquirer": "^6.5.0",
"@types/lodash.merge": "^4.6.6",
"@types/node-fetch": "^2.5.4",
"@types/ws": "^6.0.3",
"typescript": "^3.7.2"
},
"files": [
Expand Down
1 change: 1 addition & 0 deletions packages/daf-cli/src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import './did-resolver'
import './credential'
import './services'
import './data-explorer'
import './graphql'

program.parse(process.argv)
if (!process.argv.slice(2).length) {
Expand Down
41 changes: 41 additions & 0 deletions packages/daf-cli/src/graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ApolloServer } from 'apollo-server'
import program from 'commander'
import * as Daf from 'daf-core'
import * as W3c from 'daf-w3c'
import * as DIDComm from 'daf-did-comm'
import { Gql as DataGql } from 'daf-data-store'
import merge from 'lodash.merge'
import { core, dataStore } from './setup'
import { listen } from './services'
program
.command('graphql')
.description('GraphQL server')
.option('-p, --port <port>', 'Port')
.option('-l, --listen', 'Listen for new messages')
.action(async cmd => {
const server = new ApolloServer({
typeDefs: [
Daf.Gql.baseTypeDefs,
Daf.Gql.Core.typeDefs,
Daf.Gql.IdentityManager.typeDefs,
DataGql.typeDefs,
DIDComm.Gql.typeDefs,
W3c.Gql.typeDefs,
],
resolvers: merge(
Daf.Gql.Core.resolvers,
Daf.Gql.IdentityManager.resolvers,
DataGql.resolvers,
DIDComm.Gql.resolvers,
W3c.Gql.resolvers,
),
context: () => ({ dataStore, core }),
introspection: true,
})
const info = await server.listen({ port: cmd.port })
console.log(`🚀 Server ready at ${info.url}`)

if (cmd.listen) {
await listen()
}
})
22 changes: 12 additions & 10 deletions packages/daf-cli/src/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@ import program from 'commander'
program
.command('listen')
.description('Receive new messages and listen for new ones')
.action(async (did) => {
await dataStore.initialize()
.action(async cmd => {
await listen()
})

core.on(EventTypes.validatedMessage, (type, msg: Types.ValidatedMessage) => {
console.log('New message type:', msg.type)
})
export const listen = async () => {
await dataStore.initialize()

await core.startServices()
await core.syncServices(
await dataStore.latestMessageTimestamps()
)
core.on(EventTypes.validatedMessage, async (type, msg: Types.ValidatedMessage) => {
console.log('New message type:', msg.type)
await dataStore.saveMessage(msg)
})


await core.startServices()
await core.syncServices(await dataStore.latestMessageTimestamps())
}
Loading

0 comments on commit 70890bd

Please sign in to comment.