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

Error in optimization code when there is a single promise not in the 0th index in the list of fields to be resolved. #4

Open
cam-mh opened this issue Jun 25, 2024 · 1 comment

Comments

@cam-mh
Copy link

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']}}
@cam-mh cam-mh changed the title Error in optimization code when there is only a single promise in the list of fields to be resolved. Error in optimization code when there is a single promise not in the 0th index in the list of fields to be resolved. Jun 25, 2024
@tony
Copy link

tony commented Aug 4, 2024

The fix at #5 handled this 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 │
╰────────────────────────────────────────────────────────────────────────────────────────╯

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

No branches or pull requests

2 participants