Skip to content

Commit 8518a89

Browse files
committed
fix: Setup GQLType.doctype resolver manually
1 parent 974538c commit 8518a89

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

frappe_graphql/utils/cursor_pagination.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def get_data(self, doctype, filters, sorting_fields, sort_dir, limit):
152152

153153
def get_fields_to_fetch(self, doctype, filters, sorting_fields):
154154
fieldnames = get_allowed_fieldnames_for_doctype(doctype)
155-
return list(set(fieldnames + [f"SUBSTR(\".{doctype}\", 2) as doctype"] + sorting_fields))
155+
return list(set(fieldnames + sorting_fields))
156156

157157
def get_sort_args(self, sorting_input=None):
158158
sort_dir = self.default_sorting_direction if self.default_sorting_direction in (

frappe_graphql/utils/permissions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def get_allowed_fieldnames_for_doctype(doctype: str, parent_doctype: str = None)
88
Gets a list of fieldnames that's allowed for the current User to
99
read on the specified doctype. This includes default_fields
1010
"""
11-
fieldnames = list(default_fields) + ["\"{}\" as `doctype`".format(doctype)]
11+
fieldnames = list(default_fields)
1212
fieldnames.remove("doctype")
1313

1414
meta = frappe.get_meta(doctype)

frappe_graphql/utils/resolver/__init__.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from graphql import GraphQLSchema, GraphQLType
1+
from graphql import GraphQLSchema, GraphQLType, GraphQLResolveInfo
22

33
import frappe
44
from frappe.model.meta import Meta
@@ -24,6 +24,7 @@ def setup_default_resolvers(schema: GraphQLSchema):
2424
meta = frappe.get_meta(dt)
2525

2626
setup_frappe_df(meta, gql_type)
27+
setup_doctype_resolver(meta, gql_type)
2728
setup_link_field_resolvers(meta, gql_type)
2829
setup_select_field_resolvers(meta, gql_type)
2930
setup_child_table_resolvers(meta, gql_type)
@@ -48,5 +49,20 @@ def setup_frappe_df(meta: Meta, gql_type: GraphQLType):
4849
gql_type.fields[df.fieldname].frappe_df = df
4950

5051

52+
def setup_doctype_resolver(meta: Meta, gql_type: GraphQLType):
53+
"""
54+
Sets custom resolver to BaseDocument.doctype field
55+
"""
56+
if "doctype" not in gql_type.fields:
57+
return
58+
59+
gql_type.fields["doctype"].resolve = _doctype_resolver
60+
61+
62+
def _doctype_resolver(obj, info: GraphQLResolveInfo, **kwargs):
63+
dt = get_singular_doctype(info.parent_type.name)
64+
return dt
65+
66+
5167
def setup_select_field_resolvers(meta: Meta, gql_type: GraphQLType):
5268
pass

frappe_graphql/utils/tests/test_permissions.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ def test_admin_on_user(self):
2424
fieldnames,
2525
[x.fieldname for x in meta.fields if x.fieldtype not in no_value_fields]
2626
+ [x for x in default_fields if x != "doctype"]
27-
+ ["\"{}\" as doctype".format(meta.name)]
2827
)
2928

3029
def test_permlevels_on_user(self):
@@ -42,7 +41,6 @@ def test_permlevels_on_user(self):
4241
[x.fieldname for x in user_meta.fields
4342
if x.permlevel == 1 and x.fieldtype not in no_value_fields]
4443
+ [x for x in default_fields if x != "doctype"]
45-
+ ["\"{}\" as doctype".format(user_meta.name)]
4644
)
4745

4846
# Clear meta_cache for User doctype
@@ -55,7 +53,6 @@ def test_on_child_doctype(self):
5553
fieldnames,
5654
[x.fieldname for x in meta.fields if x.fieldtype not in no_value_fields]
5755
+ [x for x in default_fields if x != "doctype"]
58-
+ ["\"{}\" as doctype".format(meta.name)]
5956
)
6057

6158
def test_on_child_doctype_with_no_parent_doctype(self):

0 commit comments

Comments
 (0)