【问题标题】:NodeJs GraphQL dynamic query variableNodeJs GraphQL 动态查询变量
【发布时间】:2018-04-08 23:38:16
【问题描述】:

https://launchpad.graphql.com/9qvqz3v5r

这是我的 graphQL 示例

如果我将 userId:2 作为静态传递

{
  user(userId:2){
    firstName
    lastName
  }
}

我会得到如下输出

{
  "data": {
    "user": {
      "firstName": "Joe",
      "lastName": "spin"
    }
  }
}

我需要使用动态查询变量的相同输出

query getUserNameData($userId: User){
  user(userId:$userId){
    firstName
    lastName
  }
}
query variables
{
  "userId" : 2
}

但我是吸气剂错误: 变量\"$userId\"不能是非输入类型\"User http://prntscr.com/h2ojor

谁能帮帮我?

【问题讨论】:

    标签: node.js graphql graphql-js apollo-server express-graphql


    【解决方案1】:

    http://prntscr.com/h2oop9

    您必须使用 ! 指定数据类型这个符号来获取变量

    query getUserNameData($userId: Int!) {
      user(userId: $userId) {
        firstName
        lastName
      }
    }
    

    【讨论】:

    • 您能否举例说明如何拥有动态字段,例如firstNamelastName
    猜你喜欢
    • 1970-01-01
    • 2018-03-02
    • 2019-05-17
    • 2023-03-12
    • 2018-12-21
    • 2018-02-27
    • 1970-01-01
    • 2021-02-14
    • 2019-01-07
    相关资源
    最近更新 更多