Skip to content

Commit

Permalink
Handle missing query on datatype relationship
Browse files Browse the repository at this point in the history
  • Loading branch information
kjellmorten committed Apr 8, 2018
1 parent 14c97af commit c3c0d01
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/datatype/castQueryParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ const getField = require('../utils/getField')
function castQueryParams (relId, data, {relationships}) {
const relationship = relationships[relId]

if (!relationship.query) {
return {}
}

return Object.keys(relationship.query)
.reduce((params, key) => {
const value = getField(data, relationship.query[key])
Expand Down
22 changes: 22 additions & 0 deletions lib/datatype/index-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,28 @@ test('castQueryParams should return params for rel query', (t) => {
t.deepEqual(ret, expected)
})

test('castQueryParams should return empty array when no query object', (t) => {
const data = {
id: 'johnf',
type: 'user',
attributes: {genre: 'fiction'},
relationships: {country: {id: 'no', type: 'country'}}
}
const type = datatype({
id: 'user',
relationships: {
books: {
type: 'book'
}
}
})
const expected = {}

const ret = type.castQueryParams('books', data)

t.deepEqual(ret, expected)
})

test('castQueryParams should return id array from many-relationship', (t) => {
const data = {
id: 'johnf',
Expand Down
2 changes: 1 addition & 1 deletion lib/integreat.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const setupDispatch = require('./dispatch')
const builtinActions = require('./actions')
const reduceToObject = require('./utils/reduceToObject')

const version = '0.6.2'
const version = '0.6.3'

/**
* Return an Integreat instance with a dispatch method.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "integreat",
"version": "0.6.2",
"version": "0.6.3",
"description": "Node.js integration layer",
"author": "Kjell-Morten Bratsberg Thorsen <post@kjellmorten.no> (http://kjellmorten.no/)",
"license": "ISC",
Expand Down

0 comments on commit c3c0d01

Please sign in to comment.