dunglas / vulcain

🔨 Fast and idiomatic client-driven REST APIs.
https://vulcain.rocks
GNU Affero General Public License v3.0
3.51k stars 106 forks source link

Passing query params to sub-resources #52

Closed polc closed 4 years ago

polc commented 4 years ago

There is a mention about using GraphQL as a query language for Vulcain here : https://github.com/dunglas/vulcain/blob/master/docs/graphql.md#using-graphql-as-query-language-for-vulcain

I've been playing on a toyproject about Vulcain and I'm wondering if all GraphQL queries can be converted to a HTTP Request using Vulcain, and especially, deep arguments like the following :

Given this query

{
  persons(orderBy: name_ASC) {
    id
    name,
    starships(type: destroyer) {
      name,
      weapons(category: nuclear) {
        name
      }
    }
  }
}

I believe it may be translated to the following requests :

GET /persons?orderBy=name
Preload: /*/starships/*/weapons

[
  {
    "id": "/persons/1",
    "name": "Person 1",
    "starships": "/starships?person=/persons/1&type=destroyer"
  } 
]
GET /starships?person=/persons/1&type=destroyer

[
  {
    "id": "/starships/1",
    "name": "Starship 1",
    "weapons": "/weapons?starship=/startships/1&category=nuclear"
  }
]
GET /weapons?starship=/startships/1&category=nuclear

[
  {
    "id": "/weapons/1",
    "name": "Weapon 1",
  }
]

Now It's easy to change weapons(category: nuclear) to weapons(category: explosives) in GraphQL, but I'm not sure how to do it with Vulcain. Do you have any insight about this problem?

soyuka commented 4 years ago

Just use /persons?orderBy=name vulcain acts as a reverse proxy it'll work

polc commented 4 years ago

My problem is about passing params to deep resources, I realized the issue was not very clear so I updated the example.

soyuka commented 4 years ago

I don't see the issue it should work just like this

polc commented 4 years ago

Sorry the previous requests were missing the query params I wanted to show :

GET /persons?orderBy=name&starships[type]=destroyer&starships.weapons[category]=nuclear
Preload: /*/starships/*/weapons

[
  {
    "id": "/persons/1",
    "name": "Person 1",
    "starships": "/starships?person=/persons/1&type=destroyer&weapons[category]=nuclear"
  } 
]
GET /starships?person=/persons/1&type=destroyer&weapons[category]=nuclear

[
  {
    "id": "/starships/1",
    "name": "Starship 1",
    "weapons": "/weapons?starship=/startships/1&category=nuclear"
  }
]
GET /weapons?starship=/startships/1&category=nuclear

[
  {
    "id": "/weapons/1",
    "name": "Weapon 1",
  }
]

Here, to pass query params deeper, I add them to the root resource. Even if it's not a big problem, I see 2 minor inconveniences :

Is this the recommended way of passing query params?