Way to specify resource's fields list in RESTful API request

后端 未结 2 1443
滥情空心
滥情空心 2020-12-25 12:08

I have RESTful API within a web-service with some kind of resources like users, posts and so on. When I make request for list of posts (GET /posts), I want to retrieve array

2条回答
  •  独厮守ぢ
    2020-12-25 12:55

    Have been doing research into this as well and was pointed towards Facebook's GraphQL as an alternative to requesting a restful api with the fields wanted. It is still in the very early stages but seems very promising.

    https://facebook.github.io/react/blog/2015/05/01/graphql-introduction.html

    EDIT: Reproduced from the URL:

    A GraphQL query is a string interpreted by a server that returns data in a specified format. Here is an example query:

    {
      user(id: 3500401) {
        id,
        name,
        isViewerFriend,
        profilePicture(size: 50)  {
          uri,
          width,
          height
        }
      }
    }
    

    (Note: this syntax is slightly different from previous GraphQL examples. We've recently been making improvements to the language.)

    And here is the response to that query.

    {
      "user" : {
        "id": 3500401,
        "name": "Jing Chen",
        "isViewerFriend": true,
        "profilePicture": {
          "uri": "http://someurl.cdn/pic.jpg",
          "width": 50,
          "height": 50
        }
      }
    }
    

提交回复
热议问题