This project demonstrates how to build a GraphQL API using Graphene, SQLAlchemy, Flask, and Promise. It includes a simple data model with users and posts, and provides GraphQL queries to fetch and manipulate the data. The Promise library is used to implement the DataLoader pattern for efficient loading of data.
To install the project, you need to have Python and Poetry installed. Then, you can clone the repository and install the dependencies:
git clone https://github.com/EM51641/dataloader-graphql.git
cd data-loader-graphql
poetry install
Before you can run the project, you need to set up your environment variables.
export SQLALCHEMY_DATABASE_URI=sqlite:///data.db
This command creates a new file named .env in the root of the project and adds a line to it that sets the SQLALCHEMY_DATABASE_URI
environment variable. This variable tells SQLAlchemy where your database is located. In this example, it's set to use a SQLite database named data.db in the root of the project. You can replace sqlite:///data.db
with the URI for your own database.
To run the project, use the following command:
poetry run python main.py
This will start the Flask server on http://localhost:5000
Here are some example GraphQL queries that you can run on this API.
mutation {
CreateUser(username: "newuser", email: "newuser@example.com") {
user {
id
username
email
}
}
}
mutation {
CreatePost(title: "New Post", content: "This is a new post.", user_id: 1) {
post {
id
title
content
}
}
}
query {
users {
id
email
username
posts(first: 5) {
edges {
node {
id
content
title
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
}