feat(webhooks): initial gql queries & mutations

This commit is contained in:
izzy lyseggen
2021-07-09 15:09:49 +01:00
parent 942cee1023
commit 8577d6a7a9
3 changed files with 99 additions and 2 deletions
@@ -0,0 +1,34 @@
const { createWebhook, getWebhook, updateWebhook, deleteWebhook, getStreamWebhooks, getLastWebhookEvents } = require( '../../services/webhooks' )
module.exports = {
Stream: {
async webhooks( parent, args, context, info ) {
if ( args.id ) {
let wh = await getWebhook( { id: args.id } )
let items = wh ? [ wh ] : []
return { items, totalCount: items.length }
}
let items = await getStreamWebhooks( { streamId: parent.id } )
return { items, totalCount: items.length }
}
},
Mutation: {
async webhookCreate( parent, args, context, info ) {
let id = await createWebhook( { streamId: args.webhook.streamId, url: args.webhook.url, description: args.webhook.description, secret: args.webhook.secret, enabled: args.webhook.enabled !== false, events: args.webhook.events } )
return id
},
async webhookUpdate( parent, args, context, info ) {
let updated = await updateWebhook( { id: args.webhook.id, url: args.webhook.url, description: args.webhook.description, secret: args.webhook.secret, enabled: args.webhook.enabled !== false, events: args.webhook.events } )
return !!updated
},
async webhookDelete( parent, args, context, info ) {
let deleted = await deleteWebhook( { id: args.id } )
return !!deleted
}
}
}
@@ -0,0 +1,62 @@
extend type Stream {
webhooks(id: String): WebhookCollection
@hasRole(role: "stream:owner")
@hasScope(scope: "streams:write")
}
extend type Mutation {
"""
Creates a new webhook on a stream
"""
webhookCreate(webhook: WebhookCreateInput!): String!
@hasRole(role: "stream:owner")
@hasScope(scope: "streams:write")
"""
Updates an existing webhook
"""
webhookUpdate(webhook: WebhookUpdateInput!): String!
@hasRole(role: "stream:owner")
@hasScope(scope: "streams:write")
"""
Deletes an existing webhook
"""
webhookDelete(id: String!): String!
@hasRole(role: "stream:owner")
@hasScope(scope: "streams:write")
}
type WebhookCollection {
totalCount: Int
cursor: String
items: [Webhook]
}
type Webhook {
id: String!
streamId: String!
url: String!
description: String
events: JSONObject!
secret: String
enabled: Boolean
}
input WebhookCreateInput {
streamId: String!
url: String!
description: String
events: JSONObject!
secret: String
enabled: Boolean
}
input WebhookUpdateInput {
id: String!
url: String
description: String
secret: String
enabled: Boolean
events: JSONObject
}
@@ -11,7 +11,7 @@ module.exports = {
async createWebhook( { streamId, url, description, secret, enabled, events } ) {
// TODO: limit max number of webhooks for a stream to 100 (github has a 20 limit per event)
let [ id ] = await WebhooksConfig( ).returning( 'id' ).insert( {
id: crs( { length: 10 } ),
streamId,
@@ -23,7 +23,7 @@ module.exports = {
} )
return id
},
async getWebhook( { id } ) {
// TODO: get webhook object + summary of event history (last event status, etc)
return await WebhooksConfig().select( '*' ).where( { id } ).first()
@@ -61,6 +61,7 @@ module.exports = {
if ( !limit ) {
limit = 100
}
return await WebhooksEvents( ).select( '*' ).where( { webhookId } ).orderBy( 'lastUpdate', 'desc' ).limit( limit )
},
}