Skip to content

Commit

Permalink
add tests for more id parsing cases
Browse files Browse the repository at this point in the history
  • Loading branch information
hayes committed Feb 23, 2023
1 parent c32a1a0 commit 07d8df5
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 12 deletions.
14 changes: 4 additions & 10 deletions packages/plugin-relay/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export class PothosRelayPlugin<Types extends SchemaTypes> extends BasePlugin<Typ
): GraphQLFieldResolver<unknown, Types['Context'], object> {
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;
Expand All @@ -42,13 +42,7 @@ export class PothosRelayPlugin<Types extends SchemaTypes> extends BasePlugin<Typ
const argMapper = createInputValueMapper(
argMappings,
(globalID, mappings, ctx: Types['Context'], info: GraphQLResolveInfo) =>
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) =>
Expand Down
14 changes: 12 additions & 2 deletions packages/plugin-relay/src/schema-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ schemaBuilderProto.nodeInterfaceRef = function nodeInterfaceRef() {
...this.options.relayOptions.nodeQueryOptions,
type: ref as InterfaceRef<unknown>,
args: {
id: t.arg.globalID({ required: true }),
id: t.arg.globalID({
required: true,
extensions: {
alwaysParse: true,
},
}),
},
resolve: resolveNodeFn
? (root, args, context, info) =>
Expand Down Expand Up @@ -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) =>
Expand Down
8 changes: 8 additions & 0 deletions packages/plugin-relay/tests/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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]!
Expand Down
35 changes: 35 additions & 0 deletions packages/plugin-relay/tests/examples/relay/schema/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
],
}),
);
30 changes: 30 additions & 0 deletions packages/plugin-relay/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,16 @@ describe('relay example schema', () => {
id
number
}
echoIDs(
globalID: "TnVtYmVyOjE="
numberThingID: "TnVtYmVyOjE="
genericNumberThingID: "TnVtYmVyOjE="
) {
id
typename
arg
idType
}
}
`;

Expand All @@ -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": {
Expand Down

0 comments on commit 07d8df5

Please sign in to comment.