Skip to content

Commit

Permalink
Merge pull request hayes#799 from tgriesser/tgriesser/fix/colon-in-id
Browse files Browse the repository at this point in the history
feat: handle string contining ':' in global ID
  • Loading branch information
hayes authored Feb 22, 2023
2 parents a415921 + d60cb49 commit 3b47275
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/early-chefs-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@pothos/plugin-relay': minor
---

handle string contining ':' in global ID
5 changes: 3 additions & 2 deletions packages/deno/packages/plugin-relay/utils/global-ids.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/plugin-relay/src/utils/global-ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ export function encodeGlobalID(typename: string, id: bigint | number | string) {
}

export function decodeGlobalID(globalID: string) {
const [typename, id] = decodeBase64(globalID).split(':');
const decoded = decodeBase64(globalID).split(':');
const [typename, id] = decoded;

if (!typename || !id) {
throw new PothosValidationError(`Invalid global ID: ${globalID}`);
}

return { typename, id };
return { typename, id: decoded.length > 2 ? decoded.slice(1).join(':') : id };
}
7 changes: 7 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,11 @@ input GlobalIDInput {
otherList: [OtherInput!] = [{someField: \\"abc\\"}]
}
type IDWithColon implements Node {
id: ID!
idString: String!
}
type Mutation {
answerPoll(answer: Int!, id: ID!): Poll!
createPoll(answers: [String!]!, question: String!): Poll!
Expand Down Expand Up @@ -129,6 +134,8 @@ type Query {
batchNumbers(after: ID, before: ID, first: Int, last: Int): QueryBatchNumbersConnection!
cursorConnection(after: ID, before: ID, first: Int, last: Int): QueryCursorConnection!
extraNode: Node
idWithColon(id: ID!): IDWithColon!
idsWithColon(ids: [ID!]!): [IDWithColon!]!
inputGlobalID(id: ID!, inputObj: GlobalIDInput!, normalId: ID!): String!
moreNodes: [Node]!
node(id: ID!): Node
Expand Down
41 changes: 41 additions & 0 deletions packages/plugin-relay/tests/examples/relay/schema/numbers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { resolveArrayConnection, resolveOffsetConnection } from '../../../../src';
import builder from '../builder';

class IDWithColon {
id: string;

constructor(id: string) {
if (!id.includes(':')) {
throw new TypeError(`Expected id to have a colon, saw ${id}`);
}
this.id = id;
}
}

class NumberThing {
id: number;

Expand All @@ -17,6 +28,16 @@ class BatchLoadableNumberThing {
}
}

const IDWithColonRef = builder.node(IDWithColon, {
name: 'IDWithColon',
id: {
resolve: (n) => n.id,
},
fields: (t) => ({
idString: t.exposeString('id'),
}),
});

const NumberThingRef = builder.node(NumberThing, {
id: {
resolve: (n) => n.id,
Expand Down Expand Up @@ -207,6 +228,26 @@ builder.queryField('sharedEdgeConnection', (t) =>
),
);

builder.queryField('idWithColon', (t) =>
t.field({
type: IDWithColonRef,
args: {
id: t.arg.globalID({ required: true, for: [IDWithColonRef] }),
},
resolve: (root, args) => new IDWithColon(args.id.id),
}),
);

builder.queryField('idsWithColon', (t) =>
t.field({
type: [IDWithColonRef],
args: {
ids: t.arg.globalIDList({ required: true, for: [IDWithColonRef] }),
},
resolve: (root, args) => args.ids.map((id) => new IDWithColon(id.id)),
}),
);

builder.queryField('numberThingByID', (t) =>
t.field({
type: NumberThing,
Expand Down
22 changes: 22 additions & 0 deletions packages/plugin-relay/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,14 @@ describe('relay example schema', () => {
it('parses ids', async () => {
const query = gql`
query {
idWithColon(id: "SURXaXRoQ29sb246MTp0ZXN0") {
id
idString
}
idsWithColon(ids: ["SURXaXRoQ29sb246MTp0ZXN0", "SURXaXRoQ29sb246Mjp0ZXN0OmV4YW1wbGU="]) {
id
idString
}
numberThingByID(id: "TnVtYmVyOjE=") {
id
number
Expand Down Expand Up @@ -541,6 +549,20 @@ describe('relay example schema', () => {
expect(result).toMatchInlineSnapshot(`
{
"data": {
"idWithColon": {
"id": "SURXaXRoQ29sb246MTp0ZXN0",
"idString": "1:test",
},
"idsWithColon": [
{
"id": "SURXaXRoQ29sb246MTp0ZXN0",
"idString": "1:test",
},
{
"id": "SURXaXRoQ29sb246Mjp0ZXN0OmV4YW1wbGU=",
"idString": "2:test:example",
},
],
"invalid": null,
"invalidList": null,
"numberThingByID": {
Expand Down

0 comments on commit 3b47275

Please sign in to comment.