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

context.nodeModel.runQuery should return [] instead of null #25857

Closed
aaronadamsCA opened this issue Jul 19, 2020 · 5 comments · Fixed by #25885
Closed

context.nodeModel.runQuery should return [] instead of null #25857

aaronadamsCA opened this issue Jul 19, 2020 · 5 comments · Fixed by #25885
Labels
help wanted Issue with a clear description that the community can help with. topic: GraphQL Related to Gatsby's GraphQL layer type: bug An issue or pull request relating to a bug in Gatsby

Comments

@aaronadamsCA
Copy link
Contributor

Description

With a query that returns no results, context.nodeModel.runQuery returns Promise<null>, but the documentation specifies non-null type Promise<Node[]>.

I'd prefer to get back [] instead of null, because dealing with one type is easier than dealing with two, and because it's consistent with how the rest of Gatsby GraphQL works. If I pass the same filter to a static query and runQuery, I think both should return the same result, [].

Environment

  System:
    OS: Linux 4.19 Debian GNU/Linux 10 (buster) 10 (buster)
    CPU: (12) x64 Intel(R) Core(TM) i7-8850H CPU @ 2.60GHz
    Shell: 5.0.3 - /bin/bash
  Binaries:
    Node: 12.16.3 - /usr/local/bin/node
    Yarn: 1.22.4 - /usr/bin/yarn
    npm: 6.14.4 - /usr/local/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  npmPackages:
    gatsby: ^2.24.4 => 2.24.4
    gatsby-image: ^2.4.13 => 2.4.13
    gatsby-plugin-google-gtag: ^2.1.10 => 2.1.10
    gatsby-plugin-manifest: ^2.4.18 => 2.4.18
    gatsby-plugin-netlify: ^2.3.11 => 2.3.11
    gatsby-plugin-react-helmet: ^3.3.10 => 3.3.10
    gatsby-plugin-sharp: ^2.6.19 => 2.6.19
    gatsby-plugin-theme-ui: ^0.3.0 => 0.3.0
    gatsby-source-sanity: ^6.0.3 => 6.0.3
    gatsby-theme-style-guide: ^0.3.1 => 0.3.1
    gatsby-transformer-sharp: ^2.5.11 => 2.5.11
  npmGlobalPackages:
    gatsby-cli: 2.12.45
@aaronadamsCA aaronadamsCA added the type: bug An issue or pull request relating to a bug in Gatsby label Jul 19, 2020
@gatsbot gatsbot bot added the status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer label Jul 19, 2020
@sidharthachatterjee sidharthachatterjee added topic: GraphQL Related to Gatsby's GraphQL layer and removed status: triage needed Issue or pull request that need to be triaged and assigned to a reviewer labels Jul 20, 2020
@vladar
Copy link
Contributor

vladar commented Jul 20, 2020

Hi @aaronadamsCA !

Returning an empty array sounds correct to me too. But we explicitly test for null for some reason:

expect(resultSingular).toEqual([])
expect(resultMany).toEqual(null)

The test was added long ago in #11448 and the implementation was there even longer.

So, my main concern is that it is a breaking change. I think we should still change this behavior, but the question is if we should wait for the next major or can include it as a patch/minor. @pieh @pvdz any thoughts on this or maybe more context?

Anyways, the PR is welcome if you're up to it!

I think the change should be trivial somewhere here (just always return an empty array):

if (firstOnly) {
return []
}
return null
}

@vladar vladar added the help wanted Issue with a clear description that the community can help with. label Jul 20, 2020
@kartikcho
Copy link
Contributor

Can I provide a patch with these changes above @vladar, after it's confirmed and if it's alright with @aaronadamsCA?

@aaronadamsCA
Copy link
Contributor Author

@kartikcho Absolutely, I just didn't know where to start - though it looks like @vladar already found the spot. 👍

@vladar My guess is somebody just got the tests backwards? Because I'd probably expect null if I'd asked for an object vs. [] if I'd asked for an array. I'm guessing not toooo many people get this deep into the APIs.

@pvdz
Copy link
Contributor

pvdz commented Jul 29, 2020

(As mentioned in the PR, this is a subtle breaking change that I'd love to apply but will have to be done in a major bump because it's bound to break somebody's setup)

@github-actions
Copy link

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here.
If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!
As a friendly reminder: the best way to see this issue, or any other, fixed is to open a Pull Request. Check out gatsby.dev/contribute for more information about opening PRs, triaging issues, and contributing!

Thanks for being a part of the Gatsby community! 💪💜

@github-actions github-actions bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Aug 18, 2020
@vladar vladar added not stale and removed stale? Issue that may be closed soon due to the original author not responding any more. labels Aug 18, 2020
@LekoArts LekoArts removed the not stale label Mar 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Issue with a clear description that the community can help with. topic: GraphQL Related to Gatsby's GraphQL layer type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants