Building APIs with GraphQL and Flask

Page 1

Building APIs with GraphQL and Flask

• Assumptions

o I’m assuming that y’all have some basic knowledge about Graphql & Flask.

o If not then kindly go through below link:

o Graphql: https://graphql.org/learn/

o Flask: https://flask.palletsprojects.com/

• Introduction

o GraphQL is a query language for APIs and a server-side runtime that allows clients to request only the data that they need from APIs.

o GraphQL is meant to be a more efficient and flexible alternative to REST.

• Initial steps

o Create virtual environment

o Install requirements

▫ Flask

▫ Flask-sqlalchemy

▫ Flask_login

▫ Flask_migration

▫ Psycopg2-binary

o

o We are going to use the ariadne library to integrate graphql in flask for that install ariadne library using this command.

▪ pip install ariadne

o Configure flask app

o Create models for your project and migrate

o Create schema.graphql file which contain types of models field in your root directory

➢ The Query and Mutation type

• Every graphql service has a query and mutation type. These types are the same as regular types, but they are special because they define the entry point of every graphql query.

• Put this code in schema.graphql

• Types and fields

o The most basic components of a GraphQL schema are object types, which just represent a kind of object you can fetch from your service, and what fields it has. In the GraphQL schema language, for example

• Example

o OrderAll is a graphql object type, meaning it’s a type with some fields.

o success, errors and order are fields on the OrderAll type. That means success, errors and order are the only fields that can appear in graphql query that operates on the OrderAll type.

o String is one of the built-in scalar types; these are types that resolve to a single scalar object, and can’t have sub-selection in query.

o String! means that the field is non-nullable, meaning that the Graphql service always gives you value when you query this field. We’ll represent those with an exclamation mark.

o [Order]! Represent an array of Order objects. you can get every item of the array when you query the order field. it can have sub-selections in the query.

• Create function for graphql execution

o Create resource.py file your app for implement graphql resolver(functions)

o Configure QueryType and MutationType from ariadne library in project __init__ file

o Import query and mutation in resource.py file it is used as wrapper in function which is map function to graphql query.

o Create graphql query for get_all_user

o First create model for user

o Create user model in model.py file

o Add fields like username, email, mobile_number, password

o Create to_dict function for return all data in dictionary it is helping for fetch particular data

• Create get all user resolver

o Create function which return all users data in json

o “*_” in users function is use for ignore unwanted list of parameter

o In user_data we get list of all user data in json from database

o This function returns success and users, where success contains Boolean value and users contain a list of user data.

• Conclusion

o Building APIs with GraphQL and Flask is a powerful combination that can greatly simplify API development. By using GraphQL as a query language and Flask as a web framework, developers can build flexible, scalable APIs that are easy to maintain and evolve over time. Through this documentation, you will learn how to integrate GraphQL with Flask and leverage the strengths of both technologies to build robust APIs that meet your specific requirements. By following the best practices and guidelines outlined in this documentation, you can ensure that your APIs are efficient, secure, and provide a great user experience.

Originally published by: Building APIs with GraphQL and Flask

Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.
Building APIs with GraphQL and Flask by INEXTURE Solutions - Issuu