Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix single non zeroth index resolving #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

cam-mh
Copy link

@cam-mh cam-mh commented Jun 25, 2024

There is a bug on this line. I believe it should be return completed_results[index].then(on_one_resolved) instead of return completed_results[0].then(on_one_resolved)

It looks like that code is optimizing for when there is only a single Promise being returned and tries to execute the 0th index of "awaitable_promise"s. However it is possible the awaitable index may not be the 0ths index, it could be the 1st.

Here is some sample code that reproduces the issue.

from graphene import List, ObjectType, Schema, String
from graphene.test import Client
from graphql_core_promise import PromiseExecutionContext
from promise import Promise
from promise.dataloader import DataLoader


class SomeLoader(DataLoader):
    def batch_load_fn(self, keys):
        return Promise.resolve([f"{key=} resolved!" for key in keys])


some_loader = SomeLoader()


class Query(ObjectType):
    some_field = List(String)

    def resolve_some_field(root, _):
        return ["Some string", some_loader.load(10), "Another string"]


schema = Schema(query=Query)


client = Client(schema=schema, execution_context_class=PromiseExecutionContext)

foo = client.execute("query myQuery { someField }")
print(foo)

Before:

{'errors': [{'message': "'str' object has no attribute 'then'", 'locations': [{'line': 1, 'column': 17}], 'path': ['someField']}], 'data': {'someField': None}}

After:

{'data': {'someField': ['Some string', 'key=10 resolved!', 'Another string']}}

@@ -460,7 +460,7 @@ def on_one_resolved(result):

# If there is only one index, avoid the overhead of parallelization.
index = awaitable_indices[0]
return completed_results[0].then(on_one_resolved)
return completed_results[index].then(on_one_resolved)
Copy link

@tony tony Aug 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fellowapp @sciyoshi This fixed an issue for me.

Traceback
╭────────────────────────── Traceback (most recent call last) ───────────────────────────╮                                                                               │
│ promise.py:213 in execute_field                                                        │
│                                                                                        │
│   210 │   │   │   │                                                                    │
│   211 │   │   │   │   return await_result()                                            │
│   212 │   │   │                                                                        │
│ ❱ 213 │   │   │   completed = self.complete_value(                                     │
│   214 │   │   │   │   return_type, field_nodes, info, path, result                     │
│   215 │   │   │   )                                                                    │
│   216 │   │   │   if self.is_promise(completed):                                       │
│                                                                                        │
│ site-packages/graphql/execution/execute.py:612 in complete_value                       │
│                                                                                        │
│    609 │   │   # If field type is NonNull, complete for inner type, and throw field er │
│    610 │   │   # result is null.                                                       │
│    611 │   │   if is_non_null_type(return_type):                                       │
│ ❱  612 │   │   │   completed = self.complete_value(                                    │
│    613 │   │   │   │   cast(GraphQLNonNull, return_type).of_type,                      │
│    614 │   │   │   │   field_nodes,                                                    │
│    615 │   │   │   │   info,                                                           │
│                                                                                        │
│ site-packages/graphql/execution/execute.py:632 in complete_value                       │
│                                                                                        │
│    629 │   │                                                                           │
│    630 │   │   # If field type is List, complete each item in the list with inner type │
│    631 │   │   if is_list_type(return_type):                                           │
│ ❱  632 │   │   │   return self.complete_list_value(                                    │
│    633 │   │   │   │   cast(GraphQLList, return_type), field_nodes, info, path, result │
│    634 │   │   │   )                                                                   │
│    635                                                                                 │
│                                                                                        │
│ promise.py:484 in complete_list_value                                                  │
│                                                                                        │
│   481 │   │   │   │   completed_results[index] for index in awaitable_indices          │
│   482 │   │   │   ]).then(on_all_resolve)                                              │
│   483 │   │                                                                            │
│ ❱ 484 │   │   res = get_completed_results()                                            │
│   485 │   │   return res                                                               │
│   486                                                                                  │
│                                                                                        │
│ promise.py:473 in get_completed_results                                                │
│                                                                                        │
│   470 │   │   │   │                                                                    │
│   471 │   │   │   │   # If there is only one index, avoid the overhead of parallelizat │
│   472 │   │   │   │   index = awaitable_indices[0]                                     │
│ ❱ 473 │   │   │   │   return completed_results[0].then(on_one_resolved)                │
│   474 │   │   │                                                                        │
│   475 │   │   │   def on_all_resolve(results):                                         │
│   476 │   │   │   │   for index, result in zip(awaitable_indices, results, strict=Fals │
╰────────────────────────────────────────────────────────────────────────────────────────╯

Unfortunately, due to the complexity of the schemas I am working with, I don't have an MVP for a test or recreation. If there's a critical need for it please ping me.

@Yobi-Song
Copy link

I'm having the same issue.
I wonder if #5 will be merged?
image

@simon-alvalabs
Copy link

Would be great to have this merged 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants