Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Commit

Permalink
fix(project): fix RequestControlAction mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristiaanScheermeijer committed Mar 19, 2020
1 parent bbe7e73 commit ab0e72e
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 55 deletions.
46 changes: 0 additions & 46 deletions src/commands/QueryAndPublishResolver.js

This file was deleted.

1 change: 1 addition & 0 deletions src/commands/RequestControlActionCommand.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class RequestControlActionCommand {
}
// createdControlAction.entryPointIdentifier = template.identifier
pubsub.publish('ControlActionRequest', { ControlActionRequest: createdControlAction, entryPointIdentifier: template.identifier })
pubsub.publish('ThingCreateMutation', { identifier: createdControlAction.identifier, type: 'ControlAction' })

return createdControlAction
})
Expand Down
6 changes: 1 addition & 5 deletions src/resolvers/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { info, warning } from '../utils/logger'
import { driver } from '../driver'
import { retrieveNodeData, pubsub } from '../resolvers'
import RequestControlActionCommand from '../commands/RequestControlActionCommand'
import QueryAndPublishResolver from '../commands/QueryAndPublishResolver'
import UpdateControlActionQuery from '../queries/UpdateControlActionQuery'

export const mutationResolvers = {
Expand All @@ -14,10 +13,7 @@ export const mutationResolvers = {
UpdateControlAction (object, params, ctx, resolveInfo) {
const queryGenerator = new UpdateControlActionQuery(params, resolveInfo)
return runQuery(queryGenerator.query, 'UpdateControlAction', 'ControlActionMutation')
},
CreateMediaObject: QueryAndPublishResolver.createResolver('MediaObject', 'MediaObjectCreateMutation'),
CreateVideoObject: QueryAndPublishResolver.createResolver('VideoObject', 'VideoObjectCreateMutation'),
CreateAudioObject: QueryAndPublishResolver.createResolver('AudioObject', 'AudioObjectCreateMutation')
}
}
}

Expand Down
47 changes: 43 additions & 4 deletions src/transformers/subscriptionFieldTransformer.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TransformRootFields } from 'graphql-tools'
import QueryAndPublishResolver from '../commands/QueryAndPublishResolver'
import { pubsub } from '../resolvers'

// add custom subscription triggers for specific types (key)
const customTypeTriggers = {
Expand All @@ -10,7 +10,12 @@ const customTypeTriggers = {

export const subscriptionFieldTransformer = new TransformRootFields((operation, fieldName, field) => {
// subscriptions are only triggered for mutations
if (operation !== 'Mutation') {
if (operation !== 'Mutation' || fieldName === 'RequestControlAction') {
return undefined
}

// ignore custom mutations with custom resolvers
if (fieldName === 'UpdateControlAction' || fieldName === 'RequestControlAction') {
return undefined
}

Expand All @@ -25,8 +30,42 @@ export const subscriptionFieldTransformer = new TransformRootFields((operation,
return next(object, params, context, resolveInfo)
}

const resolve = QueryAndPublishResolver.createResolver(typeName, customTypeTriggers[typeName])
const selections = resolveInfo.fieldNodes[0] && resolveInfo.fieldNodes[0].selectionSet.selections
const hasIdentifier = selections && selections.find(({ name }) => name.value === 'identifier')

// add identifier to selections
if (!hasIdentifier) {
resolveInfo.fieldNodes[0].selectionSet.selections.push({
kind: 'Field',
name: {
kind: 'Name',
value: 'identifier'
},
arguments: [],
directives: []
})
}

return next(object, params, context, resolveInfo).then(response => {
// remove the identifier so it doesn't end up in the response if the original request does not include it in
// its response
if (!hasIdentifier) {
const idx = selections.findIndex(({ name }) => name.value === 'identifier')

selections.splice(idx, 1)
}

// publish for subscriptions
if (response.identifier) {
// always trigger a ThingCreateMutation
pubsub.publish('ThingCreateMutation', { type: typeName, identifier: response.identifier })

if (customTypeTriggers[typeName]) {
pubsub.publish(customTypeTriggers[typeName], { type: typeName, identifier: response.identifier, params })
}
}

return resolve(object, params, context, resolveInfo)
return response
})
}
})

0 comments on commit ab0e72e

Please sign in to comment.