graphql / graphql/graphql-js

Support for deep input graphs

Open
#4,031 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
20.3k
Forks
2.1k
Avg merge
44m
Merged PRs (30d)
6

Description

I currently ran in the downstream project graphql-python/graphql-core in an recursion issue (string input graphs with depths around >200 fail).

It seems to be a construction problem in the base implementation (recursive design). And the token limiting is also useless because such deep graphs will fail roughly at the same level for the js implementation.

There is a simple fix: the "stack free" (not really stack free) generator pattern, you save the current state via generator and pushes nodes and new function calls at two stacks.

here an example (python, sry not ported yet):

````python
from functools import partial

def _foo(level=0, *, get_result):
if level > 100000:
yield level
return
print("1", level)
yield partial(_foo, level=level + 1)
print("2", get_result())
yield level

def foo():
result_stack = []

fn_stack = [_foo(get_result=result_stack.pop)]

while fn_stack:
cur_el = fn_stack[-1]
try:
next_el = next(cur_el)
if isinstance(next_el, partial):
fn_stack.append(next_el(get_result=result_stack.pop))
else:
result_stack.append(next_el)
except StopIteration:
fn_stack.pop()
return result_stack.pop()

print("final", foo())
````

Links:
- https://github.com/graphql-python/graphql-core/issues/216 Issue reported downstream
- https://github.com/devkral/graphene-protector Project, in which I use the generator pattern

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.