OPAL Fetcher using GraphQL and Neon
Tags: python, postgressql, quine, graphql
Published: 2024-06-14 12:51:21

- As an enthusiastic student passionate about leveraging cutting-edge technologies, I participated in the Medium Track of the OPAL challenge. My submission focuses on developing a custom GraphQL data fetcher for the **OPAL policy engine**, seamlessly integrated with a Neon _serverless Postgres_ database. This project not only showcases my technical skills but also contributes to the OPAL ecosystem by enhancing its data-fetching capabilities.

## Project Overview
- In this project, I created a custom data fetcher that allows OPAL to fetch data from a GraphQL API in real-time and synchronize it with the policy engine. The data fetcher is designed to be highly configurable and uses environment variables to manage connections securely. This approach simplifies the process of integrating various data sources into OPAL, facilitating dynamic policy updates based on external data.

## Key Features

1. `GraphQL Integration`:

- The fetcher connects to a GraphQL API to retrieve data needed for policy decisions. For demonstration, I used the GitHub GraphQL API, allowing the fetcher to query user information and repository details.
- The GraphQL query and endpoint are defined through environment variables, making the fetcher flexible and adaptable to different GraphQL APIs.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7metrivs32opsingbssj.png)
2. `Neon Integration`: 
- The project utilizes Neon, a serverless Postgres database, to store and manage fetched data. This choice aligns with modern cloud-native practices, ensuring scalability and reliability without the overhead of managing database infrastructure.
- The connection to the Neon database is also managed via environment variables, ensuring secure and straightforward configuration.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s96es1mmunkik2xhagmp.png)
3. `Dockerized Deployment`:
- The entire application is containerized using Docker, ensuring consistency across different development and production environments.
- A Docker Compose setup is provided to simplify the deployment process, allowing users to get the fetcher up and running quickly.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f1l04nwt27669rzix7pb.png)

## Project Structure

```
opal-fetcher-graphql/
├── src/
│   ├── opal_fetcher_graphql/
│   │   ├── __init__.py
│   │   ├── provider.py
│   ├── Dockerfile
├── README.md
├── docker-compose.yml
└── requirements.txt

```
The project organized as follows:
- `provider.py`: Contains the core logic for the GraphQL fetcher, including methods for fetching and processing data from the GraphQL API.
- `Dockerfile` : Defines the Docker image setup, ensuring the fetcher and its dependencies are correctly installed.
- `docker-compose.yml` : Facilitates easy deployment, setting up the necessary environment variables and services.

- For **code and how to contribute** checkout the github repo: [Code Link](https://github.com/nithamitabh/opal_fetcher_graphql).

## References
- I used this docs on OPAL [opal docs](https://opal.ac/tutorials/write_your_own_fetch_provider/) .
- I also understand how to integrate to postgress using : [repo link](https://github.com/permitio/opal-fetcher-postgres?tab=readme-ov-file)
- I also some YouTube stuffs(which i can't remember) and my presonal experience with Neon and GraphQL as i already used in previous project.
- `Note` I am happy to hear your suggestion/feedback here and wait for useful contribution on GitHub.

## Contribution
- You can my `Readme.md` file where i explain how to setup my project locally and test it.