Skip to content

Commit

Permalink
feat(graph): adds Dockerfile and docker-compose.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
alber70g committed Nov 21, 2024
1 parent dd5d2c4 commit 44aad8c
Show file tree
Hide file tree
Showing 11 changed files with 452 additions and 261 deletions.
8 changes: 8 additions & 0 deletions .changeset/witty-foxes-buy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@kadena/graph': minor
---

Adds improvements to run as stand-alone as well as in Docker
- adds postinstall script to install the platform specific Prisma engines
- adds a Dockerfile to build from NPM deployed @kadena/graph package
- adds a Dockerfile to build from source
112 changes: 112 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
.git

##################################################
### Below is the same as .gitignore
##################################################

# build artefacts
packages/*/*/lib
packages/*/*/lib-commonjs
packages/*/*/.parcel-cache
packages/*/*/dist
packages/*/*/coverage

# lock-files that don't belong here
package-lock.json
yarn.lock
npm-shrinkwrap.json
shrinkwrap.json

# Metadata
tsdoc-metadata.json

# Logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables files
.env
# direnv
.envrc

# next.js build output
.next

# OS X temporary files
.DS_Store

packages/*/*/temp
# Heft
.heft

# Pact command history
.pact-history

# Intelij idea files
.idea
.idea/*

# Turborepo
.turbo

# Storybook static build
storybook-static

temp

#Playwright
/packages/*/*/test-results
/packages/*/*/playwright-report
/packages/*/*/blob-report
# cache-sh
.cache-sh

# parcel
.parcel-cache
20 changes: 20 additions & 0 deletions packages/apps/graph/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# This dockerfile is using @kadena/graph from NPM
FROM node:20-bookworm-slim AS build

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable

WORKDIR /app

RUN apt-get update
RUN apt-get install -y openssl

RUN pnpm i -g @prisma/engines prisma
RUN pnpm i -g @kadena/graph

# empty to satisfy checks
RUN mkdir /app/cwd-extra-migrations

EXPOSE 4000
CMD ["kadena-graph"]
74 changes: 48 additions & 26 deletions packages/apps/graph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,33 @@
</picture>
<!-- markdownlint-enable MD033 -->

