Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toGlobalId/fromGlobalId not working inside React Native #152

Closed
vincentisambart opened this issue Mar 22, 2017 · 4 comments · Fixed by #346
Closed

toGlobalId/fromGlobalId not working inside React Native #152

vincentisambart opened this issue Mar 22, 2017 · 4 comments · Fixed by #346

Comments

@vincentisambart
Copy link

I tried using this library's toGlobalId and fromGlobalId from a React Native application, however I got an error that Buffer is not defined (it's used in src/utils/base64.js). It seems that Buffer is only available from NodeJS...

If you consider that this library should also be available from React Native (or a browser), at least some changes need to be made to base64.js.

In the time being I'll probably just use join/split.

By the way, the reason I was trying to use this library from React Native is to do something similar to http://graphql.org/blog/rest-api-graphql-wrapper/.

@wincent wincent changed the title Not working inside React Native toGlobalId/fromGlobalId not working inside React Native Mar 22, 2017
@wincent
Copy link
Contributor

wincent commented Mar 22, 2017

Thanks for the report @vincentisambart.

In the time being I'll probably just use join/split.

This is a reasonable approach. Note that the use of base64 encoding is a convention but not a requirement. The main reason we have used base64 historically is to make it clear that the identifiers should be considered opaque, and the client should avoid doing any operations that depend on their internal structure; in many (most?) cases you can write a client without needing the functionality of toGlobalId/fromGlobalId at all. (At the same time, base64 is an easily enough removed form of obfuscation that you can trivially undo if you need to look "inside" an ID for debugging purposes.)

@blackxored
Copy link

I ran into exactly the same issue. Any ways to make this cross-platform?

@sibelius
Copy link
Contributor

@vincentisambart how did you solved your problem?

@sibelius
Copy link
Contributor

You can create a simple helper

import { Buffer } from 'buffer';

export type Base64String = string;

export function base64(i: string): Base64String {
  return new Buffer(i, 'utf8').toString('base64');
}

export const toGlobalId = (type: string, id: string): string => {
  return base64([type, id].join(':'));
};

IvanGoncharov added a commit to IvanGoncharov/graphql-relay-js that referenced this issue Jun 25, 2021
Previously we used Node's API for converting to/from base64
but it limits portability of this library to defirent enviroments
include Deno

Fixes graphql#152
IvanGoncharov added a commit that referenced this issue Jun 25, 2021
Previously we used Node's API for converting to/from base64
but it limits portability of this library to defirent enviroments
include Deno

Fixes #152
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants