acquia / waterwheel.js

A generic JavaScript helper library to query and manipulate Drupal 8 via core REST and JSON API
https://github.com/acquia/waterwheel.js
233 stars 26 forks source link

JSONAPI - include and fields format & outputs #61

Closed yann-yinn closed 7 years ago

yann-yinn commented 7 years ago

Hello ! First, Thanks a lot for your great work on waterwheel !

I open an issue because i was a little bit confused by "params" object format from json api get method

Here are my "params" object :

    const params = {
      sort: '-created',
      page: { limit: 4 },
      include: ['image', 'image.thumbnail'], // ARRAY HERE
      fields: {
        recipes: ['title', 'difficulty', 'image'], // & HERE
        images: ['name', 'thumbnail'], 
        files: ['filename']
      }
    }

Here is the resulting url : http://dev-contentacms.pantheonsite.io/api/recipes?_format=api_json&fields%5Bfiles%5D=filename&fields%5Bimages%5D=name&fields%5Bimages%5D=thumbnail&fields%5Brecipes%5D=difficulty&fields%5Brecipes%5D=image&fields%5Brecipes%5D=title&include=image&include=image.thumbnail&page%5Blimit%5D=4&sort=-created

In a more readable way :

capture d ecran 2017-07-06 a 21 47 11

I expected it to works but :

Expected output for the object i passed should be, according to JSON API format : include=image,image.thumbnail

And for the fields :

fields[images]=name,thumbnail

So i guess when using waterwheel with JSON API, I MUST always pass params object this way (using strings and not arrays for fields and include) ? Am i correct ? Maybe we could add a full working example in the documentation to have an example ?

    const params = {
      sort: '-created',
      page: { limit: 4 },
      include: "image,image.thumbnail",
      fields: {
        recipes: "title,difficulty,image",
        images: "name, thumbnail",
        files: "filename"
      }
    }

Or is it JSON API module on PHP side that is supposed to understand correctly arrays of the first request ?

And just in case as an information, i was using this small module to write params as full objects and arrays, because i find it easier to manipulate arrays for "fields" and "include" keys than "concatened strings separated by commas" when i create my query dynamically

https://www.npmjs.com/package/d8-jsonapi-querystring

yann-yinn commented 7 years ago

Here is a working example params object, will make a PR to add this as an example in the README for JSON API

    const options = {
      page: { limit },
      filter: {
        isPromoted: {
          path:'isPromoted',
          value: 1
        }
      },
      include: "image,image.thumbnail",
      fields: {
        recipes: "title,difficulty,image",
        images: "name,thumbnail",
        files: "filename"
      },
      sort: '-created'
    }