Flutter GraphQLError field requires type String, found String

665 Views Asked by At

I'm new in GraphQl in flutter and i would like to use this query which i implemented in Laravel:

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

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

this query work fine for me when i'm testing that, but when i try to implementing that on Flutter i get this error:

I/flutter (20938): OperationException(linkException: null, graphqlErrors: [GraphQLError(message: Field "loginAccount" argument "mobile_number" requires type String, found String., locations: [ErrorLocation(line: 3, column: 31)], path: null, extensions: {category: graphql})])

my code:

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();
            }
        ));
  }
}

1

There are 1 best solutions below

3
On

your error is here,

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

you need a number but you are passing String(here String is not going as variable that you can assign letter it going constant String).

i did something like this in my project where "${txt.text}" is going as dynamic variable

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

also you can try query in some console first and then run in flutter.