- [Getting started](#getting-started)
- [Method 1: Using the published npm package](#method-1-using-the-published-npm-package)
- [Method 2: Building and running from source](#method-2-building-and-running-from-source)
- [Running your first query](#running-your-first-query)
- [Example queries per use case](#example-queries-per-use-case)
- [Wallet related](#wallet-related)
- [Explorer related](#explorer-related)
- [Event related](#event-related)
- [Fungible related](#fungible-related)
- [Non-fungible related](#non-fungible-related)
- [Features](#features)
- [Tracing and trace analysis](#tracing-and-trace-analysis)
- [Query Complexity](#query-complexity)
- [Prisma JSON field queries](#prisma-json-field-queries)
- [Paginated results](#paginated-results)
- [Useful extra's](#useful-extras)
- [Network Information](#network-information)
- [Running devnet](#running-devnet)
- [GraphQL](#graphql)
- [Connecting to the database](#connecting-to-the-database)
- [Simulating traffic on the devnet](#simulating-traffic-on-the-devnet)
- [Coin simulation](#coin-simulation)
- [Marmalade simulation](#marmalade-simulation)
- [Flood devnet](#flood-devnet)
- [Changelog](#changelog)
- [@kadena/graph](#kadenagraph)
- [Getting started](#getting-started)
- [Method 1: Using the published npm package](#method-1-using-the-published-npm-package)
- [Method 2: Building and running from source](#method-2-building-and-running-from-source)
- [Method 3: Using docker-compose](#method-3-using-docker-compose)
- [Running your first query](#running-your-first-query)
- [Example queries per use case](#example-queries-per-use-case)
- [Wallet related](#wallet-related)
- [Explorer related](#explorer-related)
- [Event related](#event-related)
- [Fungible related](#fungible-related)
- [Non-fungible related](#non-fungible-related)
- [Features](#features)
- [Tracing and trace analysis](#tracing-and-trace-analysis)
- [Query Complexity](#query-complexity)
- [Prisma JSON field queries](#prisma-json-field-queries)
- [Paginated results](#paginated-results)
- [Useful extra's](#useful-extras)
- [Network Information](#network-information)
- [Running devnet](#running-devnet)
- [GraphQL](#graphql)
- [Connecting to the database](#connecting-to-the-database)
- [Simulating traffic on the devnet](#simulating-traffic-on-the-devnet)
- [Coin simulation](#coin-simulation)
- [Marmalade simulation](#marmalade-simulation)
- [Flood devnet](#flood-devnet)
- [Changelog](#changelog)

## Getting started

Expand All @@ -44,7 +46,7 @@ If you are not familiar yet with GraphQL, we recommend to first read the [offici

This GraphQL server creates a readonly GraphQL endpoint that retrieves data from a Chainweb node and a [chainweb-data](https://github.com/kadena-io/chainweb-data) PostgreSQL database. The Chainweb node is used to execute pact queries to, for uses such as retrieving account balances. The PostgreSQL database is used to read data such as blocks, transactions, and events. By default, the GraphQL server points to a local devnet instance.

Prequisites:
Prerequisites:

- [Node.js](https://nodejs.org/en/download/)
- A running Chainweb node and chainweb-data PostgreSQL database with the migrations in `cwd-extra-migrations` applied. If you don't have this set up, see [Running devnet](#running-devnet).
Expand Down Expand Up @@ -83,6 +85,26 @@ Then, run the project:
pnpm run start
```

### Method 3: Using docker-compose

Build and run the project from docker-compose.

Modify the [`.env`](./.env.example) accordingly, and then run the following commands:

This will use the **released package from NPM** @kadena/graph.

```sh
docker compose build graphql
docker compose up graphql -p 4000:4000
```

If you want to use the **build from source**, you can use the following command:

```sh
docker compose build graphql-source
docker compose up graphql-source -p 4000:4000
```

### Running your first query

On startup, the GraphQL server runs a set of system checks to see if Chainweb node and the chainweb-data database are reachable. It also checks if all the extra migrations have been executed. If the checks fail, the server will not start. If the checks pass, the server will start and you can access the GraphQL endpoint at `localhost:4000/graphql`.
Expand Down
23 changes: 23 additions & 0 deletions packages/apps/graph/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
services:
graphql-source:
image: kadena/devnet
ports:
- "4000:4000"
env_file: .env
environment:
- NODE_ENV=production
build:
context: ../../../
dockerfile: ./packages/apps/graph/source.Dockerfile

# build with `docker compose build graphql`
# run with `docker compose up graphql`
graphql:
image: kadena/devnet
ports:
- "4000:4000"
env_file: .env
environment:
- NODE_ENV=production
build:
dockerfile: Dockerfile
12 changes: 11 additions & 1 deletion packages/apps/graph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@
"bin": {
"kadena-graph": "./dist/index.js"
},
"files": [
"dist",
"prisma"
],
"prisma.schema": "./prisma/schema.prisma",
"scripts": {
"build": "pnpm run pactjs:generate:contracts && pnpm prisma:generate && tsc",
"build:bun": "rm -rf build && mkdir -p build && bun build src/index.ts --sourcemap --target bun --outdir build",
"build:docker": "cache-sh -i \"{src/**/*,build/*,package.json,Dockerfile}\" \"pnpm run build:bun && docker build -t kadena/graph .\"",
"deploy:marmalade": "ts-node -T src/devnet/deployment/index.ts deploy:marmalade",
"devnet": "docker volume create kadena_devnet; docker run --rm -it -p 8080:8080 -p 5432:5432 -p 9999:9999 -p 1789:1789 -v kadena_devnet:/data -v ./cwd-extra-migrations:/cwd-extra-migrations:ro --name devnet kadena/devnet",
"devnet:update": "docker pull kadena/devnet",
Expand Down Expand Up @@ -45,7 +52,8 @@
"start:generate": "pnpm build && npx ts-node-dev --respawn --no-notify --exit-child src/index.ts",
"test": "echo \"no test specified\"",
"testing": "ts-node -T src/devnet/simulation/index.ts test",
"tracing:analyse": "npx ts-node -T src/services/tracing/trace-analyser.ts"
"tracing:analyse": "npx ts-node -T src/services/tracing/trace-analyser.ts",
"postinstall": "prisma generate"
},
"dependencies": {
"@kadena/chainweb-node-client": "workspace:*",
Expand Down Expand Up @@ -96,7 +104,9 @@
"@types/pg": "~8.11.5",
"@types/seedrandom": "~3.0.8",
"@types/ws": "^8.5.10",
"cache-sh": "^1.2.1",
"prettier": "~3.2.5",
"rimraf": "~5.0.1",
"ts-node": "~10.9.2",
"typescript": "5.4.5"
},
Expand Down
4 changes: 2 additions & 2 deletions packages/apps/graph/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
generator client {
provider = "prisma-client-js"
previewFeatures = ["views"]
previewFeatures = ["views", "relationJoins"]
}

generator pothos {
provider = "prisma-pothos-types"
provider = "prisma-pothos-types"
}

datasource db {
Expand Down
24 changes: 24 additions & 0 deletions packages/apps/graph/source.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM node:20-bookworm-slim AS build

ENV PNPM_HOME="/pnpm"
ENV PATH="$PNPM_HOME:$PATH"
RUN corepack enable
RUN pnpm i -g turbo

WORKDIR /app
COPY ./../../../ .

RUN pnpm install --filter @kadena/graph...
RUN turbo run build --filter @kadena/graph^...
RUN turbo run build --filter @kadena/graph

RUN apt-get update
RUN apt-get install -y openssl
RUN apt-get install -y ssh
RUN apt-get install -y byobu
RUN apt-get install -y postgresql-client

RUN cd packages/apps/graph && pnpm run prisma:generate

EXPOSE 4000
CMD ["node", "./packages/apps/graph/dist/index.js"]
2 changes: 1 addition & 1 deletion packages/apps/graph/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ runSystemsCheck()
.then(() => {
httpServer.listen(dotenv.PORT, () => {
console.info(
`\nServer is running on http://localhost:${dotenv.PORT}/graphql\n`,
`\nServer is running on http://${dotenv.HOST}:${dotenv.PORT}/graphql\n`,
);
});
})
Expand Down
2 changes: 2 additions & 0 deletions packages/apps/graph/src/utils/dotenv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export const dotenv: {
NETWORK_STATISTICS_URL: string;
NODE_ENV: string;
ENVIRONMENT: string;
HOST: string;
PORT: number;
PRISMA_LOGGING_ENABLED: boolean;
PRISMA_LOG_TO_FILE: boolean;
Expand Down Expand Up @@ -105,6 +106,7 @@ export const dotenv: {
),
NODE_ENV: or(process.env.NODE_ENV, 'production'),
ENVIRONMENT: or(process.env.ENVIRONMENT, 'mainnet'),
HOST: or(process.env.HOST, 'localhost'),
PORT: parseInt(or(process.env.PORT, '4000'), 10),
PRISMA_LOGGING_ENABLED: or(
process.env.PRISMA_LOGGING_ENABLED?.toLocaleLowerCase() === 'true',
Expand Down
Loading

0 comments on commit 44aad8c

Please sign in to comment.