Skip to content

Latest commit

 

History

History
60 lines (38 loc) · 1.68 KB

File metadata and controls

60 lines (38 loc) · 1.68 KB

How to write a python GraphQL server?

  1. First you write a schema:
    1. You write your types. I think of these as the nouns.
    2. Next you write queries on those types. I think of these as the reads, or your GET API endpoints, or your SELECT queries in SQL.
    3. You write mutations. These are your writes. Maybe your inserts, updates, and deletes.
    4. I'm skipping how to write subscriptions. They sure look cool!
  2. Outside your schema, you write resolvers for your queries and for your mutations.

Stupidest possible schema

ariadne_example.py

Use it like this:

curl -H "Content-Type: application/json" \
-X POST \
-d '{"query": "query {demo {current_timestamp bogus_field_a bogus_field_b} }"}' \
http://127.0.0.1:8000

The schema for that simple bar

bogus_schema.graphql

There's a war going on over how much should we write these schemas by hand, versus use code to build them up, versus use code to infer them by studying the underlying datasources, like databases, or REST APIs.

Warnings

  • When you write resolvers, you need to make sure that you think through who is asking for what data.

    In other words, GraphQL doesn't solve thorny authorization issues.

    For example, imagine that you make a GraphQL interface for a bank. You limit access to logged-in users. But in your resolvers, you need to make sure that Matt is only accessing data that Matt is blessed to access.