Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Improved consistency of handling results of different types returned by custom handlers in CRUD resolvers:
+ Wrap only objects (i.e. not primitive types or arrays) returned by custom handlers in arrays in create, read, and update resolvers
+ Delete mutations return the length of an array that is returned by a `DELETE` custom handler or 1 if a single object is returned

### Fixed

### Removed
Expand Down
3 changes: 2 additions & 1 deletion lib/resolvers/crud/create.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { INSERT } = require('@sap/cds/lib').ql
const { ARGS } = require('../../constants')
const formatResult = require('../parse/ast/result')
const { getArgumentByName, astToEntries } = require('../parse/ast2cqn')
const { isPlainObject } = require('../utils')
const { entriesStructureToEntityStructure } = require('./utils')

module.exports = async (service, entity, selection) => {
Expand All @@ -12,7 +13,7 @@ module.exports = async (service, entity, selection) => {
query.entries(entries)

const result = await service.run(query)
const resultInArray = Array.isArray(result) ? result : [result]
const resultInArray = isPlainObject(result) ? [result] : result

return formatResult(selection, resultInArray, true)
}
6 changes: 6 additions & 0 deletions lib/resolvers/crud/delete.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { DELETE } = require('@sap/cds/lib').ql
const { ARGS } = require('../../constants')
const { getArgumentByName, astToWhere } = require('../parse/ast2cqn')
const { isPlainObject } = require('../utils')

module.exports = async (service, entity, selection) => {
let query = DELETE.from(entity)
Expand All @@ -16,5 +17,10 @@ module.exports = async (service, entity, selection) => {
else throw e
}

// The CDS delete query returns the number of deleted entries
// However, custom handlers can return non-numeric results for delete
if (isPlainObject(result)) return 1
if (Array.isArray(result)) return result.length

return result
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so strings, numbers, buffers, etc. shall still be returned as provided?

Copy link
Member Author

@schwma schwma Apr 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, the GraphQL server framework will return an error such as 'Int cannot represent non-integer value: "foo"', since the return type of delete mutations is an integer

}
5 changes: 4 additions & 1 deletion lib/resolvers/crud/read.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { SELECT } = require('@sap/cds/lib').ql
const { ARGS, CONNECTION_FIELDS } = require('../../constants')
const { getArgumentByName, astToColumns, astToWhere, astToOrderBy, astToLimit } = require('../parse/ast2cqn')
const formatResult = require('../parse/ast/result')
const { isPlainObject } = require('../utils')

module.exports = async (service, entity, selection) => {
const selections = selection.selectionSet.selections
Expand All @@ -27,5 +28,7 @@ module.exports = async (service, entity, selection) => {

const result = await service.run(query)

return formatResult(selection, result)
const resultInArray = isPlainObject(result) ? [result] : result

return formatResult(selection, resultInArray)
}
14 changes: 10 additions & 4 deletions lib/resolvers/crud/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const { SELECT, UPDATE } = require('@sap/cds/lib').ql
const { ARGS } = require('../../constants')
const formatResult = require('../parse/ast/result')
const { getArgumentByName, astToColumns, astToWhere, astToEntries } = require('../parse/ast2cqn')
const { isPlainObject } = require('../utils')
const { entriesStructureToEntityStructure } = require('./utils')

module.exports = async (service, entity, selection) => {
Expand All @@ -26,12 +27,17 @@ module.exports = async (service, entity, selection) => {
const result = await service.tx(async tx => {
// read needs to be done before the update, otherwise the where clause might become invalid (case that properties in where clause are updated by the mutation)
resultBeforeUpdate = await tx.run(queryBeforeUpdate)
if (resultBeforeUpdate.length === 0) return []
if (resultBeforeUpdate.length === 0) return {}
return tx.run(query)
})

// Merge selected fields with updated data
const mergedResults = resultBeforeUpdate.map(original => ({ ...original, ...result }))
let mergedResults = result
if (Array.isArray(resultBeforeUpdate)) {
// Merge selected fields with updated data
mergedResults = resultBeforeUpdate.map(original => ({ ...original, ...result }))
}

return formatResult(selection, mergedResults, true)
const resultInArray = isPlainObject(mergedResults) ? [mergedResults] : mergedResults

return formatResult(selection, resultInArray, true)
}
2 changes: 1 addition & 1 deletion lib/resolvers/parse/ast/fromObject.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { Kind } = require('graphql')
const { isPlainObject } = require('./util')
const { isPlainObject } = require('../../utils')

const _nullValue = { kind: Kind.NULL }

Expand Down
2 changes: 1 addition & 1 deletion lib/resolvers/parse/ast/result.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { CONNECTION_FIELDS } = require('../../../constants')
const { isPlainObject } = require('../../utils')
const { getPotentiallyNestedNodesSelections } = require('../util')
const { isPlainObject } = require('./util')

const formatResult = (field, result, skipTopLevelConnection) => {
const _formatObject = (selections, object) => {
Expand Down
3 changes: 0 additions & 3 deletions lib/resolvers/parse/ast/util/index.js

This file was deleted.

5 changes: 4 additions & 1 deletion lib/resolvers/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
const isPlainObject = value =>
value !== null && typeof value === 'object' && !Array.isArray(value) && !Buffer.isBuffer(value)

const _ensureError = error => (error instanceof Error ? error : new Error(error))

const setResponse = async (response, key, value) => {
Expand All @@ -8,4 +11,4 @@ const setResponse = async (response, key, value) => {
}
}

module.exports = { setResponse }
module.exports = { isPlainObject, setResponse }
6 changes: 6 additions & 0 deletions test/resources/custom-handlers/server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const cds = require('@sap/cds')
const path = require('path')

cds.env.protocols = {
graphql: { path: '/graphql', impl: path.join(__dirname, '../../../index.js') }
}
21 changes: 21 additions & 0 deletions test/resources/custom-handlers/srv/return-types.cds
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
service ReturnTypesService {
entity Integer {
key id : UUID;
string : cds.String;
}

entity String {
key id : UUID;
string : cds.String;
}

entity Object {
key id : UUID;
string : cds.String;
}

entity Array {
key id : UUID;
string : cds.String;
}
}
9 changes: 9 additions & 0 deletions test/resources/custom-handlers/srv/return-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = srv => {
const string = 'foo'
const id = '0557a188-326e-4dcb-999b-e1acf7979fa3'

srv.on('*', 'Integer', () => 999)
srv.on('*', 'String', () => string)
srv.on('*', 'Object', () => ({ id, string }))
srv.on('*', 'Array', () => [{ id, string }, { id, string }])
}
2 changes: 1 addition & 1 deletion test/tests/concurrency.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
describe('graphql resolver concurrency', () => {
describe('graphql - resolver concurrency', () => {
const cds = require('@sap/cds/lib')
const path = require('path')
const { gql } = require('../util')
Expand Down
Loading