'Graphql_flutter runMutation's QueryResult is returning null, though the mutation is executing correctly
I'm using graphql_flutter 3.1.0 and have started the authentication for my app and am needing to set the user in my Provider state on successful creation. My mutation to CreateUser is currently:
final String createUserMutation = """
mutation CreateUser(\$firstName: String!, \$username: String!, \$phoneNumber: String!) {
CreateUser(firstName: \$firstName, username: \$username, phoneNumber: \$phoneNumber, role: USER) {
id,
firstName,
username,
phoneNumber,
role
}
}
""";
This successfully creates the user in my Neo4j database. If I run that same query in my GraphQL interface, I get the response exactly as expected:
{
"data": {
"CreateUser": {
"id": "e2a44525-fd46-4aae-8e00-38d263b769e6",
"firstName": "Sam",
"username": "sgamgee",
"phoneNumber": "3216543216",
"role": "USER"
}
}
}
In my SignupForm
widget I am wrapping the form in Mutation
and passing the createUserMutation
to the MutationOptions
like so:
class SignupFormState extends State<SignupForm> {
final _formKey = GlobalKey<FormState>();
final user = User();
@override
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(35),
child: Mutation(
options: MutationOptions(
documentNode: gql(createUserMutation)
),
builder: (RunMutation runMutation, QueryResult result) {
return UserCreateForm(
formKey: _formKey,
user: user,
runMutation: runMutation,
result: result
);
},
)
);
}
}
All is well so far, no errors. When I attempt to submit the form via an onPressed closure:
onPressed: () async {
final form = _formKey.currentState;
if (form.validate()) {
form.save();
runMutation({
'firstName': user.firstName,
'username': user.username,
'phoneNumber': user.phoneNumber
});
if (result.hasException) {
return AlertBox(
type: AlertType.error,
text: result.exception.toString()
);
} else if (result.loading) {
return const Center(
child: CircularProgressIndicator(),
);
} else {
// NOTE: result.data is null though the mutation is running successfully.
var createdUser = result.data['CreateUser'];
Provider.of<UserProvider>(context, listen: false).signUp(createdUser);
if (Provider.of<UserProvider>(context, listen: false).isAuthenticated()) {
Navigator.pushReplacementNamed(context, '/signup-success');
} else {
Navigator.pushReplacementNamed(context, '/signup');
}
}
}
}
The result.data is consistently null. I know neither runMutation
or result
are futures and thus cannot be awaited. Also hasException
and loading
are present and false or true as the case may be. Does anyone notice anything else going on that could cause result.data
to consistently be null?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|