Skip to content
This repository has been archived by the owner on Oct 21, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5 from erebus1/add-more-integration-tests
Browse files Browse the repository at this point in the history
add external types integration test
  • Loading branch information
erebus1 authored Aug 11, 2019
2 parents f6834af + 50f7bfc commit 911cde6
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ build:
cd integration_tests && docker-compose build

test:
cd integration_tests && docker-compose run --rm tests
cd integration_tests && docker-compose down && docker-compose run --rm tests
6 changes: 3 additions & 3 deletions graphene_federation/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def _get_query(schema, query_cls):
return federated_query_cls


def build_schema(query_cls, **kwargs):
schema = graphene.Schema(query=query_cls, **kwargs)
return graphene.Schema(query=_get_query(schema, query_cls), **kwargs)
def build_schema(query, **kwargs):
schema = graphene.Schema(query=query, **kwargs)
return graphene.Schema(query=_get_query(schema, query), **kwargs)


def key(fields: str):
Expand Down
8 changes: 8 additions & 0 deletions integration_tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ services:
- ../:/project/federation_deps
command: sh -c "pip install ./federation_deps && python ./src/app.py"

service_b:
build: service_b/.
volumes:
- ./service_b/src:/project/src
- ../:/project/federation_deps
command: sh -c "pip install ./federation_deps && python ./src/app.py"

federation:
build: federation/.
volumes:
- ./federation/src:/project/src
depends_on:
- service_a
- service_b
ports:
- 3000:3000
healthcheck:
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/federation/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import {ApolloServer} from 'apollo-server'
import {ApolloGateway} from '@apollo/gateway'

const serviceA_url: string = 'http://service_a:5000/graphql';
const serviceB_url: string = 'http://service_b:5000/graphql';

const gateway = new ApolloGateway({
serviceList: [
{ name: 'service_a', url: serviceA_url },
{ name: 'service_b', url: serviceB_url },
],
});

Expand Down
33 changes: 24 additions & 9 deletions integration_tests/service_a/src/schema.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,32 @@
from graphene import ObjectType, String, Schema
from graphene_federation import build_schema
from graphene import ObjectType, String, Int, List, NonNull
from graphene_federation import build_schema, extend, external


@extend(fields='id')
class FileNode(ObjectType):
id = external(Int(required=True))


class Post(ObjectType):
id = Int(required=True)
title = String(required=True)
text = String(required=True)
files = List(NonNull(FileNode))


class Query(ObjectType):
# this defines a Field `hello` in our Schema with a single Argument `name`
hello = String(name=String(default_value="stranger"))
goodbye = String()
posts = List(NonNull(Post))

# our Resolver method takes the GraphQL context (root, info) as well as
# Argument (name) for the Field and returns data for the query Response
def resolve_hello(root, info, name):
return f'Hello {name}!'
def resolve_posts(root, info):
return [
Post(id=1, title='title1', text='text1', files=[FileNode(id=1)]),
Post(id=2, title='title2', text='text2', files=[FileNode(id=2), FileNode(id=3)]),
Post(id=3, title='title3', text='text3'),
]

def resolve_goodbye(root, info):
return 'See ya!'

schema = build_schema(Query)

schema = build_schema(query=Query)
9 changes: 9 additions & 0 deletions integration_tests/service_b/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM python:3.6-alpine3.9

WORKDIR project

COPY ./requirements.txt ./
RUN pip install -r requirements.txt

EXPOSE 5000
CMD [ "python", "./src/app.py"]
3 changes: 3 additions & 0 deletions integration_tests/service_b/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
graphene==2.1.7
flask==1.1.1
flask_graphql==2.0.0
Empty file.
12 changes: 12 additions & 0 deletions integration_tests/service_b/src/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from flask import Flask

from flask_graphql import GraphQLView
from schema import schema

app = Flask(__name__)
app.debug = True

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')
20 changes: 20 additions & 0 deletions integration_tests/service_b/src/schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from graphene import ObjectType, String, Int, Field
from graphene_federation import build_schema, key


@key(fields='id')
class FileNode(ObjectType):
id = Int(required=True)
name = String(required=True)

def __resolve_reference(self, info, **kwargs):
# todo test raise exception here
return FileNode(id=self.id, name=f'file_{self.id}')


class Query(ObjectType):
# todo support query w/o root fields
file = Field(lambda: FileNode)


schema = build_schema(Query, types=[FileNode])
30 changes: 29 additions & 1 deletion integration_tests/tests/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import requests


def test_federation_can_integrate_simple_schema():
def test_integrate_simple_schema():
query = {
'query': """
query {
Expand All @@ -18,3 +18,31 @@ def test_federation_can_integrate_simple_schema():
assert response.status_code == 200
data = json.loads(response.content)['data']
assert data['goodbye'] == "See ya!"


def test_external_types():
query = {
'query': """
query {
posts {
title
text
files {
id
name
}
}
}
""",
'variables': {}
}
response = requests.post(
'http://federation:3000/graphql/',
json=query,
)
assert response.status_code == 200
posts = json.loads(response.content)['data']['posts']
assert 3 == len(posts)
assert [{"id": 1, "name": "file_1"}] == posts[0]['files']
assert [{"id": 2, "name": "file_2"}, {"id": 3, "name": "file_3"}] == posts[1]['files']
assert posts[2]['files'] is None

0 comments on commit 911cde6

Please sign in to comment.