Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js NodeJs GraphQL动态查询变量_Node.js_Graphql_Graphql Js_Apollo Server_Express Graphql - Fatal编程技术网

Node.js NodeJs GraphQL动态查询变量

Node.js NodeJs GraphQL动态查询变量,node.js,graphql,graphql-js,apollo-server,express-graphql,Node.js,Graphql,Graphql Js,Apollo Server,Express Graphql,下面是我的graphQL示例 如果我将userId:2作为静态传递 { user(userId:2){ firstName lastName } } 我将得到如下输出 { "data": { "user": { "firstName": "Joe", "lastName": "spin" } } } 我需要使用动态查询变量的相同输出 query getUserNameData($userId: User){ user

下面是我的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”

有人能帮我吗?

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

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

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

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

请举例说明如何设置动态字段,如
firstName
lastName
?请举例说明如何设置动态字段,如
firstName
lastName