Skip to content

Commit

Permalink
fix(store): create reference to node instance
Browse files Browse the repository at this point in the history
Eg. `store.createReference(node)`
  • Loading branch information
hjvedvik committed May 11, 2019
1 parent a413d4a commit 81bb047
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 13 deletions.
5 changes: 4 additions & 1 deletion gridsome/lib/graphql/__tests__/createFieldTypes.spec.js
Expand Up @@ -32,8 +32,10 @@ const nodes = [
refList: [
{ typeName: 'Post1', id: '1' },
{ typeName: 'Post2', id: '1' },
{ typeName: 'Post3', id: '1' }
{ typeName: 'Post3', id: '1' },
{ typeName: undefined, id: '1' }
],
invalidRef: { typeName: undefined, id: '1' },
simpleObj: {
foo: 'bar'
},
Expand Down Expand Up @@ -94,6 +96,7 @@ test('merge node fields', () => {
expect(fields.simpleObj).toMatchObject({ foo: 'bar' })
expect(fields.extendObj).toMatchObject({ bar: 'foo' })
expect(fields.obj).toMatchObject({ foo: 'bar', bar: 'foo', test: { foo: 'bar' }})
expect(fields.invalidRef).toBeUndefined()
})

test('create graphql types from node fields', () => {
Expand Down
8 changes: 3 additions & 5 deletions gridsome/lib/graphql/__tests__/nodes.spec.js
Expand Up @@ -238,7 +238,7 @@ test('create node reference', async () => {
}
})

authors.addNode({
const author = authors.addNode({
id: '2'
})

Expand All @@ -252,7 +252,7 @@ test('create node reference', async () => {

posts.addNode({
id: '2',
customRef: api.store.createReference('TestAuthor', '2')
customRef: api.store.createReference(author)
})

const query = `{
Expand Down Expand Up @@ -281,9 +281,7 @@ test('create node reference', async () => {
})

test('create node reference to same typeName', async () => {
const { addNode } = api.store.addContentType({
typeName: 'TestPost'
})
const { addNode } = api.store.addContentType('TestPost')

const post = addNode({ id: '1' })

Expand Down
20 changes: 14 additions & 6 deletions gridsome/lib/graphql/createFieldDefinitions.js
@@ -1,5 +1,6 @@
const { omit, isPlainObject, isNumber, isInteger } = require('lodash')
const { isRefField, isRefFieldDefinition } = require('./utils')
const { warn } = require('../utils/log')

module.exports = function createFieldDefinitions (nodes) {
let fields = {}
Expand All @@ -11,7 +12,7 @@ module.exports = function createFieldDefinitions (nodes) {
return fields
}

function fieldValues (obj, currentObj = {}) {
function fieldValues (obj, currentObj = {}, path = []) {
const res = { ...currentObj }

for (const key in obj) {
Expand All @@ -22,13 +23,13 @@ function fieldValues (obj, currentObj = {}) {
if (value === undefined) continue
if (value === null) continue

res[key] = fieldValue(value, currentObj[key])
res[key] = fieldValue(value, currentObj[key], path.concat(key))
}

return res
}

function fieldValue (value, currentValue) {
function fieldValue (value, currentValue, path = []) {
if (Array.isArray(value)) {
const arr = Array.isArray(currentValue) ? currentValue : []
const length = value.length
Expand All @@ -39,7 +40,9 @@ function fieldValue (value, currentValue) {
}

for (let i = 0; i < length; i++) {
if (!currentValue.typeName.includes(value[i].typeName)) {
if (!value[i].typeName) {
warn(`Missing typeName for reference at: ${path.join('.')}.${i}`)
} else if (!currentValue.typeName.includes(value[i].typeName)) {
currentValue.typeName.push(value[i].typeName)
}
}
Expand All @@ -52,19 +55,24 @@ function fieldValue (value, currentValue) {
}

for (let i = 0; i < length; i++) {
arr[0] = fieldValue(value[i], arr[0])
arr[0] = fieldValue(value[i], arr[0], path.concat(i))
}

return arr
} else if (isPlainObject(value)) {
if (isRefField(value)) {
if (!value.typeName) {
warn(`Missing typeName for reference in field: ${path.join('.')}`)
return currentValue
}

const ref = currentValue || { typeName: value.typeName }
ref.isList = ref.isList || Array.isArray(value.id)

return ref
}

return fieldValues(value, currentValue)
return fieldValues(value, currentValue, path)
} else if (isNumber(value)) {
return isNumber(currentValue) && isInteger(value)
? currentValue
Expand Down
4 changes: 4 additions & 0 deletions gridsome/lib/graphql/resolvers.js
Expand Up @@ -15,6 +15,10 @@ function createRefResolver ({ typeName, isList = false }) {
const query = {}
let chain

if (fieldValue.hasOwnProperty('typeName') && !fieldValue.typeName) {
return isList ? [] : null
}

if (id) {
query.id = Array.isArray(id) ? { $in: id } : id
query['internal.typeName'] = Array.isArray(typeName) ? { $in: typeName } : typeName
Expand Down
6 changes: 5 additions & 1 deletion gridsome/lib/store/PluginStore.js
Expand Up @@ -184,7 +184,11 @@ class PluginStore {

createReference (typeName, id) {
if (isPlainObject(typeName)) {
return { typeName: typeName.typeName, id: typeName.id }
if (!typeName.$loki) {
throw new Error(`store.createReference() expected a node.`)
}

return { typeName: typeName.internal.typeName, id: typeName.id }
}

return { typeName, id }
Expand Down

0 comments on commit 81bb047

Please sign in to comment.