Are Aliases really necessary for GraphQL?
See original GitHub issueHello,
So personally, I don’t see the use case for the aliases in the GraphQL if I request multiple businesses. There might be other use cases out there that are not for me and I get that. But currently, the aliases just get in my way.
When I use the search endpoint, the framework that I use (C#) can easily cast that JSON result to a list of models. However, with the extra text in the GraphQL request, this is more difficult. To use the GraphQL I currently need to make a request, get the JSON string back, basically do string manipulations to strip out all of the aliases, this makes it look like an array of businesses just like the search endpoint, and then I can cast that to a list of models. The only other way I can think of is using dynamic objects or models that contain hardcoded versions of the alias names as object names and I don’t want to do that either.
I guess I was expecting to be able to send an array of ids with the same fragment and get the same type of results I would get back from search.
Request Body
{
business(id: ["fiero-caffe-san-mateo-2", "ninis-coffee-shop-san-mateo"]) {
id
name
rating
price
}
}
or
{
business(id: "fiero-caffe-san-mateo-2") {
...basicBizInfo
},
business(id: "ninis-coffee-shop-san-mateo") {
...basicBizInfo
}
}
fragment basicBizInfo on Business {
id
name
rating
price
}
Response Body
{
"businesses": [
{
"id": "fiero-caffe-san-mateo-2",
"name": "Fiero Caffe",
"rating": 4.5,
"price": "$$"
},
{
"id": "ninis-coffee-shop-san-mateo",
"name": "Nini's Coffee Shop",
"rating": 4.5,
"price": "$$"
}
]
}
This seems a lot more straight forward to me. Do you think the aliases could become optional in the future? Or maybe the alias could become part of the business data model?
{
"alias": "b1",
"id": "ninis-coffee-shop-san-mateo",
"name": "Nini's Coffee Shop",
"rating": 4.5,
"price": "$$"
}
What do you think? Do my suggestions go against what you are trying to achieve? Am I missing something?
The GraphQL endpoint is definitely something I want to utilize as I have lists of businesses that I want to retrieve in as few calls as possible. I’m just curious about your thoughts on the matter going forward as I try to decide how much engineering effort to put forth in making clean GraphQL implementations on my side. Definitely not expecting timelines or promises. Just curious what your thoughts are.
Thanks.
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (4 by maintainers)
@Pinski I think I might have solution 😃
I modified the code from this bit of the JSON.Net docs:
I didn’t run this code and my C# is also rusty, but hopefully its a helpful starting point 😃
@watterso for some reason I didn’t even consider mapping that to a dictionary. I think I was focused too much on Arrays and Lists. Unfortunately, it didn’t convert directly to a dictionary either.
But then I realized since the GraphQL response is a standard, there must be a .NET implementation. That lead me to a few projects and examples. I learned more about the Newtonsoft.Json.Linq.JObject type, which I never heard of before. Between that type and your suggestion about dictionaries, I finally got it to work.
Here it is in case anyone else needs it in the future.
Thanks for all the help and suggestions everyone.