简体   繁体   English

如何在GraphQL中包含变异请求元数据?

[英]How to include mutation request metadata in GraphQL?

I'm building a GraphQL server from scratch with an API backend, replacing a REST API server. 我正在使用API​​后端从头开始构建GraphQL服务器,以取代REST API服务器。 Currently some requests in the existing API server, mainly Create / Update requests that will be mutations in GraphQL, include a request id which is used by the client. 当前,现有API服务器中的一些请求(主要是创建/更新请求)将是GraphQL中的突变,其中包括客户端使用的请求ID。 This request id is metadata about the request itself not part of the domain resource. 此请求ID是有关请求本身的元数据,而不是域资源的一部分。

My question is how to model / include request metadata in GraphQL queries and mutations? 我的问题是如何在GraphQL查询和变异中建模/包含请求元数据? My only solution so far is to make a request Type, which other types could include as a field so the request data can be included in the mutation return type. 到目前为止,我唯一的解决方案是创建一个请求类型,其他类型也可以将其作为字段包括在内,以便请求数据可以包含在变异返回类型中。 However i do not like this approach as it mixes request data into my domain types and this very much feels against the ethos of GraphQL. 但是我不喜欢这种方法,因为它将请求数据混合到我的域类型中,这在很大程度上与GraphQL的精神背道而驰。 Is there an accepted way to pass 'arbitrary' eg non-domain type data in the response of a query or mutation in GraphQL? 在GraphQL的查询或变异的响应中,是否存在接受的传递“任意”(例如非域类型)数据的方法?

Example - what i would like to avoid: 示例-我想避免的事情:

 type UserType { id: ID name: String request: RequestType // mixing request data in with domain type of User } type RequestType { id: ID } 

Update 更新资料

For others interested in this problem, based on the responses here I have decided that the GraphQL extensions key is good option for adding arbitrary data to a GraphQL response without the data becoming part of your data Graph. 对于对此问题感兴趣的其他人,根据此处的响应,我认为GraphQL extensions键是在GraphQL响应中添加任意数据而不使数据成为数据Graph的一部分的好选择。 In Express-GraphQL, docs on adding an extensions key to responses can be found here: https://github.com/graphql/express-graphql#providing-extensions . 在Express-GraphQL中,可以在以下位置找到有关为响应添加扩展键的文档: https : //github.com/graphql/express-graphql#providing-extensions A good overview of extensions here: https://marmelab.com/blog/2017/09/06/dive-into-graphql-part-iii-building-a-graphql-server-with-nodejs.html#server-logging 扩展的良好概述: https : //marmelab.com/blog/2017/09/06/dive-into-graphql-part-iii-building-a-graphql-server-with-nodejs.html#server-logging

That said, if the request metadata is conceptually part of the domain data then following Kashif's suggestion below of creating ResponseTypes that embed domain Types might be the correct approach. 就是说,如果请求元数据从概念上讲是域数据的一部分,那么按照Kashif的以下建议(创建嵌入域类型的ResponseType),这可能是正确的方法。

This kind of meta elements are usually passed in HEADERS with each request. 通常,每种请求都会在HEADERS中传递这种元元素。 And then you can handle those in your GraphQL server as you like 然后,您可以根据需要在GraphQL服务器中处理这些问题

For eg. 例如。

//Request Headers
...

X-Request-Id: <Your_Request_Id>

...

Updates 更新

Headers work both ways so there shouldn't be any problems using them on client too. 标头可以双向工作,因此在客户端上使用标头也应该没有任何问题。 Pull them out of the response whenever you need. 随时根据需要将它们拉出响应。

However if you really want the requestId in your mutation response then that IS a part of your domain. 但是,如果您确实希望在突变响应中加入requestId,那么这就是您域的一部分。 And there is nothing wrong in have a custom response type, Many existing GraphQL apis do have a custom response type like LoginResponse , RegisterResponse which then wraps around your domain objects but also include extra stuff in meta fields 拥有自定义响应类型没有什么问题,许多现有的GraphQL api确实具有自定义响应类型,例如LoginResponseRegisterResponse ,然后它包装您的域对象,但在meta字段中还包含其他内容

Since GraphQL was designed to be protocol agnostic, you should use your chosen protocol's metadata pattern. 由于GraphQL被设计为与协议无关,因此您应该使用所选协议的元数据模式。 If you're using HTTP, you should pass headers. 如果使用的是HTTP,则应传递标头。 If you're using WS/STOMP/MQTT/AMQP you can include metadata in each frame. 如果您使用的是WS / STOMP / MQTT / AMQP,则可以在每个帧中包含元数据。

Alternatively GraphQL has an extensions protocol . 或者,GraphQL具有扩展协议 You could add a request-id to your responses in the extensions object. 您可以在扩展对象中的响应中添加一个request-id Apollo uses extensions for caching, telemetry, etc. Apollo使用扩展进行缓存,遥测等。

We've been using GraphQL in production for about a year, we made the mistake of adding metadata into GraphQL and it gets hard to manage. 我们已经在生产中使用GraphQL大约一年了,我们犯了将元数据添加到GraphQL中的错误,并且变得难以管理。 Don't lose out on the benefits from the features of the protocol you're using. 不要失去正在使用的协议功能的好处。

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

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