I am building an application using:
- MySQL as the backend database
- Apollo GraphQL server as a query layer for that database
- Sequelize as the ORM layer between GraphQL and MySQL
As I am building out my GraphQL schema I'm using the GraphQL ID data type to uniquely identify records. Here's an example schema and its MySQL resolver/connector
Graphql Type:
type Person {
id: ID!
firstName: String
middleName: String
lastName: String
createdAt: String
updatedAt: String
}
Sequelize connector
export const Person = sequelize.define('person', {
firstName: { type: Sequelize.STRING },
middleName: { type: Sequelize.STRING },
lastName: { type: Sequelize.STRING },
});
GraphQL resolver:
Query: {
person(_, args) {
return Person.findById(args.id);
}
So that all works. Here's my question. GraphQL seems to treat the ID
type as a string. While the ID value gets stored in the MySQL database as an INT
by Sequelize. I can use GraphQL to query the MySQL db with either a string or a integer that matches the ID value in the database. However, GraphQL will always return the ID value as a string.
How should I be handling this value in the client? Should I always convert it to an integer as soon as I get it from GraphQL? Should I modify my sequelize code to store the ID value as a string? Is there a correct way to proceed when using GraphQL IDs like this?