-
Notifications
You must be signed in to change notification settings - Fork 860
Custom queries and mutations using custom resolvers #40
Comments
I'd like to be able to do aggregations on the data essentially what I can achieve in elastic search. Say I have the following:
Id like to be able to groupBy It would return something like:
|
What you can do at the moment is to define a new You define a many-to-many relation between Then you can query all genre counts with this query: query {
allGenres {
_vinylsMeta {
count
}
key
}
} {
"data": {
"allGenres": [
{
"_vinylsMeta": {
"count": 2
},
"key": "SLOW_LIFE"
},
{
"_vinylsMeta": {
"count": 10
},
"key": "TIMELINK"
}
]
}
} or by using aliases: query {
slowlife: allGenres(filter: {key: SLOW_LIFE}) {
_vinylsMeta {
count
}
key
}
timelink: allGenres(filter: {key: TIMELINK}) {
_vinylsMeta {
count
}
key
}
} {
"data": {
"slowlife": [
{
"_vinylsMeta": {
"count": 2
},
"key": "SLOW_LIFE"
}
],
"timelink": [
{
"_vinylsMeta": {
"count": 10
},
"key": "TIMELINK"
}
]
}
} There probably still needs to be some work done on the client-side to accomplish what you want, but until custom queries become a thing that should get the job done. Hope that helps! :) |
We would like to use GraphCool as a proxy to our existing external MySQL database. We have no possibility to migrate our entire database to GraphCool, but we would like to use GraphCool's awesome capabilities to query our own data, without needing to write our own GraphQL server. Ideally, GraphCool would just need a db host and username, and based on the table relations it would be able to figure everything out itself. |
Things we would love to see if we wanted to actually use this for a project:
We have a lot of existing RESTful services (some ours, some of partners) that we cannot migrate. Being able to hook into these would make this incredibly useful, allowing us to "transform" older services into shiny new ones that are all linked together. Like @Thomas-Kuipers said, I think just exposing the ability to connect to a database with a qualified URL would be fantastic and allow for those who have existing data or have a complex setup to get started quickly without trying to migrate GBs or PBs of data. |
another use case for custom queries:
|
Yet another case for custom queries (and access to the database) is to be able to run a custom algorithm on the data and expose it via a query. For example, if you have a newsfeed that depends on a non-trivial ranking process (more than just filtering) it's not possible to implement with Graphcool right now. |
Also see #79 |
A nice addition for these custom operations is the possibility to include information of the currently logged in user as input arguments. |
+1 really need this so we can integrate with our other backend REST APIs. Like others have said, we want one endpoint to Graph.cool and a way to integrate that into our existing services. This would be a huge win! |
Yes custom extension of the GraphQL schema would be awesome 👍 I'm taking care of a legacy app (REST API with underlying Rails code) and it will take a while before the whole thing can be revamped with GraphQL. So a migration path would be needed where the code migration and the data migration can be decoupled. |
Regarding my last comment, actually, it might be more feasible to restart from the Postgres DB layer directly, as others mentioned. I use a Postgresql DB so I guess something like Postgraphql could be used to introspect the schema. At any rate, I think this is quite an important feature also from Graphcool's point of view so that you can attract bigger existing customers. It's not very feasible to require of a production system to get rewritten from scratch at once. A more sensible approach is to do decouple the code and data migration. I'm planning to start working on this now, and would love to do so using Graphcool. Would you have a status update? 🙂 |
A lot of good use cases in this thread - thanks everyone! A few notes: @migueloller In the case of a complex newsfeed you might consider adopting a fan out approach as popularised by twitter and described in http://highscalability.com/blog/2013/7/8/the-architecture-twitter-uses-to-deal-with-150m-active-users.html This guarantees stable read performance and can be easily modelled on Graphcool with two models:
In general @kabriel`s description in https://github.com/graphcool/feature-requests/issues/40#issuecomment-283770482 aligns very well with our vision. We can't commit to a timeline yet, but this is something we are actively working on. |
@sorenbs, interesting approach. I had read this article previously but didn't think how it could be applied with a custom type system. Based on your recommendation, where would custom computations happen? In the mutation callback of the incoming event? Perhaps with a microservice that runs the necessary computation and creates a timeline event with the required fields needed to do a timeline query? |
Our use case is a little bit different from Twitter because we don't have a specific event (a tweet) that triggers a feed update. For us, the feed simply updates every day (every day there are new restaurants and events to recommend) so we would have to simulate that some how with a custom mutation in Graphcool that we have a worker call every day. This sounds a bit hacky, though. |
I think even in that case it's a pretty good approach. We will at some point build a scheduling mechanism into the Graphcool platform, but for now it would be fine to use something external. If your are comfortable with aws I would suggest a periodic lambda function that does the work. If you have a lot of work to do, you can have the periodic function enqueue a bunch of work items to kinesis. I realise this is more work than writing a complex sql query that handles all the ranking though. |
+1 👍 - this would be super powerful. |
👍 |
The ability to incrementBy/decrementBy (x) a numeric field value, for example the likes value on a Post, without having to write two queries to;
would be nice |
Yet another great use case: custom queries can be used to avoid deeply nested json responses and reduce query complexity for nested structures. Instead of querying for See also this related proposal for the GraphQL spec: graphql/graphql-spec#249 |
+1 |
Would love this feature, it would make Graphcool viable for a lot of project in the consultancy I work at, masking obsolete APIs and databases to the client apps moving forward. Also, a the specific use case that brought me to this issue is integration of a weather API in my React Native app. As of now, I will have to interact with it directly, leading to the app communicating with two APIs instead of one - and also preventing me from using GraphQL and Relay, which makes me sad :-/ ( ;-) ) |
Yep, this one would allow Graph.cool to act as an "API gateway" so that one could have a type not backed by the graph.cool automatic database thing but by a function instead. The type could look like (code might be inaccurate):
and the 2 function backing it (one for single element and another for list):
Am I making sense? NOTE: I think having a "context" that easily allow me to access ie: current user, convenience graph.cool graphql querying method ect like in #219 would be best in those functions |
I think the concepts are similar enough that it makes sense to discuss them in this FR. Thanks @thenikso, that looks like a great approach. |
If those two features are connected, I'm worried about the 'getting there' part... |
Configuring an event trigger with a hook to "read" in a request pipeline function might just do the trick. Also, maybe marking fields in a node that should not be stored in the db but provided in a function could be an idea. Even specifying a specific function for a field could cover both partial and complete type backing.
|
An event pipeline for queries sounds great! |
+1 That would be awesome |
Also, keep in mind that a GraphQL endpoint is usually geared towards a specific front-end, and bringing together all back-ends/microservices/datasources in one endpoint. For that reasons, queries should be as specific as necessary for the frontend, and tailored to it. One of the keys to allowing custom queries and mutations is to be able to specify queries on every Type, and being able to enable/disable the auto-generated ones too. For example, if I have a Type The other use case is that my front-end should be as thin as possible, so I don't want to construct queries with ordering and filtering and specific fields. For this, being able to define persisted queries on a Type would be great: https://dev-blog.apollodata.com/persisted-graphql-queries-with-apollo-client-119fd7e6bba5 |
Is there a targeted release date for the ability to use graphcool to wrap rest apis yet? Next week / Next Month / Next year? As soon as I can do this I'm in. I'm on the edge of my seat. Thanks. |
Hey @dberringer, with the new feature Schema Extensions, wrapping REST APIs is already possible. Feel free to join the discussion in the forum to bring up your use case and join the beta 🙂 |
Even better. Thanks @marktani! |
Another use case that either I'm blind and didn't see, or would be really helpful is to be able to effectively create views over data. We have 3 tables, all which have different data, but in a particular screen, they need to be sorted such that they need to be queries and paginated together. So an example:
In SQL we could just union the things and give them a unified structure, then ORDER BY whatever we called the date filed. In graph.cool right now we've created another record that relates to all 3 so we can execute this query, but it'd be much nicer to construct this with a view rather than by needing an actual new node that denormalises the data in order to feed what shows in one screen while the rest of the nodes are actually quite distinct. |
@blargity That seems to be actually more related to https://github.com/graphcool/feature-requests/issues/165. |
@kbrandwijk Indeed, I'll post it over there, thanks! |
Resolver functions available as of now: https://blog.graph.cool/extend-your-graphcool-api-with-resolvers-ca0f0270bca7 🎉 Thanks a lot for everyone who helped testing it out and provided feedback along the way 🙏 |
Use functions to extend your project's GraphQL schema.
The text was updated successfully, but these errors were encountered: