In this article, we are going to learn how to use GraphQL flutter package to execute queries on the server. In this flutter example, we will create queries to fetch countries data from API.
GraphQL is query language that allows us to send query to GraphQL server. Then server gives filtered data to us from existing data according to our queries.
For example one api contains user data such as id, first name, last name, address etc. But we want only first name of the user not the whole user info in the data response then this will be possible with GraphQL API.
Using GraphQL API, we can retrieve data according to our requirement. We do not need to ask backend developer to contain only particular data. First we need to create GraphQLClient to connect to GraphQL server.
Add required dependency in your pubspec.yaml file
Connect GraphQL Flutter Server
First we need to initialize GraphQLClient with link and cache to use it. Here we are using HttpLink as our link and Optimisticache as our cache. HttpLink is used to retrieve endpoint of GraphQL server. ValueNotifier is used to notify GraphQLClient if any value is changed.
GraphQLProvider is used to connect GraphQL server without any endpoint of GraphQL server. We just need to wrap all widgets under the GraphQLProvider and add our GraphQLClient to client property.