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

useLazyQuery void/no onCompleted #3597

Open
johnhult opened this issue Oct 15, 2019 · 2 comments
Open

useLazyQuery void/no onCompleted #3597

johnhult opened this issue Oct 15, 2019 · 2 comments

Comments

@johnhult
Copy link

johnhult commented Oct 15, 2019

Intended outcome:
I have a situation where I need to access passed parameters once I've ran useLazyQuery. Right now I can't await the result since the method returns void and I can't run the execute function with onCompleted as an option. This means I have to default to ApolloClient.query(). This issue is discussed here but I think one way to solve it that isn't discussed here is to be able to pass an onCompleted on the execute function instead, rather than having an onCompleted on the useLazyQuery or returning a Promise. I also know @FredyC originally intended for a Promise to be return but having an onComplete on the execute function would also work.

Actual outcome:
useLazyQuery returns a tuple with a function which, when used, returns void. Using onCompleted when defining the useLazyQuery means I can't access passed variables from a method when I need to.

How to reproduce the issue:
Something like the code below (not functioning code, just to give an idea of the usecase).

const [someQuery, {data, loading, error}] = useLazyQuery(SOME_QUERY, {variables: {foo: "bar"}});

const someMethod = (someVar) => {
  // Doesn't work
  let data = await someQuery();
  anotherMethod(data, someVar)

  // Doesn't work
  someQuery({onCompleted: data => {
    anotherMethod(data, someVar)
  }})
}

const anotherMethod = (someData, passedVar) => {
  // Do something
}

render() {
  <button onClick={() => someMethod("quaz")}></button>
}

Version
System:
OS: macOS Sierra 10.12.6
Binaries:
Node: 10.0.0 - ~/.nvm/versions/node/v10.0.0/bin/node
Yarn: 1.12.3 - /usr/local/bin/yarn
npm: 5.6.0 - ~/.nvm/versions/node/v10.0.0/bin/npm
Browsers:
Chrome: 77.0.3865.120
Firefox: 68.0.2
Safari: 12.1.2
npmPackages:
@apollo/react-hooks: ^3.1.0 => 3.1.2
@apollo/react-ssr: ^3.1.0 => 3.1.2
@apollo/react-testing: ^3.1.0 => 3.1.2
apollo-cache-inmemory: ^1.5.1 => 1.6.3
apollo-client: ^2.5.1 => 2.6.4
apollo-link: ^1.2.12 => 1.2.13
apollo-link-error: ^1.1.12 => 1.1.12
apollo-link-http: ^1.5.14 => 1.5.16
apollo-utilities: ^1.3.2 => 1.3.2

@danielkcz
Copy link
Contributor

Honestly, I don't think it's a good idea to approach this through the callback. A promise is more of a standardized approach for that. Besides, I don't think it's any easier to implement such callback instead of a Promise, the complexity is pretty much the same.

You can use a similar approach I have outlined here as a workaround, but instead of a promise, you can call your callback.

@johnhult
Copy link
Author

@FredyC
I agree that a promise is more standardized. It was merely a suggestion if it would've been an easier fix.

I'll look into your solution. Cheers!

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

No branches or pull requests

2 participants