Back to results for “GraphQL”
Flutter › GraphQL in Flutter

What is the graphql_flutter package, and how does it let you actually execute a GraphQL query directly from within your Flutter app?

Beginner

Answer

The graphql_flutter package provides a genuinely convenient set of dedicated widgets and classes specifically for integrating GraphQL into a Flutter app, including a Query widget that lets you declaratively execute a given specific GraphQL query and automatically receive back its resulting loading, error, and final data states, similar conceptually to how a FutureBuilder already works, letting you focus purely on properly defining exactly what specific data you genuinely need rather than manually managing the entire underlying network request process completely yourself.
Query(
  options: QueryOptions(document: gql(productQuery)),
  builder: (result, {fetchMore, refetch}) {
    if (result.isLoading) return CircularProgressIndicator();
    return Text(result.data!['product']['name']);
  },
)
Real-world example A product listing screen uses the Query widget from graphql_flutter to declaratively execute its own required GraphQL query, automatically and conveniently receiving proper loading and error state handling without needing to manually write any of that repetitive common boilerplate handling logic themselves.

Common follow-ups: What is the difference between the Query widget and manually calling a GraphQL client directly yourself?;How do you properly set up the required GraphQLClient specifically needed to actually connect to your particular backend?