-
-
Notifications
You must be signed in to change notification settings - Fork 171
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
Implement query hooks (middleware) #69
Comments
In case you want some inspiration from similar abstractions gqlgen (we use it at our company to generate the graphql code): https://github.com/99designs/gqlgen/blob/master/graphql/handler.go E.g we have an observability middleware that records the time before executing the decorated operation (query or mutation), then after and the substraction is the latency of the operation and we record as a gauge metric for prometheus, with a metric tag being the name of the operation. This way we can know how long an operation is in the graphql layer. We have another one to filter all non-login queries/mutations so we do auth, etc. |
I'm not against adding more functionality. I'm just careful about adding new public APIs and first try to understand the use-case they will satisfy that cannot be done currently.
|
Core is not a server Also I'll be exposing the service itself as a set of public APIs so people can integrate the entire service into their app. I see
You can do this right now in your own code. func httpHandler(w, r) {
...read in json
...varlidate json
...call core.GraphQL(context, query, validated_json)
...do something with outputted json
.. return it to user
}
Same as the above. it's your own http service.
I sense the confusion here arises from the fact that Super Graph is a GO library and a standalone service and I feel maybe you are talking about adding hooks to the service and not the library. So you can leverage everything the service does and add more? Correct me if this is not what you're thinking. |
Yes all core functionality is exported via core.GraphQL(context, query, validated_json), but how are we going to validate the json data (or even modify it) if we don't know if it is a query or a mutation, and what are the subjects? Typically we'll have a single api entry point (e.g. /avi/v1/graphql) and it will receive all queries and their vars. But vars are query dependent. I may have exaggerated when I said "ast"; I meant have to known something about the graphql query to allow proper validation and middleware. mutation {
product(insert: $data) {
id
name
}
} how do I know I will be validating a product instead of an user? mutation {
users(insert: $data) {
id
name
email
}
} |
We have two other exported functions that could help here. https://pkg.go.dev/github.com/dosco/super-graph/core?tab=doc#Name https://pkg.go.dev/github.com/dosco/super-graph/core?tab=doc#Operation |
The one issue I do see here is that if you manipulate the incoming json then you have to marshal it back into json before passing it to |
Woah, sorry I forgot to answer this one. The exported functions are indeed of great help! And I agree the marshalling is a minor issue. |
What would you like to be added:
Custom logic hooks before a query is run (i.e. middleware)
Why is this needed:
By implementing this, a number of features could be implemented, specially in embedded mode.
I think this could be another killer feature for supergraph, currently unmatched in other servers, and opening a wide range of features and collaboration.
How could this be implemented?
I suggest using this api, which is extensible in the future, and wouldn't break existing programs:
Currently on hasura they implement actions by calling webhooks by sending a struct {type, definition. handler, kind}.
References:
#17
#26
https://discord.com/channels/628796009539043348/628796009539043350/716096650443096164
The text was updated successfully, but these errors were encountered: