pasdo501 / gatsby-source-woocommerce

Gatsy Source Plugin for WooCommerce
34 stars 14 forks source link

[Feature Questt] Filter fields with the _fields query parameter #26

Open cleitonper opened 4 years ago

cleitonper commented 4 years ago

Filter resources with the Wordpress REST API _fields query parameter Would be nice to have a configuration in the plugin to filter resources (products, categories, etc) coming from the API.

In my case, for example, each product has dozens of fields, but my frontend in gatsby don't need all of these fields. Requesting only the fields that we need will reduce the build time significantly.

Why this feature is needed? To reduce build times.

How to handle it?

plugins:[
  {       
    resolve: '@pasdo501/gatsby-source-woocommerce',
    options: {
      // ... other options
      resources: [
         { name: 'products', fields: ['id', 'name', 'slug', 'price'] }
         { name: 'products/categories', fields: ['id', 'name', 'slug'] }
    }
  }
]

In this case the current option called fields would be renamed to resources, and each resource must have a name and a list of fields. If all the fields are needed in the gatsby we can pass a string with the all value to the fields option.

pasdo501 commented 4 years ago

Hi there,

Sorry for the delay. Nice idea, I hadn't thought of using the _fields query parameter, and also good way of handling it with the options using a resources field - would allow for backwards compatibility. I can look into implementing this, it looks like it'd actually be fairly quick if I got around to it, although I have less free time at the moment (and am not actively working with this plugin currently). Otherwise, feel free to open a PR!

pasdo501 commented 4 years ago

Hi again,

I actually ended up having a chance to have a brief look at this. I've created a new branch with the filter fields feature - using the same config layout as suggested by you.

Currently, if the resources config option is set, it'll ignore the fields and use that instead. There's not really checking to make sure the config layout is correct at the moment, but have a look if you get a chance, to see if it reduces the build time much for you / if you see major issues with it (I wouldn't be surprised, I rushed through it a little bit)