简体   繁体   English

如何使用 GraphQL 突变进行更新(热巧克力)

[英]How to do updates with GraphQL mutations(Hot Chocolate)

We recently introduced GraphQL to our project, I've never used it before, however I like it a lot.我们最近在我们的项目中引入了 GraphQL,我以前从未使用过它,但是我非常喜欢它。 We decided to go with the HotChocolate library for .NET Core and Apollo for client side.我们决定使用 .NET Core 的 HotChocolate 库和客户端的 Apollo 来 go。

One thing I am not quite sure is about mutations, specifically peforming updates and partial updates with mutations.我不太确定的一件事是关于突变,特别是使用突变执行更新和部分更新。 I read somewhere that the practice is and that I should stick with creating specific mutation for each update , for instance updateUsername(), updateAddress(), updateCity() all of them should have specific mutation.我在某处读到这种做法,我应该坚持为每个 update 创建特定的突变,例如 updateUsername()、updateAddress()、updateCity() 所有这些都应该有特定的突变。

Issue with that is that my codebase will grow enormously if I decide to go in that direction, as we are very much data driven, with a lot of tables and columns per table.问题在于,如果我决定朝这个方向发展 go,我的代码库将大幅增长,因为我们非常依赖数据驱动,每个表有很多表和列。

Another question is, how to handle nullable properties, I can create a mutation which accepts some input object, but I'll end up with my entity being overwritten and all nullable properties not provided on the calling end will be set to null.另一个问题是,如何处理可为空的属性,我可以创建一个接受一些输入 object 的突变,但最终我的实体将被覆盖,并且调用端未提供的所有可为空的属性将设置为 null。 Is there a way to handle this update partially or I should go with specific update mutation for each property I want updated?有没有办法部分处理这个更新,或者我应该 go 对我想要更新的每个属性进行特定的更新突变?

I think you understood the best practice around specific mutations wrong.我认为您错误地理解了围绕特定突变的最佳实践。 It's less "have one mutation to update one field" and more "have specific mutations that encapsulate actions in your domain".它不是“有一个突变来更新一个字段”,而是更多的是“有特定的突变来封装你的域中的动作”。 A concrete example would be creating an "addItemToBasket" mutation, instead of having 3 mutations that update the individual tables related to your shopping basket, etc.一个具体的例子是创建一个“addItemToBasket”突变,而不是有 3 个突变来更新与您的购物篮等相关的各个表。

GraphQL is very much centered around front-end development, so your mutations should, in general, closely resemble actions a user can perform in your front-end. GraphQL 非常以前端开发为中心,因此您的突变通常应该与用户可以在您的前端执行的操作非常相似。 Eg your front-end has an "Add to basket" button and your backend has an "addItemToBasket" mutation that resembles the action of placing an item in the user's basket.例如,您的前端有一个“添加到购物篮”按钮,而您的后端有一个“addItemToBasket”突变,类似于将物品放入用户购物篮的动作。

If you design your mutations with this in mind, most of the time you shouldn't be having an issue with partial updates, since the mutation knows exactly what's to do and you are not just letting the user of your schema update fields at will.如果您在设计突变时考虑到这一点,那么大多数时候您不应该遇到部分更新的问题,因为突变确切地知道要做什么,而且您不只是让架构的用户随意更新字段。

If for whatever reason you need to have these partial updates, you likely won't get around implementing the patching yourself, unless your data provider supports it.如果出于某种原因您需要进行这些部分更新,除非您的数据提供商支持,否则您可能无法自行实施修补程序。 Meaning you will have to have an input object type with nullable properties and your mutation that decides which fields have been changed and changing them using your data provider.这意味着您将必须有一个输入 object 类型,该类型具有可为空的属性,并且您的突变决定哪些字段已更改并使用您的数据提供程序更改它们。

That being said, there's also a proposal for patching types in the Hot Chocolate repository that should simplify the patching part: https://github.com/ChilliCream/hotchocolate/issues/1326话虽如此,Hot Chocolate 存储库中还有一个修补类型的建议,应该简化修补部分: https://github.com/ChilliCream/hotchocolate/issues/1326

for instance updateUsername(), updateAddress(), updateCity() all of them should have specific mutation.例如 updateUsername(), updateAddress(), updateCity() 他们都应该有特定的突变。

Issue with that is that my codebase will grow enormously if I decide to go in that direction, as we are very much data driven, with a lot of tables and columns per table.问题在于,如果我决定朝这个方向发展 go,我的代码库将大幅增长,因为我们非常依赖数据驱动,每个表有很多表和列。

Correct.正确的。 That's practically impossible to follow that way for more or less big data-driven applications.对于或多或少的大数据驱动的应用程序,这实际上是不可能遵循的。 Consider how we implement the patching in our API here .考虑我们如何在API中实现补丁。 Also consider following the discussion about the patching feature in HotChocolate github thread.还可以考虑关注 HotChocolate github 线程中有关修补功能的讨论 Hope, that helps!希望,这有帮助!

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

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