简体   繁体   English

为什么不使用GraphQL作为业务逻辑层?

[英]Why not to use GraphQL as a business logic layer?

I was wondering how to separate transport layer (REST API / GraphQL) from business logic layer when writing application. 我想知道在编写应用程序时如何将传输层(REST API / GraphQL)与业​​务逻辑层分开。 When implementing business logic function/method, for example postCreate , it might look like this: 在实现业务逻辑功能/方法时,例如postCreate ,可能看起来像这样:

async function postCreate (viewer, params) {
  // validate params (don't allow additional params!)
  // authorize viewer
  // filter/modify/authorize params according to viewer role
  // perform some logic
  // filter output according to viewer role
  // return result
}

If I would like to keep GraphQL away from business logic I would have to implement all actions performed in postCreate function by myself (or use third party libs). 如果我想让GraphQL远离业务逻辑,则必须自己执行在postCreate函数中执行的所有操作(或使用第三方库)。 Also if postCreate function would return nested data like for example post.author.firends then I would have to deal with a complicated graph-like structure in postCreate function params. 另外,如果postCreate函数将返回嵌套数据(例如post.author.firends那么我将不得不在postCreate函数参数中处理复杂的类似于图的结构。

On the other hand, with GraphQL writing such function is easy, because there is input/output validation/filtering out of the box, dealing with nested data is also easy, authorization can be done using GraphQL resolver context argument, and so on. 另一方面,使用GraphQL编写该函数很容易,因为开箱即用即可进行输入/输出验证/过滤,处理嵌套数据也很容易,可以使用GraphQL解析器context参数进行授权,依此类推。

The longer I think of it, the longer I am convinced that GraphQL is ideal for writing business logic. 我思考的时间越长,我就越相信GraphQL是编写业务逻辑的理想选择。 The fact that it is possible to expose GrpahQL api through HTTP is just a nice feature. 可以通过HTTP公开GrpahQL api的事实只是一个不错的功能。 And event I would want to make a standard REST API I could just call GraphQL from http routes, for example like this: 而且,如果我想创建一个标准的REST API,我可以从http路由调用GraphQL,例如:

app.post('/posts', async function (req, res, next) {
  const query = `mutation ($input: PostCreateData!) { postCreate (input: $input) { id, title } }`;
  const variables = { input: req.body };
  await graphql({ query, variables }); 
})

Of course it's the very simple example - in real world we would have to implement some extra params that would represent fields (possibly nested) that user would like to receive in response, handle errors properly and so on. 当然,这是一个非常简单的示例-在现实世界中,我们将必须实现一些额外的参数,以表示用户希望接收的字段(可能是嵌套的)以响应,正确地处理错误等。

Anyway my question is not about REST API because right now in 99% I write only GraphQL. 无论如何,我的问题不是关于REST API,因为现在在99%的情况下,我仅编写GraphQL。 The question is - why not to use GraphQL in business logic layer? 问题是-为什么不在业务逻辑层中使用GraphQL? The only drawback that comes to my mind is that if I would like to call some business logic method from "inside" of my app I would have to call it with GraphQL query which feels little bit awkward - but I guess this could be solved by writing GraphQL queries as plain objects (json) and translated to GraphQL... 我想到的唯一缺点是,如果我想从应用程序的“内部”调用某些业务逻辑方法,则必须使用GraphQL查询来调用它,这有点尴尬-但是我想这可以通过解决将GraphQL查询作为纯对象(json)编写并转换为GraphQL ...

What do you guys think? 你们有什么感想? Do you use GraphQL for business logic? 您是否将GraphQL用于业务逻辑?

You may have answered your own question: 您可能已经回答了自己的问题:

The only drawback that comes to my mind is that if I would like to call some business logic method from "inside" of my app I would have to call it with GraphQL query which feels little bit awkward 我想到的唯一缺点是,如果我想从应用程序的“内部”调用某些业务逻辑方法,则必须使用GraphQL查询来调用它,这有点尴尬

The structure of GraphQL resolvers can be incredibly easy to follow, and you should certainly take advantage of that. GraphQL解析器的结构非常易于遵循,您当然应该利用它。

Some people end up using an ORM to handle business logic, however, you can use a functional programming interface to structure your business logic if that is easier for you. 有些人最终使用ORM来处理业务逻辑,但是,如果您更方便,则可以使用功能性编程接口来构造业务逻辑。

The most important part is that your business logic be callable directly without going through GraphQL. 最重要的部分是无需通过GraphQL即可直接调用您的业务逻辑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM