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

External type with interface #7

Merged
merged 3 commits into from
Sep 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions graphene_federation/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ def get_sdl(schema, custom_entities):
string_schema = pattern.sub(" ", string_schema)

for entity_name, entity in custom_entities.items():
type_def = "type %s " % entity_name
repl_str = "%s %s " % (type_def, entity._sdl)
pattern = re.compile(type_def)
type_def_re = r"(type %s[^\{]*)" % entity_name
repl_str = r"\1 %s " % entity._sdl
pattern = re.compile(type_def_re)
string_schema = pattern.sub(repl_str, string_schema)

for entity_name, entity in extended_types.items():
Expand Down
15 changes: 10 additions & 5 deletions integration_tests/service_a/src/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from graphene import ObjectType, String, Int, List, NonNull
from graphene import ObjectType, String, Int, List, NonNull, Field
from graphene_federation import build_schema, extend, external


Expand All @@ -7,10 +7,15 @@ class FileNode(ObjectType):
id = external(Int(required=True))


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


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


Expand All @@ -20,9 +25,9 @@ class Query(ObjectType):

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'),
Post(id=1, title='title1', text=FunnyText(id=1), files=[FileNode(id=1)]),
Post(id=2, title='title2', text=FunnyText(id=2), files=[FileNode(id=2), FileNode(id=3)]),
Post(id=3, title='title3', text=FunnyText(id=3)),
]

def resolve_goodbye(root, info):
Expand Down
19 changes: 16 additions & 3 deletions integration_tests/service_b/src/schema.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
from graphene import ObjectType, String, Int, Field
from graphene import ObjectType, String, Int, Field, Interface
from graphene_federation import build_schema, key


class TextInterface(Interface):
id = Int(required=True)
body = String(required=True)


@key(fields='id')
class FunnyText(ObjectType):
class Meta:
interfaces = (TextInterface,)

def __resolve_reference(self, info, **kwargs):
return FunnyText(id=self.id, body=f'funny_text_{self.id}')


@key(fields='id')
class FileNode(ObjectType):
id = Int(required=True)
Expand All @@ -13,8 +27,7 @@ def __resolve_reference(self, info, **kwargs):


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


schema = build_schema(Query, types=[FileNode])
schema = build_schema(Query, types=[FileNode, FunnyText])
8 changes: 7 additions & 1 deletion integration_tests/tests/tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ def test_external_types():
query {
posts {
title
text
text {
id
body
}
files {
id
name
Expand All @@ -44,5 +47,8 @@ def test_external_types():
posts = json.loads(response.content)['data']['posts']
assert 3 == len(posts)
assert [{"id": 1, "name": "file_1"}] == posts[0]['files']
assert {"id": 1, "body": "funny_text_1"} == posts[0]['text']
assert [{"id": 2, "name": "file_2"}, {"id": 3, "name": "file_3"}] == posts[1]['files']
assert {"id": 2, "body": "funny_text_2"} == posts[1]['text']
assert posts[2]['files'] is None
assert {"id": 3, "body": "funny_text_3"} == posts[2]['text']