编写动态模式以在Graphql中返回相同的结果 [英] Write dynamic schema for return same result in Graphql
问题描述
当前,该架构可以正常工作并提供所需的结果
Currently this schema working properly and give required result
type Personal{
userId:String
name: String
email: String
}
type Contact{
mobile_no:String
email:String
}
type Address{
address:String
lat:String
long:String
}
getContact(userId: String): Contact
getPersonal(userId: String): Personal
getAddress(userId: String): Address
但是我想返回
type Response {
status: Boolean
message: String
data: []
}
使用状态和消息键返回数据,其中数据包含联系人,个人和地址对象的数组.
Return data with status and message key where data hold an array of Contact, Personal and Address objects.
不编写ResponseContact,ResponsePersonal和ResponseAddress
without writing ResponseContact, ResponsePersonal and ResponseAddress
我有想法在这样的数据中返回scalar JSON
I have Idea to return scalar JSON
in data like this
scalar JSON
type Response {
status: Boolean
message: String
data: [JSON]
}
但是这种模式的问题我不能使用graphql第二个要点问您想要什么" 所需结果
But the problem with this schema I can't use graphql second main point "Ask for what you want" Required result
type ResponseAddress {
status: Boolean
message: String
data: [Address]
}
type ResponsePersonal {
status: Boolean
message: String
data: [Personal]
}
type ResponseContact {
status: Boolean
message: String
data: [Contact]
}
getContact(userId: String): ResponseContact
getPersonal(userId: String): ResponsePersonal
getAddress(userId: String): ResponseAddress
不编写ResponseAddress,ResponsePersonal和ResponseContact.
without writing ResponseAddress, ResponsePersonal and ResponseContact.
类似的东西
type Response {
status: Boolean
message: String
data: [Address|Personal|Contact]
}
getContact(userId: String): Response
getPersonal(userId: String): Response
getAddress(userId: String): Response
当然上面的语法是错误的.
Of course above syntax are wrong.
为什么:-因为我想返回此响应更多的位置,并且不想使用较长的模式.
Why:- Because I want to return this Response more places and don't want to long schema.
要点:-这可能吗?
推荐答案
可以通过使用接口或联合将多个类型分配给单个字段:
Assigning multiple types to a single field is possible by using either an interface or an union:
union ResponseData = Address | Personal | Contact
type Response {
status: Boolean
message: String
data: ResponseData
}
请记住,查询此字段时,客户端将需要利用内联片段以指定针对每种可能的类型请求哪些字段:
Keep in mind, when querying this field, the client will need to utilize inline fragments in order to specify which fields to request for each possible type:
query {
getContact {
status
message
data {
... on Address {
# Address field here
}
... on Personal {
# Personal field here
}
... on Contact {
# Contact field here
}
}
}
}
您可以查看文档有关如何同时实现联合和接口的详细说明.请记住,您只能创建对象类型的并集,因此,如果需要返回列表的响应,则需要为响应定义至少两种类型:
You can check out the docs for a detailed explanation of how to implement both unions and interfaces. Keep in mind that you can only create a union of object types, so if you need a response that returns a list, you'll need to define at least two types for your responses:
type Response {
status: Boolean
message: String
data: ResponseData
}
type ListResponse {
status: Boolean
message: String
data: [ResponseData]
}
注意:以这种方式使用联合会在客户端上增加一些复杂性,我通常会说仅仅拥有一个较小的架构是不值得的.诸如GraphiQL和GraphQL Playground之类的工具使处理大型模式对于用户而言变得轻而易举.如果您需要的话,拥有具有冗余类型的大型架构并不是一件坏事.
Note: Using unions this way does add some complexity on the client-side and I would say generally that's not worth it just to have a smaller schema. Tools like GraphiQL and GraphQL Playground make working with large schemas a breeze for the consumer. Having a large schema with redundant types is not a bad thing if that's what you need.
这篇关于编写动态模式以在Graphql中返回相同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!