在GraphQL中,您可以更改别名的输出结构吗? [英] In GraphQL, can you change the structure of the output in an alias?

查看:47
本文介绍了在GraphQL中,您可以更改别名的输出结构吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个类似以下内容的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屋!

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