Skip to content

Commit

Permalink
fix(graphql): merge all entries in arrays (#184)
Browse files Browse the repository at this point in the history
  • Loading branch information
hjvedvik committed Feb 8, 2019
1 parent 6a2d227 commit 80952d6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 46 deletions.
120 changes: 78 additions & 42 deletions gridsome/__tests__/createFieldTypes.spec.js
Expand Up @@ -13,53 +13,89 @@ const {
GraphQLObjectType
} = require('../graphql')

test('infer types from node fields', () => {
const fields = mergeNodeFields([
{
fields: {
string: 'bar',
number: 10,
float: 1.2,
nullValue: null,
truthyBoolean: true,
stringList: ['item'],
numberList: [10],
floatList: [1.2],
extendObj: {},
refs: [],
simpleObj: {
foo: 'bar'
},
obj: {
foo: 'bar'
}
const nodes = [
{
fields: {
string: 'bar',
number: 10,
float: 1.2,
nullValue: null,
truthyBoolean: true,
stringList: ['item'],
numberList: [10],
floatList: [1.2],
objList: [
{ a: 'a' },
{ b: 'b' } // #184
],
extendObj: {},
refs: [],
refList: [
{ typeName: 'Post1', id: '1' },
{ typeName: 'Post2', id: '1' },
{ typeName: 'Post3', id: '1' }
],
simpleObj: {
foo: 'bar'
},
obj: {
foo: 'bar'
}
},
{
fields: {
string: true,
falsyBoolean: false,
booleanList: [false],
numberList: [5, 30],
emptyList: [],
emptyObj: {},
emptyString: '',
refs: [
{ typeName: 'Post', id: '1' } // #128, #129
],
extendObj: {
bar: 'foo'
},
obj: {
bar: 'foo',
test: {
foo: 'bar'
}
}
},
{
fields: {
string: true,
falsyBoolean: false,
booleanList: [false],
numberList: [5, 30],
emptyList: [],
objList: [
{ c: 'c' }
],
emptyObj: {},
emptyString: '',
refs: [
{ typeName: 'Post', id: '1' } // #128, #129
],
ref: { typeName: 'Post', id: '1' },
extendObj: {
bar: 'foo'
},
obj: {
bar: 'foo',
test: {
foo: 'bar'
}
}
}
])
}
]

test('merge node fields', () => {
const fields = mergeNodeFields(nodes)

expect(fields.emptyString).toEqual('')
expect(fields.numberList).toHaveLength(1)
expect(fields.floatList).toHaveLength(1)
expect(fields.objList).toHaveLength(1)
expect(fields.emptyList).toHaveLength(0)
expect(fields.objList[0]).toMatchObject({ a: 'a', b: 'b', c: 'c' })
expect(fields.refs.typeName).toHaveLength(1)
expect(fields.refs.typeName[0]).toEqual('Post')
expect(fields.refs.isList).toEqual(true)
expect(fields.refList.typeName).toHaveLength(3)
expect(fields.refList.typeName).toEqual(expect.arrayContaining(['Post1', 'Post2', 'Post3']))
expect(fields.refList.isList).toEqual(true)
expect(fields.ref).toMatchObject({ typeName: 'Post', isList: false })
expect(fields.emptyObj).toMatchObject({})
expect(fields.simpleObj).toMatchObject({ foo: 'bar' })
expect(fields.extendObj).toMatchObject({ bar: 'foo' })
expect(fields.obj).toMatchObject({ foo: 'bar', bar: 'foo', test: { foo: 'bar' }})
})

test('create graphql types from node fields', () => {
const fields = mergeNodeFields(nodes)
const types = createFieldTypes(fields, 'TestPost', {})

expect(types.string.type).toEqual(GraphQLString)
Expand Down
13 changes: 9 additions & 4 deletions gridsome/lib/graphql/utils/mergeFields.js
Expand Up @@ -28,12 +28,15 @@ function fieldValues (obj, currentObj = {}) {

function fieldValue (value, currentValue) {
if (Array.isArray(value)) {
const arr = Array.isArray(currentValue) ? currentValue : []
const length = value.length

if (isRefField(value[0])) {
if (!isRefValue(currentValue)) {
currentValue = { typeName: [], isList: true }
}

for (let i = 0, l = value.length; i < l; i++) {
for (let i = 0; i < length; i++) {
if (!currentValue.typeName.includes(value[i].typeName)) {
currentValue.typeName.push(value[i].typeName)
}
Expand All @@ -42,9 +45,11 @@ function fieldValue (value, currentValue) {
return currentValue
}

return value.map((value, index) => {
return fieldValue(value, currentValue ? currentValue[index] : undefined)
})
for (let i = 0; i < length; i++) {
arr[0] = fieldValue(value[i], arr[0])
}

return arr
} else if (isPlainObject(value)) {
if (isRefField(value)) {
const ref = currentValue || { typeName: value.typeName }
Expand Down

0 comments on commit 80952d6

Please sign in to comment.