paypal / mocca

Mocca is a GraphQL client for JVM languages with the goal of being easy to use, flexible and modular.
MIT License
15 stars 14 forks source link

Add support for GraphQL Field Alias for GraphQL queries #26

Open fabiocarvalho777 opened 3 years ago

fabiocarvalho777 commented 3 years ago

Introduction

Mocca should support GraphQL fields alias for queries. The biggest advantage of this feature is to support multiple GraphQL queries inside a single request. That is only possible when field alias are used, so the data set in the response can differentiate the returned value for each query.

Supported use cases

All use cases described in the next sections should be supported as part of the scope of this issue.

Using Field Alias to send multiple queries inside the same request

Notice that the biggest advantage of this feature is to support multiple GraphQL queries inside a single request. That is only possible when field alias are used, so the data set in the response can differentiate the returned value for each query.

Request:

{
  zuck: user(id: 4) {
    id
    name
  }
  jdoe: user(id: 5) {
    id
    name
  }
}

Response:

{
  "zuck": {
    "id": 4,
    "name": "Mark Zuckerberg"
  }
  "jdoe": {
    "id": 5,
    "name": "John Doe"
  }
}

Using Field Alias to all but one query

When sending multiple queries inside the same request (using file alias), the user can leave one, and only one, query still named with the default query name, as seen in the example below.

Request:

{
  user(id: 4) {
    id
    name
  }
  jdoe: user(id: 5) {
    id
    name
  }
}

Response:

{
  "user": {
    "id": 4,
    "name": "Mark Zuckerberg"
  }
  "jdoe": {
    "id": 5,
    "name": "John Doe"
  }
}

Using Field Alias to send only one query

Even when the user just wants to send one single query (as in the example below), still Mocca should supports field alias.

Request:

{
  zuck: user(id: 4) {
    id
    name
  }
}

Response:

{
  "jdoe": {
    "id": 5,
    "name": "John Doe"
  }
}

Using Field Alias with mutations

As part of this issue, research if GraphQL also supports Field Alias for mutations (allowing multiple mutations in the same request) and even hybrid requests (containing multiple mutations and queries making use of field alias). If GraphQL does support one or both of these scenarios, then they also have to be implemented as part of this issue.

Use cases NOT in the scope of this story

Using Field Alias inside of Selection Sets

In the supported use cases described previously, GraphQL field alias are used only to the queries (as queries are also considered fields in GraphQL). However, it is important to notice that in GraphQL field alias can also be applied to the fields set inside of the selection sets (as seen example below). This particular use case is NOT covered in this issue, it will covered in a separated issue.

{
  user(id: 4) {
    id
    name
    smallPic: profilePic(size: 64)
    bigPic: profilePic(size: 1024)
  }
}
{
  "user": {
    "id": 4,
    "name": "Mark Zuckerberg",
    "smallPic": "https://cdn.site.io/pic-4-64.jpg",
    "bigPic": "https://cdn.site.io/pic-4-1024.jpg"
  }
}