【问题标题】:Flutter GraphQLError field requires type String, found StringFlutter GraphQL 错误字段需要类型 String,找到 String
【发布时间】:2021-09-21 22:32:15
【问题描述】:

我是 GraphQl 的新手,我想使用我在 Laravel 中实现的这个查询:

type Query {
    loginAccount(mobile_number:String
        @rules(apply:["required","min:11","max:11"])
    ):ResponseResultWithMessage
}

type ResponseResultWithMessage{
    result:Int,
    title:String
    description:String
}

当我测试它时,这个查询对我来说很好,但是当我尝试在 Flutter 上实现它时,我得到了这个错误:

I/flutter (20938): OperationException(linkException: null, graphqlErrors:[GraphQLError(消息:字段“loginAccount” 参数“mobile_number”需要字符串类型,找到字符串。, 位置:[ErrorLocation(行:3,列:31)],路径:空,扩展: {类别:graphql})])

我的代码:

class _Login extends State<Login> {
  final _formKey = GlobalKey<FormState>();
  final sample = r'''
      query {
        loginAccount(mobile_number:String) {
          result
          title
          description
        }
      }
  ''';

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        //...
        body: Query(
            options: QueryOptions(document: gql(sample), variables:<String,dynamic> {
              'mobile_number': '0123456789',
            }),
            builder: (QueryResult result, { VoidCallback refetch, FetchMore fetchMore }) {
              if (result.hasException) {
                debugPrint(result.exception.toString());

                return Text(result.exception.toString());
              }

              if (result.isLoading) {
                return const Text('Loading');
              }

              debugPrint('$result');

              return Container();
            }
        ));
  }
}

【问题讨论】:

  • 你能添加更多关于你的服务器端实现的信息吗?您使用的是哪个确切的 PHP 包?似乎该指令正在将输入类型 String 更改为不同的类型但具有相同的名称(想象有两个具有相同名称的不同类不等于另一个)。

标签: flutter dart graphql flutter-graphql


【解决方案1】:

你的错误在这里,

query {
        loginAccount(mobile_number:String) {    //<-- mobile_number:String error!!
          result
          title
          description
        }
      }

您需要一个数字,但您传递的是字符串(这里的字符串不是变量,您可以将其分配为常量String)。

我在“${txt.text}”作为动态变量的项目中做了类似的事情

 query recipes {
  recipes(where: {name: {_similar: "${txt.text}"}}) {
    id
    name
    description
    ingredients {
      id
      name
    }
  }
}

您也可以先尝试在某个控制台中查询,然后在 Flutter 中运行。

【讨论】:

  • 你的意思是variables:&lt;String,dynamic&gt; { 'mobile_number': '0123456789',}应该是`variables:&lt;String,dynamic&gt; { 'mobile_number': '${mobile_number.text}',}?
  • 是的,但是让我知道您使用什么插件进行 graphql 查询我正在使用 hasura_connect,这对我来说很好
  • 我们使用 Laravel 在服务器上实现了 GraphQL
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-17
  • 2021-05-10
  • 2015-04-03
  • 1970-01-01
  • 2023-03-14
  • 2020-02-14
  • 2019-03-23
相关资源
最近更新 更多