Search code examples
shopware6shopware6-api

Where to POST Shopware 6 search criteria to?


Referring to these two docs:

https://developer.shopware.com/docs/guides/integrations-api/general-concepts/search-criteria

Generally, we refer to the endpoints that use POST method and receive the criteria as a JSON object as search criteria.

https://shopware.stoplight.io/docs/admin-api/eecbb397dce52-list-with-basic-information-of-product-resources

As you can see in the second link, there is no API endpoint to POST to and get data in return.

I tried the following (GET) request:

http://shopware65.local/api/product?query=%7B%22limit%22%3A10%2C%22associations%22%3A%7B%22manufacturer%22%3A%5B%5D%7D%2C%22includes%22%3A%7B%22product%22%3A%5B%22calculatedPrice%22%2C%22cover%22%2C%22id%22%2C%22manufacturer%22%2C%22ean%22%5D%2C%22product_media%22%3A%5B%22media%22%5D%7D%7D

JSON decoded:

{"limit":10,"associations":{"manufacturer":[]},"includes":{"product":["calculatedPrice","cover","id","manufacturer","ean"],"product_media":["media"]}}

The parameter limit is set to 10 and the fields to return are limited by includes but the API ignores all of that.

What do I post to the links in the API docs or, where is the API they are talking about in the docs where shall send POST to read data?

Does this stuff have anything to do with the SwagQL library? Do we need to setup a SwagQL server, because in the API docs they say the query field is for SwagQL queries (does it make sense to talk to a REST API in GraphQL language? It doesn't)?


Solution

  • You can use those entity-specific endpoints for simplified CRUD operations. You could also use the list with all the criteria properties written as query parameters. So for instance you could still have multiple filters in the form of ?filter[]=...&filter[]=....

    However what you would probably prefer is the generic /api/search/{entity} search endpoint, e.g. for searching products it would be /api/search/product. As the name suggests, this is specifically meant to be used for more complex search queries. This one uses the POST method and as such you can provide a request body with the criteria as JSON-encoded payload. If you go with this endpoint, you will furthermore likely want to set the Accept header to application/json to get a more comprehensive output.