Skip to content
This repository has been archived by the owner on Mar 8, 2023. It is now read-only.

Adds support for asynchronous executions via Promises #345

Merged
merged 3 commits into from
Jul 26, 2019

Conversation

acelot
Copy link
Contributor

@acelot acelot commented May 18, 2019

  • New promiseExecute method in Execution.php
  • Helpers for API (graphqlAsync(), processAsync)

@Jalle19
Copy link
Contributor

Jalle19 commented May 18, 2019

Would it be possible to use the async version all the time, and just change the graphql helper function to wait for the result? That way there would be a lot less code duplication.

@Jalle19 Jalle19 requested a review from crisu83 May 18, 2019 07:22
@Jalle19 Jalle19 added enhancement New feature or request execution Related to execution performance Related to perfomance public api Related to public API labels May 18, 2019
@acelot
Copy link
Contributor Author

acelot commented May 18, 2019

@Jalle19 I think so too, but did so for backward compatibility

@Jalle19
Copy link
Contributor

Jalle19 commented May 18, 2019

@Jalle19 I think so too, but did so for backward compatibility

You can think of the graphql() helper function and it's related functions as the public API, not the actual public interfaces etc.

@acelot
Copy link
Contributor Author

acelot commented May 18, 2019

It seems to me that synchronous and asynchronous methods should exist separately, since impossible to turn asynchronous promise into synchronous, because the result of promise comes through a callback.

@Jalle19
Copy link
Contributor

Jalle19 commented May 18, 2019

Isn't it possible to just add ->then() and return the result to turn it into synchronous?

@acelot
Copy link
Contributor Author

acelot commented May 18, 2019

Nope. In any case, will return Promise.

return GraphQL::promiseExecute(...)->then(function (ExecutionResult $result) {
    return $result;
})
$result = graphql(...);
var_export($result instanceof Promise); // true

@Jalle19
Copy link
Contributor

Jalle19 commented May 18, 2019

Isn't there some wait method that could be used?

@acelot
Copy link
Contributor Author

acelot commented May 19, 2019

All promises works in synchronous way without event loop. But thanks to the ReactPHP event loop PHP works in real asynchronous mode. Unfortunately I don't know how ReactPHP resolves promises.

@acelot acelot force-pushed the master branch 2 times, most recently from d709fd2 to b29ff69 Compare May 20, 2019 03:19
@acelot
Copy link
Contributor Author

acelot commented May 20, 2019

I have refactored the code. Now execute method in Execution always returns a Promise. I have moved promise resolving for synchronous calls into API method graphql. I also added a check, if a Event Loop is used (React PHP), an error is returned.

- Now `execute` method in `Execution.php` returns Promise
- Helpers for API (`graphqlAsync()`, `processAsync()`)
- Error messages when user trying to use non async methods in Event Loop
@acelot
Copy link
Contributor Author

acelot commented May 24, 2019

Any chance that this PR will be merged?

@Jalle19
Copy link
Contributor

Jalle19 commented May 27, 2019

I'll try to find some time to test this out this week, the latest changes look very promising (no pun intended)!

@Jalle19
Copy link
Contributor

Jalle19 commented Jun 1, 2019

@acelot I fixed the tests, for some reason Travis CI doesn't run on this pull request. What's the next step in getting deferred resolving to work?

@acelot
Copy link
Contributor Author

acelot commented Jun 2, 2019

https://github.com/lordthorzonus/php-dataloader works for me very well. But my project based on ReactPHP. I have not tested data loader in regular PHP environment.

@Jalle19
Copy link
Contributor

Jalle19 commented Jun 4, 2019

I still need to test this in a non-asynchronous environment before I dare merge this, I'm not super familiar with part of the code. I have to think if we really want to break the ExecutionInterface signature even though I said it was fine earlier.

@crisu83
Copy link
Contributor

crisu83 commented Jul 26, 2019

Hello @acelot,

First of all, I'd like to thank you for all the work you've put into this. I'm going through a divorce so I haven't had any energy to focus on this project for quite some time now, but now I'm back at work after a long vacation so I started working on a version 1.2 yesterday.

I agree that separate methods are the way to go, and we definitely want to hand over the promise from the public API, instead of resolving it before returning the result. This gives the consumer the freedom to do whatever they need with that promise.

I will now go through the changes and add my comments. After that, if all is good and no more changes are required we can merge this. Thanks once again for your efforts! 😊

@crisu83
Copy link
Contributor

crisu83 commented Jul 26, 2019

We had some problems with deferred resolvers (#336). Any change you've had any success with them @acelot? The problem was that the promises got resolved too early, instead of using a dictionary, delaying the resolution and resolving them all at once. Did you find a way to fix the deferred resolvers? Thanks in advance.

Copy link
Contributor

@crisu83 crisu83 left a comment

Choose a reason for hiding this comment

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

I added some comments, but they mostly nit-picking. Change them if you feel like it, I can also fix them afterwards if you're not up for it. 🙂

src/api.php Outdated Show resolved Hide resolved
src/api.php Outdated Show resolved Hide resolved
src/api.php Outdated Show resolved Hide resolved
src/api.php Outdated Show resolved Hide resolved
src/api.php Show resolved Hide resolved
src/api.php Show resolved Hide resolved
src/api.php Show resolved Hide resolved
src/api.php Show resolved Hide resolved
@crisu83 crisu83 marked this pull request as ready for review July 26, 2019 07:24
@crisu83 crisu83 self-assigned this Jul 26, 2019
@crisu83 crisu83 added this to the Version 1.2 milestone Jul 26, 2019
@crisu83 crisu83 mentioned this pull request Jul 26, 2019
@crisu83 crisu83 merged commit c82c400 into digiaonline:master Jul 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request execution Related to execution performance Related to perfomance public api Related to public API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants