gregorip02 / restql

📦 A data resolution package for your Laravel models.
https://github.com/gregorip02/restql/wiki
GNU General Public License v3.0
129 stars 6 forks source link

Query Params don't work #15

Closed andreacivita closed 4 years ago

andreacivita commented 4 years ago

I was following all README.md instruction but, at this point restql#refactor-time, query params is not working.

Following the example, i have made a js script with axios, using this code:

axios.get('http://127.0.0.1:8000/api/restql', { params: { authors: { take: 1, select: 'name' } } }).then(({ data: authors }) => { // Do something... console.log(authors) });

However, instead of taking just 1 item, endpoint returns all item in table. Furthermore, select param is not working, because endpoint returns all columns in table (instead of taking only name).

It's first time I install this, so I could have made some mistakes, even if I followed step-by-step README instructions.

gregorip02 commented 4 years ago

Hi friend,

Remember that to start you must publish the default scheme of the package.

php artisan restql:schema

Then define a list of models allowed by you for auto-discovery in config/restql.php.

If this still doesn't work, please go to the documentation page and let me know if the problem persists.

gregorip02 commented 4 years ago

You can see the following example app https://github.com/gregorip02/restql-example

andreacivita commented 4 years ago

Hi friend!

Yes I have followed all README instructions. published config files and schema, but it hadn't worked (that's why I opened an issue).

However, I had cloned your example (https://github.com/gregorip02/restql-example) but it still doesn't work. I had installed all dependencies, migrated tables and try to make an API request with Axios. I had the same result of example app I made yesterday :(

const axios = require('axios').default;

axios.get('http://127.0.0.1:8000', {
  params: {
    books: {
        take: 1,
        select: 'title'
    }
  }
}).then(({ data: books }) => {
    // Do something...
    console.log(JSON.stringify(books))
});

The response:

{
  "data": {
    "books": [
      {
        "id": 1,
        "title": "a",
        "editorial": "a",
        "user_id": 1,
        "created_at": null,
        "updated_at": null
      },
      {
        "id": 2,
        "title": "a",
        "editorial": "a",
        "user_id": 1,
        "created_at": null,
        "updated_at": null
      }
    ]
  }
}

As you can see, using take or select is useless, because endpoint always return all data in table.

If it can helps, I'm using PHP 7.4

gregorip02 commented 4 years ago

🤔🤔🤔

I haven't noticed if axios sends the parameters as query strings in the url, can you try the following please?

<?php

// ...

return [
    /*
    |--------------------------------------------------------------------------
    | Parameter name sending in the request.
    |--------------------------------------------------------------------------
    |
    | If the value of this parameter is empty, RestQL will assume that the data
    | is sent in the body of the request.
    */

    'query_param' => env('RESTQL_PARAM_NAME', 'query'),

    // ...
];
const axios = require('axios').default;

axios.get('http://127.0.0.1:8000', {
  params: {
    query: {
      books: {
        take: 1,
        select: 'title'
      }
    }
  }
}).then(({ data: books }) => {
    // Do something...
    console.log(JSON.stringify(books))
});

I also using PHP 7.4 and it works, let me know if the problem persists.

gregorip02 commented 4 years ago

I updated, the README.md file in the example.

andreacivita commented 4 years ago

Nada man, unfortunately in this way response is always empty. Even if I remove query params (take or select).

andreacivita commented 4 years ago

I have made a fork of your repository. I have made changes like:

1) RestQL endpoint is now in api.php files (so, 127.0.0.1/api)

2) Root endpoint ( " / " in web.php) now displays a welcome page that made an axios call. As you can see, restql ignore all my params. I also tried to follow your suggestion about changing query_param (https://github.com/gregorip02/restql/issues/15#issuecomment-678802684) but i just receive an empty response.

I add a screenshot, it's a bit chaotic but you can see that, even if I added take: 5, select: 'name', endpoint response include ALL data ( it takes 15 elements and displays all data like editorial )

screenrest

I have made this repository so you can directly use it and check if something is wrong. Please remember it's a fork of yours, so except changing routes and adding axios code in welcome.blade.php, all configuration is the same. :)

gregorip02 commented 4 years ago

I have noticed that some javascripts clients do not send information in the body of the request when trying to access via GET. I updated the example repository with an http.js file that requires axios to do a test query.

const axios = require('axios');

const toBase64 = (string) => new Buffer.from(string).toString('base64');

axios.get('http://0.0.0.0:8000/api', {
    params: {
        query: toBase64(JSON.stringify({
            books: {
                take: 5,
                select: 'title'
            }
        }))
    }
}).then(({ data }) => {
    console.log(JSON.stringify(data));
});

Remember run yarn for install dependencies.

php artisan serve
node http.js
gregorip02 commented 4 years ago

I usually do unit tests and with a graphical client (insomnia) but I never did tests with javascripts clients. I will fix this problem for a minor version. Thank you very much for advising.

andreacivita commented 4 years ago

Your http.js works perfectly! If I could help in fixing problem, please let me know.

PS. Thank you too for your project 😁

gregorip02 commented 4 years ago

No problem, it's good that this is useful for you. I think this would be solved by correctly parsing the received JSON parameters. 🤔🤔🤔🤔🤔🤔

andreacivita commented 4 years ago

Ok. Please let me know if, in the future, you should need help. I close issue because now it works properly 😁😁