Skip to content

Commit

Permalink
Fix bug when decoding lists of global IDs with null items
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Dec 13, 2023
1 parent 929b0f3 commit 0f5cfa9
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .changeset/selfish-pumpkins-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@pothos/plugin-relay': patch
'@pothos/core': patch
---

Fix bug when decoding lists of global IDs with null items
4 changes: 3 additions & 1 deletion packages/core/src/utils/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,9 @@ export function createInputValueMapper<Types extends SchemaTypes, T, Args extend

if (field.kind !== 'InputObject' || field.value !== null) {
mapped[fieldName] = field.isList
? (fieldVal as unknown[]).map((val) => mapValue(val, field, ...args))
? (fieldVal as unknown[]).map((val) =>
val == null ? val : mapValue(val, field, ...args),
)
: mapValue(fieldVal, field, ...args);
}
});
Expand Down
6 changes: 3 additions & 3 deletions packages/plugin-relay/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ input GlobalIDInput {
circular: GlobalIDInput
circularWithoutGlobalIds: NoGlobalIDInput
id: ID!
idList: [ID!]!
idList: [ID]!
otherList: [OtherInput!] = [{someField: \\"abc\\"}]
}
Expand Down Expand Up @@ -156,7 +156,7 @@ type Query {
extraNode: Node
idWithColon(id: ID!): IDWithColon!
idsWithColon(ids: [ID!]!): [IDWithColon!]!
inputGlobalID(id: ID!, inputObj: GlobalIDInput!, normalId: ID!): String!
inputGlobalID(id: ID!, idList: [ID]!, inputObj: GlobalIDInput!, normalId: ID!): String!
moreNodes: [Node]!
node(id: ID!): Node
nodes(ids: [ID!]!): [Node]!
Expand Down Expand Up @@ -328,7 +328,7 @@ input GlobalIDInput {
circular: GlobalIDInput
circularWithoutGlobalIds: NoGlobalIDInput
id: ID!
idList: [ID!]!
idList: [ID]!
}
type Mutation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ interface GlobalIDInputsShape {
id: string;
typename: string;
};
idList: {
id: string;
typename: string;
}[];
idList: (
| {
id: string;
typename: string;
}
| null
| undefined
)[];
}

interface CircularWithoutGlobalIds {
Expand All @@ -37,7 +41,7 @@ GlobalIDInput.implement({
idList: t.globalIDList({
required: {
list: true,
items: true,
items: false,
},
}),
}),
Expand Down Expand Up @@ -81,10 +85,13 @@ builder.queryType({
id: args.inputObj.id.id,
typename: args.inputObj.id.typename,
},
idList: args.inputObj.idList?.map((id) => ({
id: id.id,
typename: id.typename,
})),
idList: args.inputObj.idList?.map(
(id) =>
id && {
id: id.id,
typename: id.typename,
},
),
},
id: {
id: args.id.id,
Expand Down
20 changes: 15 additions & 5 deletions packages/plugin-relay/tests/examples/relay/schema/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ interface GlobalIDInputsShape {
id: string;
typename: string;
};
idList: {
id: string;
typename: string;
}[];
idList: (
| {
id: string;
typename: string;
}
| null
| undefined
)[];
}

interface CircularWithoutGlobalIds {
Expand Down Expand Up @@ -48,7 +52,7 @@ GlobalIDInput.implement({
idList: t.globalIDList({
required: {
list: true,
items: true,
items: false,
},
}),
}),
Expand All @@ -75,6 +79,12 @@ builder.queryType({
type: GlobalIDInput,
required: true,
}),
idList: t.arg.globalIDList({
required: {
list: true,
items: false,
},
}),
},
resolve(parent, args) {
return JSON.stringify(args);
Expand Down
6 changes: 4 additions & 2 deletions packages/plugin-relay/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,10 @@ describe('relay example schema', () => {
inputGlobalID(
id: "YWJjOjEyMw=="
normalId: 123
idList: [null, "YWJjOjEyMw=="]
inputObj: {
id: "YWJjOjEyMw=="
idList: ["YWJjOjEyMw=="]
idList: [null, "YWJjOjEyMw=="]
circular: { id: "YWJjOjEyMw==", idList: ["YWJjOjEyMw=="] }
}
)
Expand All @@ -380,10 +381,11 @@ describe('relay example schema', () => {
document: query,
contextValue: {},
});
console.log(result.errors);

expect(result.data).toMatchInlineSnapshot(`
{
"inputGlobalID": "{\\"id\\":{\\"typename\\":\\"abc\\",\\"id\\":\\"123\\"},\\"inputObj\\":{\\"circular\\":{\\"id\\":{\\"typename\\":\\"abc\\",\\"id\\":\\"123\\"},\\"idList\\":[{\\"typename\\":\\"abc\\",\\"id\\":\\"123\\"}],\\"otherList\\":[{\\"someField\\":\\"abc\\"}]},\\"id\\":{\\"typename\\":\\"abc\\",\\"id\\":\\"123\\"},\\"idList\\":[{\\"typename\\":\\"abc\\",\\"id\\":\\"123\\"}],\\"otherList\\":[{\\"someField\\":\\"abc\\"}]},\\"normalId\\":\\"123\\"}",
"inputGlobalID": "{\\"id\\":{\\"typename\\":\\"abc\\",\\"id\\":\\"123\\"},\\"idList\\":[null,{\\"typename\\":\\"abc\\",\\"id\\":\\"123\\"}],\\"inputObj\\":{\\"circular\\":{\\"id\\":{\\"typename\\":\\"abc\\",\\"id\\":\\"123\\"},\\"idList\\":[{\\"typename\\":\\"abc\\",\\"id\\":\\"123\\"}],\\"otherList\\":[{\\"someField\\":\\"abc\\"}]},\\"id\\":{\\"typename\\":\\"abc\\",\\"id\\":\\"123\\"},\\"idList\\":[null,{\\"typename\\":\\"abc\\",\\"id\\":\\"123\\"}],\\"otherList\\":[{\\"someField\\":\\"abc\\"}]},\\"normalId\\":\\"123\\"}",
}
`);
});
Expand Down

1 comment on commit 0f5cfa9

@vercel
Copy link

@vercel vercel bot commented on 0f5cfa9 Dec 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.