-
-
Notifications
You must be signed in to change notification settings - Fork 676
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
Decorator allowing field metadata annotations #124
Comments
To access a data from decorator in middleware, you can create custom decorators that uses middlewares under the hood: function Metadata(args: { isUseful: boolean}) {
return UseMiddleware(({}, next) => {
if (!args.isUseful) {
return null; // or other logic
}
return next();
});
} If you need to access graphql-related metadata in the middleware, you might be interested in #123. If you think about #36 and the need to emit some data into |
Yeah, I'm thinking this would be applied at schema creation, then a global middleware would have access to the annotated schema and would be able to work it's magic. My suggestion here is that @metadata would be a primitive exposed by type-graphql, which defines additional properties to be passed into a field at schema creation time. What's the plugin API you're referring to? I can't see documentation or an open issue for it. |
So you also need Sorry but I don't understand the "middleware would have access to the annotated schema" part 😕 |
Yes, the proposed feature here is for an API to do this. How would you feel about integrating this feature in the core since there isn't a plugin API yet?
It would have access to it via the This would be used top-level validation rule, possibly applied by the graphql server before the schema is even evaluated. |
For #36 I would have to add a field to decorator config, store it in MedatataStorage and manually place in ObjectType config object on schema generation.
They are global global 😆 But basically, scoping type of global middlewares makes sense 🙄 export class GlobalQueryMiddleware implements MiddlewareInterface<Context> {
static type = "resolverOnly";
async use({ context, info }: ResolverData<Context>, next: NextFn) {
return next();
}
} But for middleware functions it would have to be like the export const GlobalQueryMiddleware: MiddlewareFn = async ({ info }, next) => {
await next();
};
GlobalQueryMiddleware.type = "resolverOnly"; For now nobody complained about the global middlewares scoping, seems I should make it more flexible in the future 😉 |
I absolutely agree with this, a plugin system is definitely the right way to go. But it's a big problem exposing a public GraphQL API in production without proper query complexity validation as it basically makes it trivial to DOS your server by crafting a malicious query. So I'm wondering if a nice compromise between those two concerns would be for an API that:
That would be nice, but at least for #36, probably isn't required as I can just validate the query before evaluating it against the schema. |
Also, worth mentioning.... I'd be happy to have a go at implementing it.... assuming you were ok with the general approach 🙂 |
Sure, it's quite easy do implement it:
And remember to create a test cases for this feature and add info about it in docs. In the future I would extract it to the separate plugin package but the API will be the same. Users only need to install the package and register the plugin to make it works again. |
Awesome. Thanks for the pointers, I'll take a look over the next few days. |
@chrisdevereux |
Is it possible to add custom metadata now? We're using join monster, and that requires us to add additional metadata to GraphQL objects. |
@wesselvdv API usage - @ObjectType()
class MyObjectType {
@Metadata({ isUseful: false })
@Field()
myAnnotatedField() {
return "12"
}
} Steps to do:
fieldsMap[field.schemaName] = {
// ...
deprecationReason: field.deprecationReason,
...(field.metadata || {}),
};
|
Currently this is blocked by graphql/graphql-js#1527 (comment). The GraphQL team will remove the current hackish workaround and introduce a special property in type config for placing custom metadata, so we have to wait for |
@19majkel94 graphql/graphql-js#1527 has been implemented |
We should rename this decorator to |
@MichalLytek I would like to give this a go |
Already taken by my friend @VeryVicious, will let you know @palaparthi if something changes. |
Is @VeryVicious still working on this? |
@glen-84 I'm afraid not, you can take try to tackle it down if @palaparthi is not interested anymore 😉 |
Hello 👋 We are also very interested in this feature for our project 🙂If neither of them are interested anymore I can give it a try as well 🙂 |
Maybe |
Thank you for the suggestion @glen-84 🙂I agree, and have updated my branch to reflect it. I have now opened a PR for review and I hope we can make some progress on it. |
Closed by #521 🔒 Released in |
It is generally useful to be able to annotate fields with custom metadata for use by middleware and validators. See: #77 and #36 (my personal use case)
Since there currently isn't support for directives in GraphQL-js, an alternative might be for TypeGraphQL to add a new decorator that annotates the fiend config object passed into the GraphQLObjectType constructor with additional user-defined metadata fields.
I'd imagine the API looking something like this:
This relatively simple change would make it possible to write middleware libraries wrapping tools such as graphql-query-complexity.
The text was updated successfully, but these errors were encountered: