This repository has been archived by the owner on Oct 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
Here is a working example with Mongoengine #1
Comments
I'm glad that you've found this lib useful for your project. P.S. I've added a link in the readme on this example |
Hello, example is good but not complete, wondering if you can help wireup following:
users.py from graphene import ObjectType, String, ID, List, Int, Field
from flask import Flask
from flask_graphql import GraphQLView
import json
import urllib.request
from graphene_federation import build_schema, key, extend, external
def get_users():
with urllib.request.urlopen("https://jsonplaceholder.typicode.com/users") as req:
return json.loads(req.read().decode())
def get_user(id):
with urllib.request.urlopen("https://jsonplaceholder.typicode.com/users/" + id) as req:
return json.loads(req.read().decode())
@extend('id')
class Post(ObjectType):
id = external(ID())
# userId = ID()
# user = Field(lambda: User)
# def resolve_user(self, info):
# return get_user(self.userId)
@key('id')
class User(ObjectType):
name = String()
username = String()
email = String()
id = ID()
# posts = List(lambda: Post)
def __resolve_reference(self, info, **kwargs):
return get_user(self.id)
class Query(ObjectType):
users = List(lambda: User)
user = Field(lambda: User, id=ID(required=True))
def resolve_users(self, info):
return get_users()
def resolve_user(self, info, id):
return get_user(id)
app = Flask(__name__)
app.debug = True
schema = build_schema(query=Query)
app.add_url_rule(
'/graphql', view_func=GraphQLView.as_view('graphql', schema=schema, graphiql=True))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5002) posts.py from graphene import ObjectType, String, ID, List, Int, Field
from flask import Flask
from flask_graphql import GraphQLView
import json
import urllib.request
from graphene_federation import build_schema, key, extend, external
def get_posts():
with urllib.request.urlopen("https://jsonplaceholder.typicode.com/posts") as req:
return json.loads(req.read().decode())
def get_post(id):
with urllib.request.urlopen("https://jsonplaceholder.typicode.com/posts/" + id) as url:
return json.loads(url.read().decode())
def get_posts_by(userId):
with urllib.request.urlopen("https://jsonplaceholder.typicode.com/posts?userId=" + str(userId)) as req:
return json.loads(req.read().decode())
@extend('id')
class User(ObjectType):
id = external(ID())
# userId = ID()
# posts = List(lambda: Post)
# def resolve_posts(self, info):
# return get_posts_by(self.id)
@key('id')
class Post(ObjectType):
id = ID()
userId = ID()
title = String()
body = String()
# user = Field(lambda: User)
def __resolve_reference(self, info, **kwargs):
return get_post(self.id)
class Query(ObjectType):
posts = List(lambda: Post)
post = Field(lambda: Post, id=ID(required=True))
def resolve_posts(self, info):
return get_posts()
def resolve_post(self, info, id):
return get_post(id)
app = Flask(__name__)
app.debug = True
schema = build_schema(query=Query)
app.add_url_rule(
'/graphql', view_func=GraphQLView.as_view('graphql', schema=schema, graphiql=True))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5001) Desired schema in gateway:
|
@mac2000 |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Hello!
Thank you so much for creating this awesome tool. We are currently working with GraphQL + Mongoengine. We split up into Microservices, so Federation was the thing to do. After fiddling around for a day, I was able to create a working example with Mongoengine. I want to share it, just in case somebody needs it too.
1. Overview
The example is made up of two Services. Each Service has its own DB. We want to be able to connect these to DBs, but have access via 1 GraphQL Endpoint to be able to connect multiple Frontend Applications. I choose the following file-structure:
2. Models
We have two DB-Models, a
User
and aReview
. Each of the models is sitting in its own Database. They are only connected via Mongoengine.models/ReviewModel.py
models/UserModel.py
3. Services
I wasn't very creative on the service. I just called them
service1
andservice2
. Service1 is handling the users and Service2 the reviews.Both have the same structure:
server.py
The server.py file is the same in both services, except for the port to avoid conflicts. I am using a UploadView. We are using File uploads, so this a custom option.
app.py
The app.py is even simpler.
config.py
Nothing special here. Fill in your own details.
schema.py
The schema file is the big difference in the two services. Let's start by the User.
service1
As you can see, nothing special happens here. We just need to set the
id
as our key and add a resolver for the key.Service2 will now be able to resolve a type from another schema by using the
external
function.service2
4. Gateway
To combine our 2 Services into one gateway, we use the Apollo Gateway.
We just need to create the following file:
gateway.js
Next we need to install the depencies:
5. Start it up
The last thing is to just run the two flask applications and start the gateway via
node gateway.js
I hope all the instructions are clear enough. Have a great day!!!
The text was updated successfully, but these errors were encountered: