Nandin Borjigin
Nandin Borjigin

Reputation: 2154

Can GraphQL schema be used as database schema definition?

I'm working on a personal project (quite simple at the server side) and planning to use GraphQL (for the first time). I kinda understand that GraphQL is not an ORM and thus GraphQL schema is something different than database schema. But for my simple project, the overall features at the server side are just basic CRUD and there likely to be only one table for now. For this scenario, I really dont wan't to repeat the schema definition in GraphQL and some ORM. Could these two schemas be unified? and how should I achieve that?

P.S. I plan to use NodeJS for the server side development.

Upvotes: 2

Views: 1976

Answers (1)

JosephHall
JosephHall

Reputation: 701

Based on personal experience designing and implementing GraphQL services, I'd like to suggest that you think about schemas a bit differently. Here's why:

Your DB schema defines the strict parameters that your DB needs to store data, structure relationships, and query information effectively, specific to the kind of DB you are using. Your DB schema plays a big part in how your app sanely manages persisted data.

On the other hand, your GraphQL schema describes the shape/type/structure of data that you can get from your server, and what you can do with it -- this could be data coming from your DB, an external API, other services with which your server communicates, etc.

More importantly, your GraphQL schema describes how an ordinary human would want to interact with your server, without having to understand the internal details of how/where/why your server gets its data.

Your GraphQL schema shouldn't be unified with your DB schema simply because it is 1) not a DB, and 2) not representing the internal structure of your DB -- it is representing any data coming from your server, agnostic to where that data originated.

Even if you're building a simple CRUD app with a single table and no "outside" data, there is still value in thinking about the DB and GraphQL schemas separately.

First, think about what your ideal GraphQL API would look like. After all, it represents the features and data you are exposing to the end user. What is that data, and what would that data look like? What are the GraphQL types, queries, mutations, subscriptions, etc. I need to define to make that happen?

Now that you've used your API Schema to define how you, ideally, want to interact with your server, design the DB schema to represent what data you want to create, read, update, and delete.

You might find that your GraphQL and DB schemas end up similarly defining the same data, especially with really simple apps -- but chances are, if you design with the "graphql schema first" mindset, you'll probably be happy to forget about that the moment your app scales, you introduce more relational data, your schema becomes more complicated, or all of the above :)

However...you might want to have a look at this great library, which is currently still in early development, https://github.com/danielrearden/sqlmancer, which allows you to translate GraphQL statements into SQL Queries (not a DB schema!)

Upvotes: 10

Related Questions