Skip to content

Commit

Permalink
Fix custom function calls on the HTTP interface (#4998)
Browse files Browse the repository at this point in the history
The problem was that, if no global variables were passed,
we were passing in NULL for the global variable JSON object,
which causes strict functions to spuriously return NULL.

Fixes #4989
  • Loading branch information
msullivan committed Feb 10, 2023
1 parent 1209852 commit 576c8bc
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 16 deletions.
2 changes: 1 addition & 1 deletion edb/server/protocol/edgeql_ext.pyx
Expand Up @@ -125,7 +125,7 @@ async def handle_request(
db,
query,
variables=variables or {},
globals_=globals_ or {},
globals_=globals_,
)
except Exception as ex:
if debug.flags.server:
Expand Down
21 changes: 10 additions & 11 deletions edb/server/protocol/execute.pyx
Expand Up @@ -400,7 +400,7 @@ async def parse_execute_json(
query: str,
*,
variables: Mapping[str, Any] = immutables.Map(),
globals_: Mapping[str, Any] = immutables.Map(),
globals_: Optional[Mapping[str, Any]] = None,
output_format: compiler.OutputFormat = compiler.OutputFormat.JSON,
query_cache_enabled: Optional[bool] = None,
) -> bytes:
Expand Down Expand Up @@ -443,20 +443,19 @@ async def execute_json(
dbv: dbview.DatabaseConnectionView,
compiled: dbview.CompiledQuery,
variables: Mapping[str, Any] = immutables.Map(),
globals_: Mapping[str, Any] = immutables.Map(),
globals_: Optional[Mapping[str, Any]] = None,
*,
fe_conn: Optional[frontend.FrontendConnection] = None,
use_prep_stmt: bint = False,
) -> bytes:
if globals_:
dbv.set_globals(immutables.Map({
"__::__edb_json_globals__": config.SettingValue(
name="__::__edb_json_globals__",
value=_encode_json_value(globals_),
source='global',
scope=qltypes.ConfigScope.GLOBAL,
)
}))
dbv.set_globals(immutables.Map({
"__::__edb_json_globals__": config.SettingValue(
name="__::__edb_json_globals__",
value=_encode_json_value(globals_),
source='global',
scope=qltypes.ConfigScope.GLOBAL,
)
}))

qug = compiled.query_unit_group

Expand Down
14 changes: 10 additions & 4 deletions tests/schemas/graphql.esdl
Expand Up @@ -162,8 +162,14 @@ function get_glob() -> optional str using (global test_global_str);

alias GlobalTest := {
gstr := global test_global_str,
garray := global test_global_array,
gid := global test_global_id,
gdef := global test_global_def,
gdef2 := global test_global_def2,
garray := global test_global_array,
gid := global test_global_id,
gdef := global test_global_def,
gdef2 := global test_global_def2,
};

function id_func(s: str) -> str using (s);

alias FuncTest := {
fstr := id_func('test'),
};
20 changes: 20 additions & 0 deletions tests/test_http_edgeql.py
Expand Up @@ -326,3 +326,23 @@ def test_http_edgeql_query_globals_03(self):
globals={'default::test_global_str': 'foo'},
use_http_post=use_http_post,
)

def test_http_edgeql_query_globals_04(self):
Q = r'''select get_glob()'''

for use_http_post in [True, False]:
self.assert_edgeql_query_result(
Q,
[],
use_http_post=use_http_post,
)

def test_http_edgeql_query_func_01(self):
Q = r'''select id_func('foo')'''

for use_http_post in [True, False]:
self.assert_edgeql_query_result(
Q,
['foo'],
use_http_post=use_http_post,
)
14 changes: 14 additions & 0 deletions tests/test_http_graphql_query.py
Expand Up @@ -4136,6 +4136,20 @@ def test_graphql_globals_01(self):
use_http_post=use_http_post,
)

def test_graphql_func_01(self):
Q = r'''query { FuncTest { fstr } }'''

for use_http_post in [True, False]:
self.assert_graphql_query_result(
Q,
{
"FuncTest": [{
'fstr': 'test',
}]
},
use_http_post=use_http_post,
)


class TestGraphQLInit(tb.GraphQLTestCase):
"""Test GraphQL initialization on an empty database."""
Expand Down

0 comments on commit 576c8bc

Please sign in to comment.