【问题标题】:GraphQL alias top-level data into a nested objectGraphQL 将顶级数据别名到嵌套对象中
【发布时间】:2019-08-02 00:55:33
【问题描述】:

目前我正在currentUser 查询city

query user {
  currentUser {
    city
  }
}

但是我的前端的要求需要city 作为一个包含labelvalue 的对象。

// this doesn’t work, but is the idea…
query user {
  currentUser {
    city {
      label: city
      value: city
    }
  }
}

这可以在 GraphQL 查询级别执行吗?

【问题讨论】:

    标签: graphql schema graphql-js


    【解决方案1】:

    我没有看到你的数据库模型,但你可以有一个City 模型,类似于:

    type City {
      id: ID! @unique
      label: String
      value: String
    }
    

    然后,在您的 User 模型中,您可以执行以下操作:

    type User {
      id: ID! @unique
      .... other User fields
      city: City
    }
    

    使用该架构,您将能够查询用户内部的城市值:

    query user {
      currentUser {
        city {
          label
          value
        }
      }
    }
    

    您应该根据自己的需要调整它,但这是我认为您正在寻找的主要思想。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-23
      • 2023-01-12
      • 2019-10-12
      • 1970-01-01
      • 1970-01-01
      • 2011-12-13
      • 2020-12-23
      • 2017-09-14
      相关资源
      最近更新 更多