Using GraphQL with Flutter: A Tutorial with GitHub GraphQl API

Graphql.com imageGraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. It gives clients the power to ask for exactly what they need and nothing more. While typical REST APIs require loading from multip…


This content originally appeared on Level Up Coding - Medium and was authored by Shaik Ahron

Graphql.com image

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. It gives clients the power to ask for exactly what they need and nothing more. While typical REST APIs require loading from multiple URLs, GraphQL APIs get all the data your app needs in a single request. Apps using GraphQL can be quick even on slow mobile network connections. GraphQL APIs are organized in terms of types and fields, not endpoints. Access the full capabilities of your data from a single endpoint.

Some Fundamentals of GraphQL :

GraphQL is a query language for your API and a server-side runtime for executing queries using a type system you define for your data.

Fields: GraphQL is about asking for specific fields on objects. Let’s start by looking at a very simple query and the result we get when we run it.

query: it is one of the operation types which you can use to get some data.

mutation: it is one of the operation types by which you can create/modify server-side data.

Input types: while passing arguments you can pass complex objects as well.

subscriptions: it is one of the operation types supported by GraphQL which is like a query operation but it can have a long-lasting connection. it is like subscribing to that data. you need to create a WebSocket connection first.

To learn more about GraphQL check out here.

Let’s Build a GraphQL-based app:

In this example, we are going to use GitHub’s GraphQL API. it is free to use and it is a good start to learn GraphQL. GitHub allows two types of operations query and mutation.

Check out my GitHub repo here.

What you are gonna build

Step 1: Create a new flutter project. After creating add these dependencies in pubspec.yaml

hive_flutter: ^1.1.0
graphql_flutter: ^5.1.0

Step 2: You need to create a GraphQLClient instance.

You need to concat the GraphQL endpoint and authentication header with GitHub personal token.

You can create a token like this and replace {YOUR_GITHUB_TOKEN}

Step 3: Now I will introduce you to 3 operations that we are gonna use.

query GithubGraphQL(\$login: String!) {
user(login: \$login) {
avatarUrl(size: 200)
name
email
login
repositories(first: 10) {
pageInfo {
endCursor
startCursor
}
totalCount
edges {
node {
id
name
viewerHasStarred
}
}
}
followers {
totalCount
}
following {
totalCount
}
}
}

so, we will be making a GitHub username search on the GitHub GraphQL server.

so you can see we are using query operation and passing arguments.

so we will fetch the total repository count, total followings count, total followers, and repositories list.

Now, the next two are mutation operations.

mutation AddStar(\$starrableId: ID!) {
addStar(input: {starrableId: \$starrableId}) {
starrable {
viewerHasStarred
}
}
}

So, you can star any repo of that user by this operation.

mutation RemoveStar(\$starrableId: ID!) {
removeStar(input: {starrableId: \$starrableId}) {
starrable {
viewerHasStarred
}
}
}

you can remove the star from that repo if you’ve already starred it.

Step 4: Now, create a Query widget and add QueryOption passing the query string and passing the variables map.

in builder callback, you will receive result and fetchmore to fetch more data as we get data in a paginated manner.

Step 5: We will create ProfileWidget now.

Step 6: Now, we will create RepositoryTileView.

Now, to star repo or remove the star from a repo. you have to create a Mutation widget.

so, based on whether the repo is starred or not we will change the mutation string which I mentioned earlier, and in the builder call the runMutation method when you want to star or remove a star repo.

Step 7: Now, when you reach the end of the list you have to get more data.

so using FetchMoreOptions you can merge the new result with the previous result data by passing endCursor and passing this in the method fetchMore callback.

Step 8: So, the last step is to create a search bar to input the username that you want to search.

That’s it.

Check out my code.

Connect with me here.

Thank You for Reading


Using GraphQL with Flutter: A Tutorial with GitHub GraphQl API was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Shaik Ahron


Print Share Comment Cite Upload Translate Updates
APA

Shaik Ahron | Sciencx (2023-01-02T01:28:22+00:00) Using GraphQL with Flutter: A Tutorial with GitHub GraphQl API. Retrieved from https://www.scien.cx/2023/01/02/using-graphql-with-flutter-a-tutorial-with-github-graphql-api/

MLA
" » Using GraphQL with Flutter: A Tutorial with GitHub GraphQl API." Shaik Ahron | Sciencx - Monday January 2, 2023, https://www.scien.cx/2023/01/02/using-graphql-with-flutter-a-tutorial-with-github-graphql-api/
HARVARD
Shaik Ahron | Sciencx Monday January 2, 2023 » Using GraphQL with Flutter: A Tutorial with GitHub GraphQl API., viewed ,<https://www.scien.cx/2023/01/02/using-graphql-with-flutter-a-tutorial-with-github-graphql-api/>
VANCOUVER
Shaik Ahron | Sciencx - » Using GraphQL with Flutter: A Tutorial with GitHub GraphQl API. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/01/02/using-graphql-with-flutter-a-tutorial-with-github-graphql-api/
CHICAGO
" » Using GraphQL with Flutter: A Tutorial with GitHub GraphQl API." Shaik Ahron | Sciencx - Accessed . https://www.scien.cx/2023/01/02/using-graphql-with-flutter-a-tutorial-with-github-graphql-api/
IEEE
" » Using GraphQL with Flutter: A Tutorial with GitHub GraphQl API." Shaik Ahron | Sciencx [Online]. Available: https://www.scien.cx/2023/01/02/using-graphql-with-flutter-a-tutorial-with-github-graphql-api/. [Accessed: ]
rf:citation
» Using GraphQL with Flutter: A Tutorial with GitHub GraphQl API | Shaik Ahron | Sciencx | https://www.scien.cx/2023/01/02/using-graphql-with-flutter-a-tutorial-with-github-graphql-api/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.