Skip to content

Commit eeb1f8c

Browse files
freiksenetwardpeet
authored andcommitted
fix(gatsby): Fix ordering for node links when search field isn't id (#14176)
1 parent ec038e9 commit eeb1f8c

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

packages/gatsby/src/schema/infer/__tests__/infer.js

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ describe(`GraphQL type inference`, () => {
783783
const nodes = [
784784
{
785785
id: `1`,
786-
linkedOnCustomField: [`test1`, `test3`],
786+
linkedOnCustomField: [`test3`, `test1`],
787787
internal: { type: `Test` },
788788
},
789789
].concat(getMappingNodes())
@@ -802,20 +802,28 @@ describe(`GraphQL type inference`, () => {
802802
}
803803
)
804804

805-
expect(result.errors).not.toBeDefined()
806-
expect(result.data.allTest.edges.length).toEqual(1)
807-
expect(
808-
result.data.allTest.edges[0].node.linkedOnCustomField
809-
).toBeDefined()
810-
expect(
811-
result.data.allTest.edges[0].node.linkedOnCustomField.length
812-
).toEqual(2)
813-
expect(
814-
result.data.allTest.edges[0].node.linkedOnCustomField[0].label
815-
).toEqual(`First node`)
816-
expect(
817-
result.data.allTest.edges[0].node.linkedOnCustomField[1].label
818-
).toEqual(`Third node`)
805+
expect(result).toMatchInlineSnapshot(`
806+
Object {
807+
"data": Object {
808+
"allTest": Object {
809+
"edges": Array [
810+
Object {
811+
"node": Object {
812+
"linkedOnCustomField": Array [
813+
Object {
814+
"label": "Third node",
815+
},
816+
Object {
817+
"label": "First node",
818+
},
819+
],
820+
},
821+
},
822+
],
823+
},
824+
},
825+
}
826+
`)
819827
})
820828
})
821829

packages/gatsby/src/schema/resolvers.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,21 @@ const link = ({ by = `id`, from }) => async (source, args, context, info) => {
146146
}
147147
}, fieldValue)
148148

149-
return context.nodeModel.runQuery(
149+
const result = await context.nodeModel.runQuery(
150150
{ query: args, firstOnly: !(returnType instanceof GraphQLList), type },
151151
{ path: context.path }
152152
)
153+
if (
154+
returnType instanceof GraphQLList &&
155+
Array.isArray(fieldValue) &&
156+
Array.isArray(result)
157+
) {
158+
return fieldValue.map(value =>
159+
result.find(obj => getValueAt(obj, by) === value)
160+
)
161+
} else {
162+
return result
163+
}
153164
}
154165

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

0 commit comments

Comments
 (0)