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

Filtering on Contains in Array - JSON content #4799

Closed
thekevinbrown opened this issue Apr 2, 2018 · 10 comments
Closed

Filtering on Contains in Array - JSON content #4799

thekevinbrown opened this issue Apr 2, 2018 · 10 comments
Labels
type: bug An issue or pull request relating to a bug in Gatsby

Comments

@thekevinbrown
Copy link
Contributor

thekevinbrown commented Apr 2, 2018

Description

I have tags arrays in my content, and I want to be able to fetch by those tags.

Steps to reproduce

Use JSON content like this:

{
  tags: ['a', 'b']
}

Then try to fetch for content which contains tag 'a' with this query:

query testQuery($tag: String) {
	allContentJson(filter: { tags: { in: [$tag]}}) {
		totalCount
	}
}

Expected result

I would expect this to work as per this example and this issue:

Example:

filter: { frontmatter: { tags: { in: [$tag] }, draft: { ne: true } } }

Issue: #1645

Actual result

{
  "errors": [
    {
      "message": "Variable \"$tag\" of type \"String\" used in position expecting type \"contentJsonConnectionTopicsInputObject_2\".",
      "locations": [
        {
          "line": 1,
          "column": 56
        },
        {
          "line": 2,
          "column": 67
        }
      ]
    }
  ]
}

The only way I've found to get the query to run is this:

query testQuery($tag: String) {
	allContentJson(filter: { tags: { in: {tag: { eq: $tag}}}}) {
		totalCount
	}
}

But I always end up with null as the result.

Environment

  • Gatsby version (npm list gatsby): gatsby@1.9.243
  • gatsby-cli version (gatsby --version): Not installed (I use package.json commands)
  • Node.js version: v8.9.4
  • Operating System: OS X 10.13.3

File contents (if changed):

(I'll provide an example project which reproduces this if needed.)

@m4rrc0
Copy link
Contributor

m4rrc0 commented Apr 4, 2018

+1
I also tried using the brackets AND the eq operator like so:

query testQuery($tag: String) {
	allContentJson(filter: { tags: { in: [{tag: { eq: $tag}}]}}) {
		totalCount
	}
}

because when exploring QraphiQL it looks like that is what is needed but it always returns null.
I was starting to wonder if there was a problem with my source plugin but maybe not then...

Gatsby version: 1.9.233

@btk
Copy link
Contributor

btk commented Apr 15, 2018

Just stumbled upon this.

For my case, this was the syntax that I expected to work, similar to @MarcCoet

      filter: { language: { eq: $language }, category: { in: [{slug: {eq: $slug} }] } } }

But seems like this structure is not supported. Will have to pull out data without filtering and filter it in javascript, I guess.

@SarathSantoshDamaraju
Copy link

Facing same problem,
In my case i want to generate pages based on tags that will list the tagged items in each of the pages. But as the query is returning null, i am unable to generate pages as well

@rcaracaus
Copy link
Contributor

rcaracaus commented Jun 26, 2018

I can't seem to filter on an abject nested inside an array, either that or the syntax is too complicated, getting null as well

@pieh
Copy link
Contributor

pieh commented Jun 26, 2018

There is a bug in how schema is constructed for array of objects - inside in operator Gatsby shouldn't add more operators in "leafs"

So in example from this issue:

filter: { language: { eq: $language }, category: { in: [{slug: {eq: $slug} }] } } }

schema shouldn't add that eq there - it should be:

filter: { language: { eq: $language }, category: { in: [{slug: $slug }] } } }

If anyone is interested in fixing this - https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/schema/infer-graphql-input-fields.js and https://github.com/gatsbyjs/gatsby/blob/master/packages/gatsby/src/schema/infer-graphql-input-fields-from-fields.js is where schema is constructed for inputs

@pieh pieh added the type: bug An issue or pull request relating to a bug in Gatsby label Jun 26, 2018
@Mathspy
Copy link

Mathspy commented Jun 30, 2018

Is this bug affecting v2? I'd look into it if it's affecting both versions

@pieh
Copy link
Contributor

pieh commented Jun 30, 2018

This is affecting both v1 and v2 (there were no schema related changes in v2)

@ErisDS
Copy link
Contributor

ErisDS commented Aug 17, 2018

I also ran into this bug, but I found this PR: #6315 which appears to fix it in gatsby@2.0.0-beta.22.

I'm now able to do queries with elemMatch rather than in, E.g.:

filter: {category: { elemMatch: {slug: {eq: $slug } } } } }

@ErisDS
Copy link
Contributor

ErisDS commented Sep 18, 2018

Argh I definitely had this working, now elemMatch is an unknown field. Did something change?

Nevermind, I'd downgraded my version of gatsby and forgotten. It is definitely working.

@pieh
Copy link
Contributor

pieh commented Oct 16, 2018

This was fixed in gatsby v2 by, so I'm closing this issue

@pieh pieh closed this as completed Oct 16, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

8 participants