Table of Contents
This backend project is a FastAPI-based template designed to serve as a robust, reliable, and GraphQL API backend. It is crucial for facilitating user authentication, real-time data processing, and deployment as serverless functions. By leveraging GraphQL, this API ensures flexible and efficient data retrieval, addressing the limitations of REST APIs.
This project covers topics from basic GraphQL concepts to advanced features like authentication, ORM integration, and deployment.
This project is built using a suite of modern, powerful technologies and frameworks to ensure high performance, ease of development, and robustness:
The system is composed of the following main components, each playing a crucial role in providing a scalable, maintainable, and robust application:
- FastAPI Backend: Serves as the core of the GraphQL API, handling incoming HTTP requests, processing data, and sending responses. It's designed for high performance and encourages standard API development practices.
- Graphene: A Python library for building GraphQL APIs, providing a simple and powerful way to define schemas and handle queries and mutations.
- SQLAlchemy ORM: Used for database interactions, providing a high-level and pythonic way to interact with relational databases.
- JWT Authentication: Implements secure authentication mechanisms using JSON Web Tokens (JWT), ensuring secure access to the API.
This project adheres to GraphQL design principles, ensuring an intuitive and standard approach to API development. This design facilitates easy integration, scalability, and interaction with various clients, including web and mobile applications. The API provides flexible query and mutation capabilities, uses JWT for secure, stateless authentication, and can be deployed as serverless functions for scalability and cost-efficiency.
-
Clone the repository
git clone https://github.com/jpcadena/fastapi-graphql.git
-
Change the directory to root project
cd fastapi-graphql
-
Install Poetry package manager
pip install poetry
-
Install the project's dependencies
poetry install
-
Activate the environment
poetry shell
-
Setting up environment variables:
If you find a
.env.sample
in the project directory, make a copy of it and rename to.env
.cp .env.sample .env
This
.env
file will be used to manage your application's environment variables. -
Configuring your credentials:
Open the
.env
file in a text editor and replace the placeholder values with your actual credentials.# .env file POSTGRES_USER=your_database_user SECRET_KEY=your_api_key
Be sure to save the file after making these changes.
-
Generating RSA keys
To ensure secure communication in this project, RSA keys are used. Before running the application, you need to generate a public and private RSA key pair. We've provided a Python script to automatically generate these keys. You can find the script at
app\services\infrastructure\encryption.py
. To generate your keys, simply run:python app\services\infrastructure\encryption.py
This will create
public_key.pem
andprivate_key.pem
files in your specified directory.Once the keys are generated, the application will use them for cryptographic operations. Ensure that these files are kept secure and are not exposed publicly. The default configuration expects these keys in the root directory of the project.
-
Starting the server:
To start the local server on your machine, run the following command in your terminal:
uvicorn main:app --reload
The
--reload
flag enables hot reloading, which means the server will automatically update whenever you make changes to the code. -
Interacting with the app:
Once your server is running, you can interact with it using any API client like Postman or your web browser. You can send GraphQL queries and mutations to the API endpoints as defined in your main.py file. For example, to get all users, you can send a query to http://localhost:8000/graphql.
-
Using GraphQL Playground:
FastAPI provides automatic interactive API documentation using GraphQL Playground. Once your server is up and running, you can go to http://localhost:8000/graphql in your web browser to access it. From there, you can explore and interact with your API directly.
Please read our contributing guide for details on our code of conduct, and the process for submitting pull requests to us.
For security considerations and best practices, please refer to our Security Guide for a detailed guide.
We enforce a code of conduct for all maintainers and contributors. Please read our Code of Conduct to understand the expectations before making any contributions.
Distributed under the MIT License. See LICENSE for more information.