diff --git a/packages/plugin-relay/src/index.ts b/packages/plugin-relay/src/index.ts index 2d01a96ae..25928321a 100644 --- a/packages/plugin-relay/src/index.ts +++ b/packages/plugin-relay/src/index.ts @@ -27,9 +27,9 @@ export class PothosRelayPlugin extends BasePlugin { const argMappings = mapInputFields(fieldConfig.args, this.buildCache, (inputField) => { if (inputField.extensions?.isRelayGlobalID) { - return (inputField.extensions?.relayGlobalIDFor ?? true) as - | true - | { typename: string; parseId: (id: string, ctx: object) => unknown }[]; + return (inputField.extensions?.relayGlobalIDFor ?? + inputField.extensions?.alwaysParse ?? + false) as boolean | { typename: string; parseId: (id: string, ctx: object) => unknown }[]; } return null; @@ -42,13 +42,7 @@ export class PothosRelayPlugin extends BasePlugin - internalDecodeGlobalID( - this.builder, - String(globalID), - ctx, - info, - Array.isArray(mappings.value) ? mappings.value : true, - ), + internalDecodeGlobalID(this.builder, String(globalID), ctx, info, mappings.value ?? false), ); return (parent, args, context, info) => diff --git a/packages/plugin-relay/src/schema-builder.ts b/packages/plugin-relay/src/schema-builder.ts index 0d2f656f4..bfab4d94b 100644 --- a/packages/plugin-relay/src/schema-builder.ts +++ b/packages/plugin-relay/src/schema-builder.ts @@ -158,7 +158,12 @@ schemaBuilderProto.nodeInterfaceRef = function nodeInterfaceRef() { ...this.options.relayOptions.nodeQueryOptions, type: ref as InterfaceRef, args: { - id: t.arg.globalID({ required: true }), + id: t.arg.globalID({ + required: true, + extensions: { + alwaysParse: true, + }, + }), }, resolve: resolveNodeFn ? (root, args, context, info) => @@ -198,7 +203,12 @@ schemaBuilderProto.nodeInterfaceRef = function nodeInterfaceRef() { ...this.options.relayOptions.nodesQueryOptions, type: [ref], args: { - ids: t.arg.globalIDList({ required: true }), + ids: t.arg.globalIDList({ + required: true, + extensions: { + alwaysParse: true, + }, + }), }, resolve: resolveNodesFn ? (root, args, context, info) => diff --git a/packages/plugin-relay/tests/__snapshots__/index.test.ts.snap b/packages/plugin-relay/tests/__snapshots__/index.test.ts.snap index 45ac44025..ea0bd4526 100644 --- a/packages/plugin-relay/tests/__snapshots__/index.test.ts.snap +++ b/packages/plugin-relay/tests/__snapshots__/index.test.ts.snap @@ -46,6 +46,13 @@ input GlobalIDInput { otherList: [OtherInput!] = [{someField: \\"abc\\"}] } +type IDResult { + arg: String! + id: String! + idType: String! + typename: String! +} + type Mutation { answerPoll(answer: Int!, id: ID!): Poll! createPoll(answers: [String!]!, question: String!): Poll! @@ -128,6 +135,7 @@ type PollAnswersUsingOffsetConnectionEdge { type Query { batchNumbers(after: ID, before: ID, first: Int, last: Int): QueryBatchNumbersConnection! cursorConnection(after: ID, before: ID, first: Int, last: Int): QueryCursorConnection! + echoIDs(genericNumberThingID: ID!, globalID: ID!, numberThingID: ID!): [IDResult!]! extraNode: Node inputGlobalID(id: ID!, inputObj: GlobalIDInput!, normalId: ID!): String! moreNodes: [Node]! diff --git a/packages/plugin-relay/tests/examples/relay/schema/numbers.ts b/packages/plugin-relay/tests/examples/relay/schema/numbers.ts index e7a5846a4..cd151ecc5 100644 --- a/packages/plugin-relay/tests/examples/relay/schema/numbers.ts +++ b/packages/plugin-relay/tests/examples/relay/schema/numbers.ts @@ -236,3 +236,38 @@ builder.queryField('numberThingsByIDs', (t) => resolve: (root, args) => args.ids.map(({ id }) => new NumberThing(id)), }), ); + +const IDResult = builder + .objectRef<{ + id: unknown; + typename: string; + arg: string; + }>('IDResult') + .implement({ + fields: (t) => ({ + id: t.string({ + resolve: (n) => String(n.id), + }), + typename: t.exposeString('typename', {}), + arg: t.exposeString('arg', {}), + idType: t.string({ + resolve: (n) => typeof n.id, + }), + }), + }); + +builder.queryField('echoIDs', (t) => + t.field({ + type: [IDResult], + args: { + globalID: t.arg.globalID({ required: true }), + numberThingID: t.arg.globalID({ required: true, for: [NumberThingRef] }), + genericNumberThingID: t.arg.globalID({ required: true, for: [NumberThing] }), + }, + resolve: (_, args) => [ + { ...args.globalID, arg: 'globalID' }, + { ...args.numberThingID, arg: 'numberThingID' }, + { ...args.genericNumberThingID, arg: 'genericNumberThingID' }, + ], + }), +); diff --git a/packages/plugin-relay/tests/index.test.ts b/packages/plugin-relay/tests/index.test.ts index eb9f6322d..d362bd7e1 100644 --- a/packages/plugin-relay/tests/index.test.ts +++ b/packages/plugin-relay/tests/index.test.ts @@ -529,6 +529,16 @@ describe('relay example schema', () => { id number } + echoIDs( + globalID: "TnVtYmVyOjE=" + numberThingID: "TnVtYmVyOjE=" + genericNumberThingID: "TnVtYmVyOjE=" + ) { + id + typename + arg + idType + } } `; @@ -541,6 +551,26 @@ describe('relay example schema', () => { expect(result).toMatchInlineSnapshot(` { "data": { + "echoIDs": [ + { + "arg": "globalID", + "id": "1", + "idType": "string", + "typename": "Number", + }, + { + "arg": "numberThingID", + "id": "1", + "idType": "number", + "typename": "Number", + }, + { + "arg": "genericNumberThingID", + "id": "1", + "idType": "string", + "typename": "Number", + }, + ], "invalid": null, "invalidList": null, "numberThingByID": {