Skip to content

Instantly share code, notes, and snippets.

@rwcitek
Last active December 24, 2023 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rwcitek/594f16d80acd720c2191c6dc6a7f7742 to your computer and use it in GitHub Desktop.
Save rwcitek/594f16d80acd720c2191c6dc6a7f7742 to your computer and use it in GitHub Desktop.
GraphQL, FastAPI, Areadne in Docker

Create background container

docker container run --rm -d --name gql -p 8000:80 ubuntu sleep inf
sleep 2

Configure container

{ cat <<'eof2'

apt-get update &&
apt-get install -y python3-pip

pip install fastapi uvicorn ariadne

mkdir -p /tmp/gql
cd /tmp/gql

cut -c3- <<'eof' > main.py
  from fastapi import FastAPI
  from ariadne import gql, QueryType, make_executable_schema
  from ariadne.asgi import GraphQL
  
  app = FastAPI()
  
  type_defs = gql("""
      type Query {
          hello: String!
      }
  """)
  
  query = QueryType()
  
  @query.field("hello")
  def resolve_hello(*_):
      return "Hello, world!"
  
  schema = make_executable_schema(type_defs, query)
  
  app.add_route("/graphql", GraphQL(schema, debug=True))
eof
eof2
} | docker container exec -i gql /bin/bash

Run app

docker container exec -w /tmp/gql gql \
  uvicorn main:app --host 0.0.0.0 --port 80 --reload

Test

... in another terminal

curl -s -X POST \
  -H "Content-Type: application/json" \
  -d '{"query":"query { hello }"}' \
  http://localhost:8000/graphql |
jq .

... or in a browser

http://localhost:8000/graphql

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment