Skip to content

Instantly share code, notes, and snippets.

View birk-astrup's full-sized avatar

Birk Astrup birk-astrup

  • Netcompany
  • Oslo
View GitHub Profile
@birk-astrup
birk-astrup / schema.graphql
Last active November 1, 2019 13:42
Example graphql schema
type Query {
building_with_id(_id: ID!): Building
resident_with_id(_id: ID!): Resident
}
type Building {
id: ID!
buildYear: String!
residents: [Resident]
}
from ariadne import graphql_sync, make_executable_schema, load_schema_from_path, ObjectType, QueryType
from ariadne.constants import PLAYGROUND_HTML
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route('/graphql', methods=['GET'])
def playground():
return PLAYGROUND_HTML, 200
from ariadne import graphql_sync, make_executable_schema, load_schema_from_path, ObjectType, QueryType
from ariadne.constants import PLAYGROUND_HTML
from flask import Flask, request, jsonify
@birk-astrup
birk-astrup / buildings.json
Last active November 1, 2019 11:49
json files with data for the Flask/Ariadne example
{
"buildings": [
{
"id": "1",
"buildYear": 2009
},
{
"id": "2",
"buildYear": 2009
},
@birk-astrup
birk-astrup / type_defs.py
Last active November 1, 2019 13:52
setting type definitions from graphql schema
type_defs = load_schema_from_path('schema.graphql')
query = QueryType()
building = ObjectType('Building')
resident = ObjectType('Resident')
schema = make_executable_schema(type_defs, [building, resident, query])
import json
def building_with_id(_, info, _id):
with open('./data/buildings.json') as file:
data = json.load(file)
for building in data['buildings']:
if building['id'] == _id:
return building
from ariadne import graphql_sync, make_executable_schema, load_schema_from_path, ObjectType, QueryType
from ariadne.constants import PLAYGROUND_HTML
from flask import Flask, request, jsonify
import resolvers as r
app = Flask(__name__)
type_defs = load_schema_from_path('schema.graphql')
query = QueryType()
def resolve_residents_in_building(building, info):
print(building)
with open('./data/residents.json') as file:
data = json.load(file)
residents = [
resident
for resident
in data['residents']
if resident['building']
== building['id']]
building.set_field('residents', r.resolve_residents_in_building)