单笔交易中的GraphQL变异操作 [英] GraphQL mutation operation in single transaction

查看:59
本文介绍了单笔交易中的GraphQL变异操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提供一个GraphQL服务,该服务在单个事务中运行整个变异操作(全部或全部不更改).

I want to provide a GraphQL service that runs the whole mutation operation in a single transaction (all or nothing changes).

在单个事务中依次处理顶级字段,并相应地更改数据库.只有在执行过程中没有错误时,才应将更改提交给数据库,否则应全部回滚.

The toplevel fields are processed serially in a single transaction and change the database accordingly. Only when there is no error during execution, the changes should be committed to the database, else they should all be rolled back.

现在我有两个问题:

  1. 根据GraphQL规范,顶层字段的串行执行意味着每个顶层字段都将填充一个值,该值反映修改后的状态,然后才能知道后面的字段是否导致错误.

  1. According to the GraphQL specification, serial execution of the toplevel fields means each toplevel field will be filled out with a value that reflects the state after its modification, before it is known if a later field causes an error. Is it intended that this is not the actual value of the field when a later toplevel field causes an error which rolls back the transaction?

如果顶级字段的更改失败,则执行以下顶级字段是没有意义的,因为事务可能处于阻止进一步处理的状态,并且所有更改都将被回滚.根据GraphQL语义,可以跳过那些字段的执行(返回null并返回错误列表的条目)吗?

If a mutation of a toplevel field fails, it makes no sense to execute the following toplevel fields, as the transaction may be in a state that prevents further processing and all changes will be rolled back anyway. Is it ok, according to the GraphQL semantic, to skip execution of those fields (returning null and maybe an entry to the errors list)?

推荐答案

恐怕您误解了GraphQL规范的这一部分.如果我理解您的问题,那么您将事务的数据库写入/更新操作视为单独的突变.

I'm afraid you misunderstood that part of GraphQL specification. If I understand your question, you are thinking of DB write/update operations of the transaction as separate mutations.

GraphQL具有一个突变根对象,该对象可以具有一个或多个顶级字段.这些顶级字段是单个突变.如果我们想要具有事务行为的服务,我们将其实现为单个变异.后端实现必须处理如何确保事务行为.

GraphQL has a mutation root object, which can have one or more top level fields. These top level fields are the individual mutations. If we want a service with transaction behavior, we'll implement it as a single mutation. The back-end implementation has to deal with how to ensure transactional behavior.

让您感到困惑的规范部分与当存在多个突变顶级字段(选择集)时发生的情况有关.每个突变都是独立的.

The part of the specification that got you confused pertains to what happens when there are multiple mutation top-level fields (selection set). Each mutation is independent.

这篇关于单笔交易中的GraphQL变异操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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