Skip to content

Commit d572ea2

Browse files
authored
Merge pull request #602 from PokeAPI/graphql
2 parents dec36df + 7714b57 commit d572ea2

File tree

179 files changed

+4949
-66
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+4949
-66
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ db.*
77
venv*
88
node_modules
99
.vscode
10+
.env
11+
Resources/nginx/ssl/*
12+
!Resources/nginx/ssl/*.sample.*

Makefile

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
veekun_pokedex_repository = ../pokedex
22
local_config = --settings=config.local
33
docker_config = --settings=config.docker-compose
4+
HASURA_GRAPHQL_ADMIN_SECRET=pokemon
45

56
.PHONY: help
7+
.SILENT:
68

79
help:
810
@grep -E '^[a-zA-Z_-]+:.*?# .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?# "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
@@ -81,8 +83,20 @@ pull-veekun:
8183
git -C ${veekun_pokedex_repository} checkout master-pokeapi
8284
git -C ${veekun_pokedex_repository} pull
8385

84-
sync-from-veekun: pull pull-veekun # Copy data from ../pokedex to this repository
86+
sync-from-veekun: pull pull-veekun # Copy data from ../pokedex to this repository
8587
cp -a ${veekun_pokedex_repository}/pokedex/data/csv/. ./data/v2/csv
8688

87-
sync-to-veekun: pull pull-veekun # Copy data from this repository to ../pokedex
89+
sync-to-veekun: pull pull-veekun # Copy data from this repository to ../pokedex
8890
cp -a ./data/v2/csv/. ${veekun_pokedex_repository}/pokedex/data/csv
91+
92+
# read-env-file: # Exports ./.env into shell environment variables
93+
# export `egrep -v '^#' .env | xargs`
94+
95+
hasura-export: # Export Hasura configuration
96+
hasura md export --project graphql --admin-secret ${HASURA_GRAPHQL_ADMIN_SECRET}
97+
98+
hasura-apply: # Apply local Hasura configuration
99+
hasura md apply --project graphql --admin-secret ${HASURA_GRAPHQL_ADMIN_SECRET}
100+
101+
hasura-get-anon-schema: # Dumps GraphQL schema
102+
gq http://localhost:8080/v1/graphql --introspect > graphql/schema.graphql

README.md

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,7 @@
1818

1919
A RESTful API for Pokémon - [pokeapi.co](https://pokeapi.co)
2020

21-
## Join Us On Slack!
22-
23-
Have a question or just want to discuss new ideas and improvements? Hit us up on slack. Consider talking with us here before creating new issue.
24-
This way we can keep issues here a bit more organized and helpful in the long run. Be excellent to each other :smile:
25-
26-
[Sign up](https://pokeapi-slack-invite.herokuapp.com/) easily!
27-
28-
Once you've signed up visit [PokéAPI on Slack](https://pokeapi.slack.com)
29-
30-
## Official Wrappers
31-
32-
* Node server-side [PokeAPI/pokedex-promise-v2](https://github.com/PokeAPI/pokedex-promise-v2) | _Auto caching_
33-
* Browser client-side [PokeAPI/pokeapi-js-wrapper](https://github.com/PokeAPI/pokeapi-js-wrapper) | _Auto caching_
34-
* Java/Kotlin [PokeAPI/pokekotlin](https://github.com/PokeAPI/pokekotlin)
35-
* Python 3 [GregHilmes/pokebase](https://github.com/GregHilmes/pokebase) | _Auto caching_
36-
* Python 2/3 [PokeAPI/pokepy](https://github.com/PokeAPI/pokepy) | _Auto caching_
37-
* PHP [lmerotta/phpokeapi](https://github.com/lmerotta/phpokeapi) | _Auto caching, lazy loading_
38-
* Ruby [rdavid1099/poke-api-v2](https://github.com/rdavid1099/poke-api-v2)
39-
* .Net Standard [mtrdp642/PokeApiNet](https://github.com/mtrdp642/PokeApiNet) | _Auto caching_
40-
* Go [mtslzr/pokeapi-go](https://github.com/mtslzr/pokeapi-go) | _Auto caching_
41-
* Dart [prathanbomb/pokedart](https://github.com/prathanbomb/pokedart)
42-
* Rust [lunik1/pokerust](https://gitlab.com/lunik1/pokerust) | _Auto caching_
43-
* Spring Boot [dlfigueira/spring-pokeapi](https://github.com/dlfigueira/spring-pokeapi) | _Auto caching_
44-
* Swift [kinkofer/PokemonAPI](https://github.com/kinkofer/PokemonAPI)
21+
> Beta GraphQL support is rolling out! Check out the [GraphQL paragraph](#graphql) for more info.
4522
4623
## Setup [![pyVersion37](https://img.shields.io/badge/python-3.7-blue.svg)](https://www.python.org/download/releases/3.7/)
4724

@@ -81,7 +58,7 @@ from data.v2.build import build_all
8158
build_all()
8259
```
8360

84-
Visit [localhost:8000/api/v2/](localhost:8000/api/v2/) to see the running API!
61+
Visit [localhost:80/api/v2/](localhost:80/api/v2/) to see the running API!
8562

8663
Each time the build script is run, it will iterate over each table in the database, wipe it, and rewrite each row using the data found in data/v2/csv.
8764

@@ -95,9 +72,9 @@ make wipe_db
9572

9673
## Docker and Compose
9774

98-
There is also a multi-container setup, managed by [Docker Compose](https://docs.docker.com/compose/). This setup allow you to deploy a production-like environment, with separate containers for each services.
75+
There is also a multi-container setup, managed by [Docker Compose](https://docs.docker.com/compose/). This setup allows you to deploy a production-like environment, with separate containers for each services and is recommended if you need to simply spin up PokeAPI.
9976

100-
Start everything by simply
77+
Start everything by
10178

10279
```sh
10380
make docker-setup
@@ -113,14 +90,59 @@ docker-compose exec -T app sh -c 'echo "from data.v2.build import build_all; bui
11390
11491
Browse [localhost/api/v2/](http://localhost/api/v2/) or [localhost/api/v2/pokemon/bulbasaur/](http://localhost/api/v2/pokemon/bulbasaur/) on port `80`.
11592
93+
## GraphQL
94+
95+
<a href="ttps://github.com/hasura/graphql-engine">
96+
<img width="100px" src="https://graphql-engine-cdn.hasura.io/img/powered_by_hasura_blue.svg" />
97+
</a>
98+
99+
When you start PokeAPI with the above docker-compose setup, an [Hasura Engine](https://github.com/hasura/graphql-engine) server is started as well. It's possible to track all the PokeAPI tables and foreign keys by simply
100+
101+
```sh
102+
# hasura cli needs to be installed and available in your $PATH: https://hasura.io/docs/latest/graphql/core/hasura-cli/install-hasura-cli.html
103+
# hasura cli's version has to be v2.0.0-alpha.5
104+
make hasura-apply
105+
```
106+
107+
When finished browse http://localhost:8080 and you will find the admin console. The GraphQL endpoint will be hosted at http://localhost:8080/v1/graphql.
108+
109+
A free public GraphiQL console is browsable at the address https://beta.pokeapi.co/graphql/console/. The relative GraphQL endpoint is accessible at https://beta.pokeapi.co/graphql/v1beta
110+
111+
A set of examples are provided in the directory [/graphql/examples](./graphql/examples) of this repository.
112+
113+
## Official REST Wrappers
114+
115+
* Node server-side [PokeAPI/pokedex-promise-v2](https://github.com/PokeAPI/pokedex-promise-v2) | _Auto caching_
116+
* Browser client-side [PokeAPI/pokeapi-js-wrapper](https://github.com/PokeAPI/pokeapi-js-wrapper) | _Auto caching_
117+
* Java/Kotlin [PokeAPI/pokekotlin](https://github.com/PokeAPI/pokekotlin)
118+
* Python 3 [GregHilmes/pokebase](https://github.com/GregHilmes/pokebase) | _Auto caching_
119+
* Python 2/3 [PokeAPI/pokepy](https://github.com/PokeAPI/pokepy) | _Auto caching_
120+
* PHP [lmerotta/phpokeapi](https://github.com/lmerotta/phpokeapi) | _Auto caching, lazy loading_
121+
* Ruby [rdavid1099/poke-api-v2](https://github.com/rdavid1099/poke-api-v2)
122+
* .Net Standard [mtrdp642/PokeApiNet](https://github.com/mtrdp642/PokeApiNet) | _Auto caching_
123+
* Go [mtslzr/pokeapi-go](https://github.com/mtslzr/pokeapi-go) | _Auto caching_
124+
* Dart [prathanbomb/pokedart](https://github.com/prathanbomb/pokedart)
125+
* Rust [lunik1/pokerust](https://gitlab.com/lunik1/pokerust) | _Auto caching_
126+
* Spring Boot [dlfigueira/spring-pokeapi](https://github.com/dlfigueira/spring-pokeapi) | _Auto caching_
127+
* Swift [kinkofer/PokemonAPI](https://github.com/kinkofer/PokemonAPI)
128+
116129
## Donations
117130

118-
Help to keep PokéAPI running! If you're using PokéAPI as a teaching resource or for a project, consider sending us a $10 donation to help keep the service up. We get almost 20 million requests a month!
131+
Help to keep PokéAPI running! If you're using PokéAPI as a teaching resource or for a project, consider sending us a $10 donation to help keep the service up. We get 60 million requests a month!
119132
120133
Thank you to all our backers! [Become a backer](https://opencollective.com/pokeapi#backer)
121134
122135
<a href="https://opencollective.com/pokeapi#backers" target="_blank"><img src="https://opencollective.com/pokeapi/backers.svg?width=890"></a>
123136
137+
## Join Us On Slack!
138+
139+
Have a question or just want to discuss new ideas and improvements? Hit us up on slack. Consider talking with us here before creating new issue.
140+
This way we can keep issues here a bit more organized and helpful in the long run. Be excellent to each other :smile:
141+
142+
[Sign up](https://pokeapi-slack-invite.herokuapp.com/) easily!
143+
144+
Once you've signed up visit [PokéAPI on Slack](https://pokeapi.slack.com)
145+
124146
## Contributing
125147

126148
This project exists thanks to all the people who [contribute](https://github.com/PokeAPI/pokeapi/blob/master/CONTRIBUTING.md)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
version: '2.4'
2+
services:
3+
db:
4+
environment:
5+
POSTGRES_PASSWORD: "${POSTGRES_PASSWORD}"
6+
POSTGRES_USER: "${POSTGRES_USER}"
7+
8+
app:
9+
env_file: .env
10+
11+
web:
12+
volumes:
13+
- graphiql:/public-console:ro
14+
15+
graphql-engine:
16+
environment:
17+
HASURA_GRAPHQL_DATABASE_URL: "postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/pokeapi"
18+
HASURA_GRAPHQL_ADMIN_SECRET: "${HASURA_GRAPHQL_ADMIN_SECRET}"
19+
20+
graphiql:
21+
image: pokeapi/graphiql:1.0.1
22+
command: sh -c 'cp -a /app/static/. /transfer/ && tail -f /etc/passwd'
23+
volumes:
24+
- graphiql:/transfer
25+
depends_on:
26+
- graphql-engine
27+
28+
volumes:
29+
graphiql:
30+
31+
# docker-compose -f docker-compose.yml -f Resources/compose/docker-compose-graphql.yml up

Resources/docker/web/Dockerfile

Lines changed: 0 additions & 2 deletions
This file was deleted.

Resources/nginx/nginx.conf

Lines changed: 64 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
worker_processes 4; # 80$ droplet, so 4 core enabled
1+
worker_processes 2;
22

33
events {
4-
worker_connections 8096; # accepted incoming connections*2
4+
worker_connections 4096;
55
multi_accept on; # accept each connection as soon as you can
66
accept_mutex off;
77
use epoll;
@@ -17,75 +17,111 @@ http {
1717

1818
client_body_buffer_size 10K; # raise the threshold by which requests are written to HDD instead of RAM
1919
client_header_buffer_size 2k;
20-
client_max_body_size 8m; # we dont accept requests larger that 8mb, probably you are DoSing us
20+
client_max_body_size 8m; # we dont accept requests larger that 8mb
2121

2222
sendfile on;
2323
tcp_nopush on;
2424
tcp_nodelay on;
2525

2626
keepalive_timeout 5;
2727

28-
gzip on; # enable zipping files
29-
gzip_vary on;
30-
gzip_min_length 5120; # enable it only for medium-big files (slowbro size is 7000)
31-
gzip_proxied expired no-cache no-store private auth;
32-
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml application/json;
28+
gzip on;
3329
gzip_disable "msi6";
30+
gzip_vary on;
31+
gzip_proxied any;
32+
gzip_comp_level 6;
33+
gzip_buffers 16 8k;
34+
gzip_http_version 1.1;
35+
gzip_min_length 256;
36+
gzip_types application/atom+xml application/geo+json application/javascript application/x-javascript application/json application/ld+json application/manifest+json application/rdf+xml application/rss+xml application/xhtml+xml application/xml font/eot font/otf font/ttf image/svg+xml text/css text/javascript text/plain text/xml;
3437

3538
upstream pokeapi_upstream {
3639
# 'app' is the Django container name in Docker
37-
# DO NOT EDIT IT ALONE or it'll break docker-compose
3840
server app:8000 fail_timeout=0;
3941
}
4042

43+
upstream graphql_upstream {
44+
server graphql-engine:8080 fail_timeout=0;
45+
}
46+
47+
geo $limit {
48+
default 1;
49+
10.0.0.0/8 0;
50+
192.168.0.0/24 0;
51+
}
52+
53+
map $limit $limit_key {
54+
0 "";
55+
1 $binary_remote_addr;
56+
}
57+
58+
limit_req_zone $limit_key zone=graphqlDefaultLimit:50m rate=1r/m;
59+
limit_conn_zone $binary_remote_addr zone=addr:20m;
60+
4161
server {
4262
listen 80 deferred;
4363
server_name _;
64+
root /code;
65+
66+
include /ssl/ssl.conf*;
4467

4568
client_body_timeout 5s;
4669
client_header_timeout 5s;
70+
limit_conn addr 10;
4771

48-
root /code;
49-
50-
location /media/ {
51-
root /code;
52-
autoindex off;
72+
# Admin console
73+
location /graphql/admin/ {
74+
expires 1m; # client-side caching, one minute for each API resource
75+
add_header Cache-Control "public";
76+
add_header Pragma public;
77+
proxy_http_version 1.1;
78+
proxy_set_header Upgrade $http_upgrade;
79+
proxy_set_header Connection "upgrade";
80+
proxy_set_header X-Real-IP $remote_addr;
81+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
82+
proxy_set_header Host $http_host;
83+
proxy_redirect off;
84+
proxy_pass http://graphql_upstream/;
5385
}
5486

55-
location /static/ {
56-
alias /code/assets/;
57-
autoindex off;
87+
location /graphql/console {
88+
alias /public-console/;
89+
index index.html;
90+
try_files $uri $uri/ /index.html =405;
5891
}
59-
60-
location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ { # cache all these extensions for 5 days, disable logging
61-
access_log off;
62-
log_not_found off;
63-
expires 5d;
92+
93+
location /graphql/v1beta {
94+
limit_req zone=graphqlDefaultLimit burst=100 nodelay;
95+
limit_req_status 429;
96+
expires 1m; # client-side caching, one minute for each API resource
6497
add_header Cache-Control "public";
6598
add_header Pragma public;
99+
proxy_http_version 1.1;
100+
proxy_set_header Upgrade $http_upgrade;
101+
proxy_set_header Connection "upgrade";
102+
proxy_set_header X-Real-IP $remote_addr;
103+
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
104+
proxy_set_header Host $http_host;
105+
proxy_redirect off;
106+
proxy_pass http://graphql_upstream/v1/graphql;
66107
}
67-
108+
68109
location /api/ {
69110
expires 1m; # client-side caching, one minute for each API resource
70111
add_header Cache-Control "public";
71112
add_header Pragma public;
72-
73113
proxy_set_header X-Real-IP $remote_addr;
74114
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
75115
proxy_set_header Host $http_host;
76-
77116
proxy_redirect off;
78-
79117
proxy_pass http://pokeapi_upstream;
80118
}
81119

82120
location / {
83121
proxy_set_header X-Real-IP $remote_addr;
84122
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
85123
proxy_set_header Host $http_host;
86-
87124
proxy_redirect off;
88-
89125
proxy_pass http://pokeapi_upstream;
90126
}
91127
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Copy this file to ssl.conf, add certificate and private key to enable ssl
2+
3+
listen 443 ssl http2;
4+
listen [::]:443 ssl http2;
5+
6+
ssl_certificate /ssl/cert.cer;
7+
ssl_certificate_key /ssl/key.key;
8+
ssl_session_timeout 1d;
9+
ssl_session_cache shared:MozSSL:10m;
10+
ssl_session_tickets off;
11+
12+
ssl_protocols TLSv1.2 TLSv1.3;
13+
ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
14+
ssl_prefer_server_ciphers off;

apollo.config.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module.exports = {
2+
client: {
3+
service: {
4+
name: "pokeapi",
5+
url: "http://localhost:8080/v1/graphql",
6+
headers: {
7+
"x-hasura-admin-secret": "pokemon",
8+
},
9+
},
10+
},
11+
};

config/docker-compose.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
# Docker settings
2+
import os
23
from .settings import *
34

45
DATABASES = {
56
"default": {
67
"ENGINE": "django.db.backends.postgresql_psycopg2",
78
"NAME": "pokeapi",
8-
"USER": "ash",
9-
"PASSWORD": "pokemon",
9+
"USER": os.environ.get("POSTGRES_USER", "ash"),
10+
"PASSWORD": os.environ.get("POSTGRES_PASSWORD", "pokemon"),
1011
"HOST": "db",
1112
"PORT": 5432,
1213
}
@@ -25,3 +26,5 @@
2526

2627
DEBUG = False
2728
TASTYPIE_FULL_DEBUG = False
29+
30+
ALLOWED_HOSTS = ["*"]

0 commit comments

Comments
 (0)