Skip to content

Conversation

@lencioni
Copy link
Contributor

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. In
this commit, I swap out the split for a performant
indexOf/substring approach that satisfies this use-case. Of course,
a regex version would also work here:

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

Fixes #18

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`. In
this commit, I swap out the `split` for a performant
`indexOf`/`substring` approach that satisfies this use-case. Of course,
a regex version would also work here:

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

Fixes graphql#18
@dschafer
Copy link
Contributor

Awesome, thanks!

dschafer added a commit that referenced this pull request Aug 28, 2015
Allow for IDs to contain `:` characters
@dschafer dschafer merged commit e4f019c into graphql:master Aug 28, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants