Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How and when to generate an ID for graphql?

I am connecting graphql with an SQLite database.

In sql, ids are integers but in graphql ids are strings.

After searching, based on this question - When to use GraphQLID instead of GraphQLInt?

Stated are three suggestions:

  • Start using UUIDs for PKs. However, this has performance implications.
  • Ignore the implicit requirement (originating in the relayjs ecosystem) to have the ID unique across the application, and cast IDs to number when possible for internal consumption.
  • Hash Encode IDs on the application data layer, e.g. UUID base64 derived from a concatenation of table name & PK value.

And it's recommended the third. But i'm not sure where and how to implement it. Also, why is graphql ID made a String? The answer can affect how this ID part should be implemented.

like image 688
Ezekiel Chow Avatar asked Nov 03 '17 09:11

Ezekiel Chow


1 Answers

Why are GraphQL IDs serialized the same as strings?

As a data query language for APIs, it is desirable that GraphQL be specific where interoperability is concerned, but general otherwise to accommodate differing use cases. If you look at the quoted parts of the GraphQL specification in the answer you linked to, you can see it is specific about how data is communicated ("The ID type is serialized the same way as a String"), while being non-specific about what the content of the ID type is (saying only that it "represents a unique identifier", "often numeric" but not always, without going into detail). Making the ID type serialize as a string is the most straightforward way of providing for the communication of otherwise unspecified identifier data.

So while you do need to serialize your IDs as strings, what form those IDs take pre-serialization is up to you, as best fits your application.

How to implement the serialization

If you're going to be talking to something like Relay.js, where the IDs need to be globally unique, then I would suggest the third method, of concatenating the table name and primary key, then Base64 encoding the result. (This is how both GraphQL.js and Graphene do it.) Without knowing more about your application, I can't say much about where you should do the serialization, but here are a couple examples of how you might encode IDs:

# Node.js
var table_name = 'MyTable';
var primary_key = 1234;
var serialized_id = Buffer.from(table_name + ':' + primary_key).toString('base64');

# Python 3
import base64
table_name = 'MyTable'
primary_key = 1234
serialized_id = base64.b64encode((table_name + ':' + str(primary_key)).encode()).decode()

And how you might decode them:

# Node.js
var serialized_id = 'TXlUYWJsZToxMjM0';
var [table_name, primary_key] = Buffer.from(serialized_id, 'base64').toString('ascii').split(':');

# Python 3
serialized_id = 'TXlUYWJsZToxMjM0'
(table_name, primary_key) = base64.b64decode(serialized_id.encode()).decode().split(':')

Hope that helps!

like image 162
Sean Bolton Avatar answered Oct 27 '22 01:10

Sean Bolton