[英]GraphQL API: mutation or query?
I'm developing a GraphQL API in which there is an import feature with a preliminary data comparison (between existing data & the imported file) and a subsequent confirmation request我正在开发一个 GraphQL API ,其中有一个导入功能,具有初步数据比较(现有数据和导入文件之间)和后续确认请求
The current flow is:目前的流量是:
query previewImport($data: String)
where $data
is a base64-encoded spreadsheet. API 消费者执行 GQL 查询query previewImport($data: String)
其中$data
是 base64 编码的电子表格。 The data is parsed in the back-end and a record with a guid is inserted in the database.数据在后端解析,并在数据库中插入带有 guid 的记录。 Finally the API returns a diff result between the data in the spreadsheet and the data in database, and the guid.最后 API 返回电子表格中的数据和数据库中的数据之间的差异结果,以及 guid。mutation confirmImport($guid: String)
is made to the API and the changes are persisted in the database.为此,对 API 进行请求mutation confirmImport($guid: String)
并将更改保存在数据库中。 So I'm wondering: should previewImport
be a query or a mutation ?所以我想知道: previewImport
应该是查询还是突变? From the API consumer's point of view, it is definitely a query.从 API 消费者的角度来看,这绝对是一个查询。 But in the back-end DB a record is inserted, albeit with the only purpose to be linked to a confirmImport request shortly thereafter, and then deleted.但是在后端数据库中插入了一条记录,尽管其唯一目的是在此后不久链接到 confirmImport 请求,然后将其删除。
In the GQL Mutation spec it is said that:在 GQL Mutation 规范中,据说:
It is expected that the top level fields in a mutation operation perform side‐effects on the underlying data system.预计突变操作中的顶级字段会对底层数据系统产生副作用。
Technically, the temp record does affect the data system.从技术上讲,临时记录确实会影响数据系统。 But domain-wise, it changes nothing to the entities that are important to the app so I'm still kind of in the dark但是在领域方面,它不会改变对应用程序很重要的实体,所以我仍然有点摸不着头脑
I agree that query previewImport
is more intuitive, provided the preview operation is idempotent.我同意query previewImport
更直观,前提是预览操作是幂等的。
A query
suggests not just "read, not write", but also "I can call this all day long without error".一个query
不仅建议“读取,而不是写入”,还建议“我可以整天无误地调用它”。 A mutation
, on the other hand, typically persists a change and informs me if I try to repeat the same operation, either as an error or an "already processed, you idiot" response.另一方面, mutation
通常会持续更改并通知我是否尝试重复相同的操作,无论是作为错误还是“已经处理,你这个白痴”响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.