GraphQL模式可以用作数据库模式定义吗? [英] Can GraphQL schema be used as database schema definition?

查看:45
本文介绍了GraphQL模式可以用作数据库模式定义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个个人项目(在服务器端非常简单),并计划使用GraphQL(第一次).我有点理解GraphQL不是ORM,因此GraphQL模式与数据库模式有所不同.但是对于我的简单项目,服务器端的总体功能只是基本的CRUD,目前可能只有一张表.对于这种情况,我真的不想重复GraphQL和一些ORM中的架构定义.这两个方案可以统一吗?以及我应该如何实现呢?

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.我打算将NodeJS用于服务器端开发.

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

推荐答案

基于个人设计和实现GraphQL服务的经验,我建议您对架构的看法有所不同.原因如下:

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.

另一方面,您的GraphQL模式描述了您可以从服务器获取的数据的形状/类型/结构,以及该数据可以做什么—这可能是来自数据库,外部API,服务器与之通信的其他服务,等等.

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.

更重要的是,您的GraphQL模式描述了普通人如何与服务器交互,而不必了解服务器如何/何地/为什么获取数据的内部细节.

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.

您的GraphQL模式不应仅仅因为以下原因而与您的数据库模式统一:1)不是数据库,并且2)不代表数据库的内部结构-它代表来自服务器的任何数据,与数据的来源.

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.

即使您要构建具有单个表且没有外部"内容的简单CRUD应用,数据,分别考虑DB和GraphQL模式仍然有价值.

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.

首先,考虑一下理想的GraphQL API的外观.毕竟,它代表了您要向最终用户公开的功能和数据.那是什么数据,那数据看起来像什么?什么是GraphQL类型,查询,变异,订阅等,我需要定义使其实现?

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?

现在,您已经使用API​​架构定义了理想情况下如何与服务器交互,设计数据库架构以表示要创建,读取,更新和删除哪些数据.

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.

您可能会发现,您的GraphQL和DB模式最终以相似的方式定义了相同的数据,尤其是对于非常简单的应用程序-但是,如果您首先使用"graphql模式"进行设计,则可能会出现这种情况.思维定势,您可能会很乐意忘记应用程序扩展,引入更多的关系数据,架构变得更加复杂或以上所有情况的那一刻:)

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 :)

但是...您可能想看看这个仍在早期开发中的出色库, https://github.com/danielrearden/sqlmancer ,它使您可以将GraphQL语句转换为SQL查询(不是数据库模式!)

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!)

这篇关于GraphQL模式可以用作数据库模式定义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