Skip to content

Commit

Permalink
fix(gatsby): Fix ordering for node links when search field isn't id (g…
Browse files Browse the repository at this point in the history
  • Loading branch information
freiksenet authored and wardpeet committed May 20, 2019
1 parent ec038e9 commit eeb1f8c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
38 changes: 23 additions & 15 deletions packages/gatsby/src/schema/infer/__tests__/infer.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ describe(`GraphQL type inference`, () => {
const nodes = [
{
id: `1`,
linkedOnCustomField: [`test1`, `test3`],
linkedOnCustomField: [`test3`, `test1`],
internal: { type: `Test` },
},
].concat(getMappingNodes())
Expand All @@ -802,20 +802,28 @@ describe(`GraphQL type inference`, () => {
}
)

expect(result.errors).not.toBeDefined()
expect(result.data.allTest.edges.length).toEqual(1)
expect(
result.data.allTest.edges[0].node.linkedOnCustomField
).toBeDefined()
expect(
result.data.allTest.edges[0].node.linkedOnCustomField.length
).toEqual(2)
expect(
result.data.allTest.edges[0].node.linkedOnCustomField[0].label
).toEqual(`First node`)
expect(
result.data.allTest.edges[0].node.linkedOnCustomField[1].label
).toEqual(`Third node`)
expect(result).toMatchInlineSnapshot(`
Object {
"data": Object {
"allTest": Object {
"edges": Array [
Object {
"node": Object {
"linkedOnCustomField": Array [
Object {
"label": "Third node",
},
Object {
"label": "First node",
},
],
},
},
],
},
},
}
`)
})
})

Expand Down
13 changes: 12 additions & 1 deletion packages/gatsby/src/schema/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,21 @@ const link = ({ by = `id`, from }) => async (source, args, context, info) => {
}
}, fieldValue)

return context.nodeModel.runQuery(
const result = await context.nodeModel.runQuery(
{ query: args, firstOnly: !(returnType instanceof GraphQLList), type },
{ path: context.path }
)
if (
returnType instanceof GraphQLList &&
Array.isArray(fieldValue) &&
Array.isArray(result)
) {
return fieldValue.map(value =>
result.find(obj => getValueAt(obj, by) === value)
)
} else {
return result
}
}

const fileByPath = ({ from }) => (source, args, context, info) => {
Expand Down

0 comments on commit eeb1f8c

Please sign in to comment.