Skip to content

Either warn or allow for IDs to contain : characters #18

@lencioni

Description

@lencioni

I was recently building a toy project with Relay and GraphQL where I needed to encode extra information in my global ID. To do this, I decided to add an idFetcher argument to my globalIdField call that returned a string that was delimited by a :. However, this did not work because of how globalIds are deconstructed in fromGlobalId, so instead of getting my encoded ID (e.g. 123:test) I just got the ID part (e.g. 123).

To prevent this confusion, it might be nice to either warn when this happens or use a different strategy in fromGlobalId than split. Here's a performant version that satisfies this use-case:

export function fromGlobalId(globalId: string): ResolvedGlobalId {
  var unbasedGlobalId = unbase64(globalId);
  var delimiterPos = unbasedGlobalId.indexOf(':');
  return {
    type: unbasedGlobalId.substring(0, delimiterPos),
    id: unbasedGlobalId.substring(delimiterPos + 1)
  };
}

Of course, you could also use a regex if you prefer (but I think I like the substring solution):

export function fromGlobalId(globalId: string): ResolvedGlobalId {
  var tokens = unbase64(globalId).match(/(.+?):(.+)/);
  return {
    type: tokens[1],
    id: tokens[2]
  };
}

Let me know what you think. I'll probably just submit a PR for this change for your convenience.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions