具有动态键的对象的Apollo/GraphQL字段类型 [英] Apollo/GraphQL field type for object with dynamic keys

查看:230
本文介绍了具有动态键的对象的Apollo/GraphQL字段类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我的graphql服务器希望以JSON格式获取以下数据,其中person3person5是一些ID:

Let's say my graphql server wants to fetch the following data as JSON where person3 and person5 are some id's:

"persons": {
  "person3": {
    "id": "person3",
    "name": "Mike"
  },
  "person5": {
    "id": "person5",
    "name": "Lisa"
  }
}

问题:如何使用阿波罗创建模式类型定义?

Question: How to create the schema type definition with apollo?

此处的键person3person5是根据我的查询(即查询中使用的area)动态生成的.因此,在另一时间,我可能会返回person1person2person3. 如您所见persons不是Iterable,因此以下内容无法用作我对apollo所做的graphql类型定义:

The keys person3 and person5 here are dynamically generated depending on my query (i.e. the area used in the query). So at another time I might get person1, person2, person3 returned. As you see persons is not an Iterable, so the following won't work as a graphql type definition I did with apollo:

type Person {
  id: String
  name: String
}
type Query {
  persons(area: String): [Person]
}

persons对象中的键可能总是不同.

The keys in the persons object may always be different.

当然,一种解决方案是将传入的JSON数据转换为对persons使用数组,但是没有办法像这样处理数据吗?

One solution of course would be to transform the incoming JSON data to use an array for persons, but is there no way to work with the data as such?

推荐答案

GraphQL依赖于服务器和客户端,它们提前知道每种类型都有哪些可用字段.在某些情况下,客户端可以(通过自省)发现这些字段,但是对于服务器,始终需要提前知道它们.因此,以某种方式基于返回的数据动态生成那些字段实际上是不可能的.

GraphQL relies on both the server and the client knowing ahead of time what fields are available available for each type. In some cases, the client can discover those fields (via introspection), but for the server, they always need to be known ahead of time. So to somehow dynamically generate those fields based on the returned data is not really possible.

可以使用自定义的 JSON标量(graphql -type-json模块),然后将其返回给您的查询:

You could utilize a custom JSON scalar (graphql-type-json module) and return that for your query:

type Query {
  persons(area: String): JSON
}

通过使用JSON,您可以绕过返回的数据以适合任何特定结构的要求,因此,只要格式正确的JSON,您就可以发回任何想要的信息.

By utilizing JSON, you bypass the requirement for the returned data to fit any specific structure, so you can send back whatever you want as long it's properly formatted JSON.

当然,这样做有很多缺点.例如,您丢失了以前使用的类型提供的安全网(实际上,任何结构都可以返回,如果返回的结构不正确,则直到客户尝试之前,您都无法找到它.使用它并失败).您还将失去对返回的数据中的任何字段使用解析器的功能.

Of course, there's significant disadvantages in doing this. For example, you lose the safety net provided by the type(s) you would have previously used (literally any structure could be returned, and if you're returning the wrong one, you won't find out about it until the client tries to use it and fails). You also lose the ability to use resolvers for any fields within the returned data.

但是...你的葬礼:)

But... your funeral :)

顺便说一句,在将数据发送回客户端之前,我会考虑将数据展平到一个数组中(就像您在问题中所建议的那样).如果您正在编写客户端代码,并使用动态大小的客户列表,则使用数组而不是使用ID键入键的对象会容易得多.例如,如果您正在使用React,并为每个客户显示一个组件,您最终将把该对象转换为数组以进行映射.在设计您的API时,与避免避免对数据进行额外处理相比,我将使客户端可用性成为更高的考虑.

As an aside, I would consider flattening out the data into an array (like you suggested in your question) before sending it back to the client. If you're writing the client code, and working with a dynamically-sized list of customers, chances are an array will be much easier to work with rather than an object keyed by id. If you're using React, for example, and displaying a component for each customer, you'll end up converting that object to an array to map it anyway. In designing your API, I would make client usability a higher consideration than avoiding additional processing of your data.

这篇关于具有动态键的对象的Apollo/GraphQL字段类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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