question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Are Aliases really necessary for GraphQL?

See original GitHub issue

Hello,

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:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
wattersocommented, Jun 9, 2017

@Pinski I think I might have solution 😃

I modified the code from this bit of the JSON.Net docs:

string json; //Your JSON response
public class BusinessResponse {} //I assume this is your class?
Dictionary<string, BusinessResponse> responseDictionary = JsonConvert.DeserializeObject<Dictionary<string, BusinessResponse>>(json);
// Do things with responseDictionary
// Convert it to list of BusinessResponse's maybe?

I didn’t run this code and my C# is also rusty, but hopefully its a helpful starting point 😃

0reactions
Pinskicommented, Jun 10, 2017

@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.

var jObject = JObject.Parse(jsonResponse);
var businessResponseDictionary = jObject["data"].ToObject<Dictionary<string, BusinessResponse>>();
var businessResponseList = businessResponseDictionary.Select(keyValuePair => keyValuePair.Value).ToList();

Thanks for all the help and suggestions everyone.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using aliases in GraphQL - LogRocket Blog
Aliases are a great way to reorganize your GraphQL query results. Aliases are especially helpful when your backend data model and frontend data ......
Read more >
How to use GraphQL aliases - Atheros AI
When you use aliases, it is extremely important to keep in mind the performance and use of the data loader for batching requests...
Read more >
Working with GraphQL aliases - Rachita Bansal - Medium
This article will go through a use-case where having aliases can be very useful in the GraphQL query. I have been working on...
Read more >
How to Use GraphQL Aliases — Shopify App Development
In a nutshell, aliases are a way to rename the data that comes back. Notice how the fields, the actual literal field's names,...
Read more >
Using aliases - Apollo GraphQL Docs
The driving principles behind the naming conventions are: ... These principles work great for the vast majority of cases but can't account for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found