Skip to content

About

Civic-initiated open API for Thai political data

Resources

Stars

8 stars

Watchers

2 watching

Forks

Repository files navigation

Politigraph

Politigraph

A civic-initiated open API for Thai political data

To learn more about the data and API, visit the documentations

Warning

We are not responsible for any misinformation or consequence of any usage on our data. These open data are collected, transformed, validated, and published by us, a civil society, to push the open API standard in Thailand. We are not the official government institution who originally own and responsible in publishing these data. Please send us any feedback or suggestion via team@wevis.info or create an issue on GitHub.

Contents

The Monorepo

Managed with Turborepo

  • /apps : Application repository
    • /admin : Admin dashboard for managing Politigraph data to be built as SPA
    • /api : Backend API for GraphQL, authentication, and static file (web/assets) serving
    • /docs : Landing page and documentations to be built as static website
  • /packages : Shared packages between app
    • /auth : Authentication and related database management
    • /config : Centralized environment variable validation
    • /graphql : Schemas and custom resolvers implementation

Routes and Deployment

Main routes of the final application:

  • / A landing page and documentation for public
  • /admin Admin panel for managing politigraph data (required an account)
  • /graphql GraphQL endpoint and playground for querying and updating data.
  • /mcp MCP server for AI agents
  • /observe OpenObserve dashboard for usage analytics and logs (required an account)

https://politigraph.wevis.info will be deployed through GitHub Actions every time the repository code has updated.

Set Up Local Development

Following these steps will help you set up Politigraph on your local development environment.

Environment Variables

Add required environment variables to the root .env. See .env.example at the project root for the full list. Make sure POSTGRES_PASSWORD matches the password embedded in DATABASE_URL.

@politigraph/config loads env files in order:

  1. .env — baseline
  2. .env.{NODE_ENV} — overrides (e.g. .env.production)

All files are optional. NODE_ENV defaults to development.

Start Neo4j and Postgres Containers

Using Docker Compose

docker compose up -d

Neo4j API will be running at http://localhost:7687 with web UI at http://localhost:7474 and the data is stored in .neo4j folder in your project root.

Postgres will be running at http://localhost:5342 and the data will ve stored in docker volume.

Start apps' development server

Requires Bun

bun i	# Install dependencies for the first time
bun run dev

Dependency packages will be built and every apps will run in dev mode in parallel. Following the console to see which app running on which port.

Create user account (for admin access)

You need to create an account first to login to the admin panel, by making a POST request to /auth/sign-up/email of API URL with header Origin: http://localhost:3000 and the following body:

{
	"name": "*****",
	"email": "*****",
	"password": "*****"
}

Testing

The project has three test levels:

  • Unit tests — test individual functions and composables
  • Integration tests — test API routes and GraphQL resolvers with real databases
  • E2E tests — test full user flows in the admin panel using Playwright

Running tests

All tests require test databases. Start them with:

docker compose -f compose.test.yml up -d --wait

Then run tests:

bun run test:unit        # Unit tests only
bun run test:integration # Integration tests (requires test databases)
bun run test:e2e         # E2E tests (requires test databases + seed)

Test infrastructure

compose.test.yml provides isolated test databases:

  • Neo4j on port 7688 (avoids conflict with dev Neo4j on 7687)
  • Postgres on port 5433 (avoids conflict with dev Postgres on 5432)

References

GraphQL API authentication

GraphQL required user account for write permission. To make an update to GraphQL outside the Politigraph website, you need to create an API key by logging in into the Admin Panel (/admin) > Profile Menu > API Keys. Then add x-api-key: KEY_VALUE to the header when making a request.

Dump production database to local

To make local neo4j have up-to-dated data, we have a script to use rsync to download production database while local neo4j docker service in not running:

bun run db:pull

Maintenance mode for database migration

To stop the API and MCP from querying or mutating data during a migration, stop the API container on the app server:

docker compose stop elysia
# run the migration
docker compose up -d elysia

While the container is down, nginx answers every API route (/graphql, /mcp, /auth, etc.) with 503 and a GraphQL-style error body. The docs and admin static pages are still served. Don't push to main during the migration, as the deployment will start the container again.

Usage analytics and logs

The app server ships container logs to a self-hosted OpenObserve at /observe:

elysia, nginx (stdout) → Vector (servers/app/vector) → OpenObserve (servers/app/compose.yml)

Vector routes lines into three streams:

  • api_usage: one event per GraphQL operation (fields and deprecated as Type.field, source, client, duration, errors) and per MCP connect, tool call, and resource read, written by apps/api/src/plugins/usage.ts. Query text, variables, and responses are never logged
  • nginx: JSON access log, without static asset hits under 400
  • elysia: everything else the API prints, e.g. startup, crashes, and errors

client is the Origin host for browsers, or the apollographql-client-name header for server-side consumers, otherwise direct. The same events, without field details, are also sent to Plausible. To comply with PDPA, no raw IP or email is stored

Operation notes:

  • OPENOBSERVE_USER and OPENOBSERVE_PASSWORD must be set on the app server, compose refuses to start without them
  • Field names are lowercased on ingestion, and array fields are stored as strings, so unnest them with unnest(cast_to_arr(fields)) in SQL

Debugging Neo4j heap

The heap is a fixed block reserved at startup, so free and docker stats look the same whether it is empty or full. When Neo4j pins the CPU without a traffic spike, find the host PID of the java process and check its threads on the DB server:

docker top neo4j
top -H -p <host_java_pid>

Busy GC Thread threads mean the heap is full. Find the Java PID inside the container (the last number of NSpid), then inspect the heap as the neo4j user (uid 7474):

grep NSpid /proc/<host_java_pid>/status
docker exec -u 7474 neo4j jcmd <pid> GC.heap_info
docker exec -u 7474 neo4j jcmd <pid> GC.class_histogram | head -30

Classes from org.neo4j.cypher.internal (InputPosition, Prettifier, AST nodes) at the top mean the query plan cache filled the heap. CALL db.clearQueryCaches() frees it without a restart, and server.memory.query_cache.per_db_cache_num_entries in servers/db/compose.yml caps it.

License

Regarding the data, the team intends to open it as Open Data under the Attribution-NonCommercial 4.0 International terms. This means you can use, modify, and build upon the data, but it cannot be used for commercial purposes or to seek profit from the work, and credit must be given to WeVis.

Regarding the source code, the team intends to develop every project as Open Source under the Attribution-NonCommercial-ShareAlike 4.0 International terms. This means you can use, modify, and build upon the work, but it cannot be used for commercial purposes or to seek profit from the work. You must provide notice and credit to the work's owner, and the resulting work must be under the same Creative Commons license conditions as the original. WeVis Ltd. and Punch Up Ltd. are joint licensors.

About

Civic-initiated open API for Thai political data

Resources

Stars

8 stars

Watchers

2 watching

Forks

Used by

Contributors

Languages