Skip to content

Commit

Permalink
fix codegen rejecting operations that contained inline fragments with…
Browse files Browse the repository at this point in the history
… type conditions on interfaces because it was only checking possibleTypes, which cannot contain interfaces
  • Loading branch information
dsnam committed Sep 10, 2024
1 parent c972620 commit 2429ace
Show file tree
Hide file tree
Showing 4 changed files with 1,247 additions and 1 deletion.
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
29 changes: 29 additions & 0 deletions tests/test-operation-codegen.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import os
from io import StringIO
from graphql import Source

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


def test_operation_gen_nested_interface():
result_buff = StringIO()
test_schema_path = os.path.join(
'tests', 'test-resources', 'operation-codegen', 'operation-test.json'
)
test_op_path = os.path.join(
'tests', 'test-resources', 'operation-codegen', 'op-gen.gql'
)
schema_name = ParsedSchemaName.parse_schema_name('.schema')

with open(test_op_path) as op_file, open(test_schema_path) as schema_file:
operation_gql = [Source(op_file.read(), op_file.name)]
schema = load_schema(schema_file)
gen = CodeGen(
schema,
schema_name,
operation_gql,
result_buff.write,
short_names=False,
)
gen.write()
result_buff.close()
7 changes: 7 additions & 0 deletions tests/test-resources/operation-codegen/op-gen.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query QueryFieldOnNestedInterface {
foo {
... on Bar {
barField
}
}
}
Loading

0 comments on commit 2429ace

Please sign in to comment.