Skip to content

Commit

Permalink
Handle __typename in mutations.
Browse files Browse the repository at this point in the history
Requesting `__typename` for mutations no longer causes an error.

Issue #5985 (c)
  • Loading branch information
vpetrovykh committed Sep 18, 2023
1 parent f0a15e7 commit 6c55cf6
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 12 deletions.
28 changes: 16 additions & 12 deletions edb/graphql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -2045,20 +2045,24 @@ def get_field_type(self, name: str) -> Optional[GQLBaseType]:
fkey = (name, self.dummy)
target = None

op, name = name.split('_', 1)
if op in {'delete', 'insert', 'update'}:
if name == '__typename':
# It's a valid field that doesn't start with a command
target = super().get_field_type(name)
else:
op, name = name.split('_', 1)
if op in {'delete', 'insert', 'update'}:
target = super().get_field_type(name)

if target is None:
module, edb_name = self.get_module_and_name(name)
edb_qname = s_name.QualName(module=module, name=edb_name)
edb_type = self.edb_schema.get(
edb_qname,
default=None,
type=s_types.Type,
)
if edb_type is not None:
target = self.convert_edb_to_gql_type(edb_type)
if target is None:
module, edb_name = self.get_module_and_name(name)
edb_qname = s_name.QualName(module=module, name=edb_name)
edb_type = self.edb_schema.get(
edb_qname,
default=None,
type=s_types.Type,
)
if edb_type is not None:
target = self.convert_edb_to_gql_type(edb_type)

if target is not None:
self._fields[fkey] = target
Expand Down
136 changes: 136 additions & 0 deletions tests/test_http_graphql_mutation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2174,6 +2174,71 @@ def test_graphql_mutation_insert_readonly_02(self):
"Fixed": [data]
})

def test_graphql_mutation_insert_typename_01(self):
# This tests the typename funcitonality after insertion.
# Issue #5985
data = {
'__typename': 'other__Foo_Type',
'select': 'New TypenameTest01',
'color': 'GREEN',
}

validation_query = r"""
query {
__typename
other__Foo(filter: {select: {eq: "New TypenameTest01"}}) {
__typename
select
color
}
}
"""

self.assert_graphql_query_result(r"""
mutation insert_other__Foo {
__typename
insert_other__Foo(
data: [{
select: "New TypenameTest01",
color: GREEN,
}]
) {
__typename
select
color
}
}
""", {
"__typename": 'Mutation',
"insert_other__Foo": [data]
})

self.assert_graphql_query_result(validation_query, {
"__typename": 'Query',
"other__Foo": [data]
})

self.assert_graphql_query_result(r"""
mutation delete_other__Foo {
__typename
delete_other__Foo(
filter: {select: {eq: "New TypenameTest01"}}
) {
__typename
select
color
}
}
""", {
"__typename": 'Mutation',
"delete_other__Foo": [data]
})

# validate that the deletion worked
self.assert_graphql_query_result(validation_query, {
"other__Foo": []
})

def test_graphql_mutation_update_scalars_01(self):
orig_data = {
'p_bool': True,
Expand Down Expand Up @@ -4813,6 +4878,77 @@ def test_graphql_mutation_update_multiple_03(self):
}]
})

def test_graphql_mutation_update_typename_01(self):
# This tests the typename funcitonality after insertion.
# Issue #5985

self.assert_graphql_query_result(r"""
mutation insert_other__Foo {
__typename
insert_other__Foo(
data: [{
select: "Update TypenameTest01",
color: BLUE
}]
) {
__typename
select
color
}
}
""", {
"__typename": 'Mutation',
"insert_other__Foo": [{
'__typename': 'other__Foo_Type',
'select': 'Update TypenameTest01',
'color': 'BLUE',
}]
})

self.assert_graphql_query_result(r"""
mutation update_other__Foo {
__typename
update_other__Foo(
filter: {select: {eq: "Update TypenameTest01"}}
data: {
color: {set: RED}
}
) {
__typename
select
color
}
}
""", {
"__typename": 'Mutation',
"update_other__Foo": [{
'__typename': 'other__Foo_Type',
'select': 'Update TypenameTest01',
'color': 'RED',
}]
})

# clean up
self.assert_graphql_query_result(r"""
mutation delete_other__Foo {
__typename
delete_other__Foo(
filter: {select: {eq: "Update TypenameTest01"}}
) {
__typename
select
color
}
}
""", {
"__typename": 'Mutation',
"delete_other__Foo": [{
'__typename': 'other__Foo_Type',
'select': 'Update TypenameTest01',
'color': 'RED',
}]
})

def test_graphql_mutation_delete_alias_01(self):
self.assert_graphql_query_result(r"""
mutation insert_Setting {
Expand Down

0 comments on commit 6c55cf6

Please sign in to comment.