简体   繁体   English

Flutter 将变量传递给 GraphQl 查询并出现错误

[英]Flutter passing variable to GraphQl query and getting error

This is a very simple GraphQl query which that work fine in graphql_playground or RestClient applications这是一个非常简单的GraphQl查询,可以在graphql_playgroundRestClient应用程序中正常工作

type Query {
    user(id: ID @eq): User @find(model:"App\\Models\\User")
}


type User {
    id: ID!
    name: String
    email: String
}

now inside Flutter i want to use this query with passing any id for user method and when i try to do this i get this error:现在在Flutter我想使用此query并为user方法传递任何id ,当我尝试执行此操作时,出现此错误:

The following SourceSpanException was thrown building UserList(dirty):
Error on line 1, column 23: Expected an argument name
  ╷
1 │           query{ user($id : Int!){
  │                       ^
  ╵

my code:我的代码:

class UserList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Query(
      options: QueryOptions(
        document: gql(r'''
          query{ user($id : Int!){
            name
            id
          }
          }
        '''),
        variables:{
          'id': 9,
        },
      ),
      builder: (
        QueryResult result, {
        Future<QueryResult> Function(FetchMoreOptions)? fetchMore,
        Future<QueryResult?> Function()? refetch
      }) {
        if (result.data != null){
          print(result.data);
        }
        /// return widget
      },
    );
  }
}

Your query in the client does not seem to be correct for handling variables.您在客户端中的查询对于处理变量似乎不正确。 Check https://graphql.org/learn/queries/#variables .检查https://graphql.org/learn/queries/#variables Here is how it probably should look like for your client:对于您的客户来说,它可能应该是这样的:

query GetUser($id: Int!) { 
    user(id : $id) {
        name
        id
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在将变量通过路由传递到Laravel中的控制器时出错 - Getting an error in passing a variable through routes to controller in laravel 将变量传递给同一个控制器出现错误“函数参数太少” - Passing variable to same controller getting error “Too few arguments to function” 错误将变量传递给视图 - Error Passing variable to view 出现错误:将变量传递到 Octobercms 闭包 function 时出现“未定义变量:var” - Getting Error: “Undefined Variable : var” when passing a variable into a Octobercms closure function 使用重定向/路由从控制器传递变量到视图-Laravel 5.2-获取未定义变量错误 - Passing variable from controller with redirect/ route to view- Laravel 5.2 - getting an undefined variable error 将PHP变量作为链接传递给视图上的定位标记时,出现未定义的变量错误 - Getting undefined variable error when passing a PHP variable as a link on an anchor tag on the view Laravel 将变量传递给 whereha 查询 - Laravel passing variable to wherehas query 将变量传递给查询会使用 Laravel 生成错误“尝试获取非对象的属性‘名称’” - Passing a variable to a query creates the error “Trying to get property 'name' of non-object” with Laravel 变量没有在查询中传递值,尽管它包含值 - variable not passing value in query though it contains value 在 mysql 查询中获得一个额外的变量 - getting a extra variable in mysql query
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM