Skip to content

jpcadena/fastapi-graphql

Repository files navigation

fastapi-graphql


Logo

Fastapi GraphQL project

Fastapi backend using GraphQL API
Explore the docs »

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Contributing
  5. Security
  6. Code of Conduct
  7. License
  8. Contact

About The Project

Project

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.

(back to top)

Built with

This project is built using a suite of modern, powerful technologies and frameworks to ensure high performance, ease of development, and robustness:

Python FastAPI Graphene GraphQL Playground Pydantic Starlette Uvicorn postgresql Redis JWT HTML5 CSS3 isort Black Ruff MyPy pre-commit GitHub Actions Poetry Pycharm Visual Studio Code Markdown Swagger UI License: MIT

Components

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.

(back to top)

Getting started

Prerequisites

Installation

  1. Clone the repository

    git clone https://github.com/jpcadena/fastapi-graphql.git
  2. Change the directory to root project

    cd fastapi-graphql
  3. Install Poetry package manager

    pip install poetry
  4. Install the project's dependencies

    poetry install
  5. Activate the environment

    poetry shell

(back to top)

Usage

  1. 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.

  2. 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.

  3. 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 and private_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.

  4. 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.

  5. 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.

  6. 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.

(back to top)

Contributing

GitHub

Please read our contributing guide for details on our code of conduct, and the process for submitting pull requests to us.

(back to top)

Security

For security considerations and best practices, please refer to our Security Guide for a detailed guide.

(back to top)

Code of Conduct

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.

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

  • LinkedIn

  • Outlook

(back to top)