Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for codegen rejecting operations that contain fragments with type conditions on interfaces #243

Merged
merged 2 commits into from
Sep 13, 2024
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
13 changes: 12 additions & 1 deletion sgqlc/codegen/operation.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ def _create_type(self, typ):
possible_types = typ['possibleTypes'] or typ['interfaces'] or []
typ['possibleTypes'] = {t['name'] for t in possible_types}

interfaces = typ['interfaces'] or []
typ['interfaces'] = {t['name'] for t in interfaces}

return typ

def _create_field(self, field):
Expand Down Expand Up @@ -743,7 +746,8 @@ def enter_inline_fragment(self, node, *_args):
self.type_stack.append(None)
return
type_name = node.type_condition.name.value
if type_name not in typ['possibleTypes']:
condition_type = self.validation.get_type(type_name)
if not self._type_valid_in_fragment(condition_type, typ):
self.report_possible_type_validation(node, typ, type_name)

try:
Expand All @@ -752,6 +756,13 @@ def enter_inline_fragment(self, node, *_args):
except KeyError as ex:
self.report_type_validation(node, ex)

@staticmethod
def _type_valid_in_fragment(condition_type, fragment_type):
return (
fragment_type['name'] in condition_type['interfaces']
or condition_type['name'] in fragment_type['possibleTypes']
)

def leave_inline_fragment(self, node, *_args):
self.type_stack.pop()
return (
Expand Down
232 changes: 232 additions & 0 deletions tests/test-operation-codegen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,232 @@
from io import StringIO
from graphql import Source

from sgqlc.codegen.operation import CodeGen, load_schema, ParsedSchemaName


NESTED_INTERFACE_QUERY = """
query TestQuery {
foo {
... on Bar {
barField
}
}
}
"""

NESTED_INTERFACE_SCHEMA = """
{
"__schema": {
"queryType": {
"name": "Query"
},
"mutationType": null,
"subscriptionType": null,
"types": [
{
"kind": "OBJECT",
"name": "Query",
"description": null,
"fields": [
{
"name": "foo",
"description": null,
"args": [],
"type": {
"kind": "INTERFACE",
"name": "Foo",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [],
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INTERFACE",
"name": "Foo",
"description": null,
"fields": [
{
"name": "fooField",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [],
"enumValues": null,
"possibleTypes": [
{
"kind": "OBJECT",
"name": "Baz",
"ofType": null
}
]
},
{
"kind": "SCALAR",
"name": "String",
"description": "The `String` scalar type represents textual data",
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{
"kind": "INTERFACE",
"name": "Bar",
"description": null,
"fields": [
{
"name": "fooField",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "barField",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [
{
"kind": "INTERFACE",
"name": "Foo",
"ofType": null
}
],
"enumValues": null,
"possibleTypes": [
{
"kind": "OBJECT",
"name": "Baz",
"ofType": null
}
]
},
{
"kind": "OBJECT",
"name": "Baz",
"description": null,
"fields": [
{
"name": "barField",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "fooField",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [
{
"kind": "INTERFACE",
"name": "Bar",
"ofType": null
},
{
"kind": "INTERFACE",
"name": "Foo",
"ofType": null
}
],
"enumValues": null,
"possibleTypes": null
}
]
}
}
"""
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@barbieri I cut out a lot of the introspection output to try and keep the inlined resource small, namely the metadata about builtin things like the __Schema and __Type types that describe what comes back in the introspection query itself and things related to directives. The test still ran fine, but do you think that may cause an issue later or do you not anticipate the codegen ever caring about that? Trying to avoid some future change suddenly breaking this test due to it having an incomplete introspection output.



def test_operation_gen_nested_interface():
expected_generated_query = """def query_test_query():
_op = sgqlc.operation.Operation(_schema_root.query_type, name='TestQuery')
_op_foo = _op.foo()
_op_foo__as__Bar = _op_foo.__as__(_schema.Bar)
_op_foo__as__Bar.bar_field()
return _op"""
result_buff = StringIO()
schema_name = ParsedSchemaName.parse_schema_name('.schema')

with StringIO(NESTED_INTERFACE_QUERY) as op_file, StringIO(
NESTED_INTERFACE_SCHEMA
) as schema_file:
operation_gql = [Source(op_file.read(), 'op-gen.gql')]
schema = load_schema(schema_file)
gen = CodeGen(
schema,
schema_name,
operation_gql,
result_buff.write,
short_names=False,
)
gen.write()
generated_operations = result_buff.getvalue()
result_buff.close()
assert expected_generated_query in generated_operations
Loading