Last active
August 20, 2020 08:12
-
-
Save ahmad88me/d489b8aa87e71d24e28606e5b502ab28 to your computer and use it in GitHub Desktop.
graphQL mongodb graphene embedded document
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, jsonify, request | |
import random | |
from flask_graphql import GraphQLView | |
from database import init_db | |
from schema import schema | |
app = Flask(__name__) | |
default_query = """ | |
{ | |
allColumns { | |
edges { | |
node { | |
id | |
dataset | |
uri | |
distribution | |
distributionFormat | |
cells | |
control | |
} | |
} | |
} | |
} | |
""" | |
app.add_url_rule( | |
'/graphql', | |
view_func=GraphQLView.as_view('graphql', schema=schema, graphiql=True, default_query=default_query) | |
) | |
if __name__ == '__main__': | |
init_db() | |
app.run(host='0.0.0.0', debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from mongoengine import connect | |
from models import Column, ToolCandidatePair | |
# connect('graphene-mongo-example', host='mongomock://localhost', alias='default') | |
connect('example', host='mongodb://127.0.0.1', alias='default') | |
def init_db(): | |
football = Column() | |
football.distribution = "football.csv" | |
football.distribution_format = "CSV" | |
football.dataset = "Sports" | |
football.uri = "http://dbpedia.org/ontology/SoccerPlayer" | |
football.cells = ["Playerf1", "Playerf2", "Playerf3"] | |
football.control = True | |
pair1 = ToolCandidatePair() | |
pair1.tool = "tool1" | |
pair1.candidates = ["http://dbpedia.org/ontology/Person", "http://umbel.org/umbel/rc/SoccerPlayer"] | |
football.candidate_tool_pairs = [pair1] | |
football.save() | |
basketball = Column() | |
basketball.distribution = "basketball.csv" | |
basketball.distribution_format = "CSV" | |
basketball.dataset = "Sports" | |
basketball.uri = "http://dbpedia.org/ontology/BasketballPlayer" | |
basketball.cells = ["Playerb1", "Playerb2", "Playerb3", "Playerb4"] | |
basketball.control = True | |
pair1 = ToolCandidatePair() | |
pair1.tool = "tool1" | |
pair1.candidates = ["http://dbpedia.org/ontology/Person", "http://umbel.org/umbel/rc/BasketballPlayer"] | |
basketball.candidate_tool_pairs = [pair1] | |
basketball.save() | |
basketball = Column() | |
basketball.distribution = "basketball2.csv" | |
basketball.distribution_format = "CSV" | |
basketball.dataset = "Sports" | |
basketball.uri = "http://dbpedia.org/ontology/BasketballPlayer" | |
basketball.cells = ["Playerb11", "Playerb21", "Playerb31", "Playerb41"] | |
basketball.control = True | |
pair1 = ToolCandidatePair() | |
pair1.tool = "tool2" | |
pair1.candidates = ["http://dbpedia.org/ontology/Person", "http://umbel.org/umbel/rc/BasketballPlayer"] | |
basketball.candidate_tool_pairs = [pair1] | |
basketball.save() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from mongoengine import Document, EmbeddedDocument | |
from mongoengine.fields import ( | |
DateTimeField, ReferenceField, StringField, URLField, ListField, BooleanField, EmbeddedDocumentListField, | |
EmbeddedDocumentField | |
) | |
class ToolCandidatePair(EmbeddedDocument): | |
candidates = ListField(StringField()) | |
tool = StringField() | |
class Column(Document): | |
meta = {'collection': 'column'} | |
distribution = StringField() # boxers.csv, URL, ... | |
distribution_format = StringField() # (e.g., CSV, ...) MIME https://dublincore.org/specifications/dublin-core/dcmi-terms/#format | |
dataset = StringField() # The source (e.g., T2Dv2, Olympic Games, ..) | |
uri = URLField() # dbo:Person, dbp:height | |
# candidate_tool_pairs = ListField(EmbeddedDocumentField(ToolCandidatePair)) | |
candidate_tool_pairs = EmbeddedDocumentListField(ToolCandidatePair) # Other highly suggested classes (to be avoided by the option generator) | |
cells = ListField(StringField()) | |
control = BooleanField(default=False) | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import graphene | |
from graphene.relay import Node | |
from graphene_mongo import MongoengineConnectionField, MongoengineObjectType | |
from models import Column as ColumnModel | |
from models import ToolCandidatePair as ToolCandidatePairModel | |
class Column(MongoengineObjectType): | |
class Meta: | |
model = ColumnModel | |
interfaces = (Node,) | |
class Query(graphene.ObjectType): | |
node = Node.Field() | |
all_columns = MongoengineConnectionField(Column) | |
schema = graphene.Schema(query=Query, types=[Column]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment