Introduction to GraphQL: Concepts, Usage, and a Python Flask Implementation
This article explains the limitations of traditional RESTful APIs, introduces GraphQL as a flexible query language, details its core concepts such as operation types, object and scalar types, and schema definition, and provides a complete Python Flask example with code analysis, execution steps, and a discussion of its advantages and drawbacks.
Traditional RESTful APIs often require multiple sequential requests with dependencies, making it difficult to parallelize calls and leading to slow development cycles. To address this, GraphQL was introduced by Facebook in 2016 as a query language that lets clients declaratively specify exactly what data they need, while servers expose their capabilities through a contract.
GraphQL defines three operation types: query for data retrieval, mutation for data modification, and subscription for real‑time updates. It distinguishes between object types (user‑defined types in the schema) and scalar types (built‑in primitives such as String, Int, Float, Boolean, ID). The schema, written in the Schema Definition Language (SDL), describes the structure of data and the rules for requests, providing clear error messages when queries are invalid.
The article then presents a hands‑on example that builds a GraphQL API using Python, Flask, and the graphene library. The example includes:
pip install graphene
pip install Flask-GraphQLSchema definition ( books.graphql):
type Book{
id: ID!
name: String!
}
type Query{
books: [Book!]!
}
type Mutation{
add(name: String!): Book!
}Flask server implementation:
from flask import Flask
from flask_graphql import GraphQLView
import graphene
app = Flask(__name__)
books = []
class Book(graphene.ObjectType):
id = graphene.ID(description="book ID")
name = graphene.String(description="book name")
create = lambda id, name: Book(id=id, name=name)
books.append(create(1, "The First Book"))
class Query(graphene.ObjectType):
books = graphene.List(Book, description="list books")
version = graphene.String(description="version")
def resolve_books(self, info):
return books
def resolve_version(self, info):
return "v0.1"
class AddBook(graphene.Mutation):
class Arguments:
name = graphene.String()
Output = Book
def mutate(self, info, name):
book = Book.create(len(books) + 1, name)
books.append(book)
return book
class Mutation(graphene.ObjectType):
add = AddBook.Field()
schema = graphene.Schema(query=Query, mutation=Mutation)
app.add_url_rule('/graphql', view_func=GraphQLView.as_view('graphql', schema=schema, graphiql=True))
app.run(port=4901, debug=True)The code analysis explains that books acts as an in‑memory data store, the Query object returns both the list of books and a version string (demonstrating how a single GraphQL request can replace multiple REST calls), and the Mutation class groups all mutation operations.
To run the service, execute python server.py and query it with a tool like curl:
http://127.0.0.1:4901/graphql?query=query%20{books%20{id%20name}%20version}The article concludes with a balanced discussion of GraphQL's strengths—precise data fetching, reduced over‑fetching, and a self‑describing schema—and its weaknesses, such as the need for tight backend cooperation, limited suitability for simple CRUD scenarios, and the extra complexity of handling graph‑structured data on the client side.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
360 Tech Engineering
Official tech channel of 360, building the most professional technology aggregation platform for the brand.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
