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

Promise Namespacing Issue #60

Closed
jpserra opened this issue Aug 8, 2017 · 3 comments
Closed

Promise Namespacing Issue #60

jpserra opened this issue Aug 8, 2017 · 3 comments

Comments

@jpserra
Copy link

jpserra commented Aug 8, 2017

There is a Namespacing inconsistency in GraphQL::Batch::Loader between the Promises returned by load and load_many causing them to return different classes.

   def load(key)
      cache[cache_key(key)] ||= begin
        queue << key
        Promise.new.tap { |promise| promise.source = self } ### returns GraphQL::Batch::Promise
      end
    end

    def load_many(keys)
      ::Promise.all(keys.map { |key| load(key) })  ### returns Promise 
    end

Is there a particular reason for this? I can't seem to reason one.

@dylanahsmith
Copy link
Contributor

Your issue doesn't actually explain a problem. If you are doing something like .is_a?(GraphQL::Batch::Promise) in your application code, change it to .is_a?(Promise)

The reason for the difference is more of an implementation detail. The only difference between ::Promise and GraphQL::Batch::Promise is that GraphQL::Batch::Promise overrides defer so that we can use GraphQL::Batch::Executor.current.loading in tests to detect when an unbatched query is performed in a test.

@jpserra
Copy link
Author

jpserra commented Aug 8, 2017

That's exaclty my problem, but it didn't seem resonable to have to validate for both Promise and GraphQL::Batch::Promise since I want the code to work both when i call load and load_many seemlessly.

@dylanahsmith
Copy link
Contributor

dylanahsmith commented Aug 8, 2017

I don't know what is making it hard to write code that works for both Promise and GraphQL::Batch::Promise objects. Just don't reference GraphQL::Batch::Promise or do class equality checks.

Doing so will also allow Promise.all(promises) to just work, so you don't need to use the more verbose GraphQL::Batch::Promise.all(promises).

Ideally, I would like to just return Promise objects from both load and load_many in the future, deprecate GraphQL::Batch::Promise and alias it to ::Promise. However, this hasn't been a high priority, since it hasn't been causing problems for us that I know of.

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