在 GraphQL 中,您可以更改别名中的输出结构吗? [英] In GraphQL, can you change the structure of the output in an alias?
问题描述
假设我有一个如下所示的 GraphQL 查询:
Let's say I've got a GraphQL query that looks like this:
query {
Todo {
label
is_completed
id
}
}
但是从这个查询中消费数据的客户端需要一个有点不同的数据结构——例如一个 TypeScript 接口,如:
But the client that consumes the data from this query needs a data structure that's a bit different- e.g. a TypeScript interface like:
interface Todo {
title: string // "title" is just a different name for "label"
data: {
is_completed: boolean
id: number
}
}
使用别名将 label
作为 title
返回是很容易的.但是有没有办法让它在一个名为 data
的别名下同时返回 is_completed
和 id
?
It's easy enough to just use an alias to return label
as title
. But is there any way to make it return both is_completed
and id
under an alias called data
?
推荐答案
没有办法做到这一点.要么更改架构以反映客户端的需求,要么在客户端获取响应后转换响应.
There is no way to do that. Either change the schema to reflect the client's needs or transform the response after it is fetched on the client side.
这篇关于在 GraphQL 中,您可以更改别名中的输出结构吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!