-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Proposal: (Un)folding aliases #249
Comments
I think aliases have exactly one irreplaceable function: they allow you to query the same field twice, but with different arguments. I think the ability to rename fields is a nice side effect. Right now GraphQL is only focused on requesting data, and this would move it towards a request/transformation combo, which would probably lead in the direction of more features and make the language a lot more complex. I would suggest using a client-side library to transform the data before passing it to your UI, for example recompose for React, or Apollo Client's built in props transformation. |
Thnx for reply.
I agree that current proposed syntax might be naive yet restructuring is the first thing you do after receive data. I would like to hear from other developers if that's a scenario. |
I just think it's also good to keep in mind the disadvantages of making the GraphQL language more complex to include features for data transformation. IMO the API layer should be as simple as possible and complexity pushed to the edges. |
@esseswann - could you please elaborate a bit more on the problems your suggestion could solve? My request is more for my own understanding, but also maybe others could chime in with possible solutions, which might work without making GraphQL more complex. I agree with Sashko. Making the API more complex should be avoided. Scott |
@smolinari Valid request and this proposal does come from some difficulties I met during developing with react-apollo. Here is a simplified example:
And you want to render it in this way: If there is a lot of fields in
And obviously you just need that little string on the top level but will have to transform the result. |
Given this and other proposals that use dot syntax (#174, #248) which may be easily implemented client side. How about we instead create one proposal which reserves dot syntax space so clients like Apollo could extend the parser to add support for this while not adding unnecessary complexity to servers? |
@calebmer Non dot syntax like for example It would have a real value implementing it server side for obvious performance reasons |
We have a use case for We allow our developers to sync their data to external services such as Algolia. To specify what part of the data they want synced they specify a graphql query. Algolia has a strict requirement about how to format the data to enable geo search functionality. This feature would allow our developers to directly use graphql instead of us introducing explicit support:
I don't know that this is the right direction for GraphQL, but the use cases are obvious rom my perspective. |
Just brainstorming, another option for "values under an alias" would be an inline fragment + directive: node {
... on Node @geoloc {
lat
lng
}
} An intermediary could transform the result. |
By the way this
and even this
is also intuitive and quite handy |
I do like the dot notation. I guess whenever you request a nested field, you would need to use an alias since otherwise there wouldn't be a good name for it? Perhaps this is something that could be done in the form of a query transpiler - something that would compile this into a "regular" GraphQL query, then transform the result appropriately as well. That would allow this feature to be used without modifying the wire protocol or Apollo client. However you still wouldn't easily be able to use query validation tools so in general I'm against extending the syntax unless there is a really good reason. |
@stubailo I myself got a little concern about implementation of such feature. Wouldn't it break the Apollo cache system for example? If restructuring is done on the server then automatic cache update will be difficult |
@esseswann there are two possible approaches:
Unfortunately there isn't something like Babel for transpiling experimental GraphQL syntax so it's hard to implement features like this in a way that many people can try. |
Closing in favor of #174 |
I’d prefer to see that as a separate proposal since it’s only thematically related to #174. This issue suggested two separate proposals, in my opinion. |
Also, we discussed and rejected that proposal in the past: #137 because it lacked a champion. It could be revisited |
I like concept of aliases very much because they allow to abstract components from request, which is especially useful when backend is agnostic about specific client implementation or under heavy development
But there is another aspect of client-server data structure controversy: nesting
To make client even more independent from server schema implementation I propose these enhancements:
To get a deeply nested value
To put some values from the node under an alias
This would allow to quickly reshape queried data in declarative way while keeping the query concise and obvious
The text was updated successfully, but these errors were encountered: