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

fix(gatsby): Resolve all fields before querying with Sift #11149

Merged
merged 2 commits into from
Jan 24, 2019

Conversation

stefanprobst
Copy link
Contributor

Fix extractFieldsToSift silently omitting fields that need to be resolved before querying. See:

const _ = require('lodash')
const diff = require('jest-diff')

const filter = {
  foo: { eq: true },
  bar: {
    foo: { eq: true },
    bar: {
      foo: { eq: true },
      bar: {
        foo: { eq: true },
      },
    },
  },
  baz: { eq: true },
  qux: {
    foo: { eq: true },
  },
}

const expected = {
  foo: true,
  bar: {
    foo: true,
    bar: {
      foo: true,
      bar: {
        foo: true,
      },
    },
  },
  baz: true,
  qux: {
    foo: true,
  },
}

function extractFieldsToSift(prekey, key, preobj, obj, val) {
  if (_.isPlainObject(val)) {
    _.forEach(val, (v, k) => {
      if (k === `elemMatch`) {
        extractFieldsToSift(prekey, key, preobj, obj, v)
        return
      }
      preobj[prekey] = obj
      extractFieldsToSift(key, k, obj, {}, v)
    })
  } else {
    preobj[prekey] = true
  }
}

const fieldsToSift = {}
_.each(filter, (v, k) => {
  extractFieldsToSift(``, k, {}, fieldsToSift, v)
})

console.log(diff(fieldsToSift, expected))

@stefanprobst stefanprobst requested a review from a team as a code owner January 18, 2019 18:47
@stefanprobst
Copy link
Contributor Author

Oh, Node 6 does not have Object.entries. Will fix tomorrow.

@freiksenet freiksenet merged commit d432e2a into gatsbyjs:master Jan 24, 2019
@freiksenet
Copy link
Contributor

Thanks!

freiksenet pushed a commit to stefanprobst/gatsby that referenced this pull request Jan 25, 2019
freiksenet pushed a commit that referenced this pull request Jan 25, 2019
@stefanprobst stefanprobst deleted the fix-extract-fields-to-sift branch July 8, 2019 15:01
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

Successfully merging this pull request may close these issues.

2 participants