Skip to content
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

docs: custom directive examples and docs page #982

Merged
merged 8 commits into from
May 1, 2023

Conversation

brainrepo
Copy link
Contributor

@brainrepo brainrepo commented Apr 18, 2023

This pr adds a doc page and an example of implementing a custom directive.
After a few iterations, to keep this example compact and avoid working with the AST, I decided to use two libraries, @graphql-tools/schema and @graphql-tools/utils.

I also added some comments in the example file, but I am unsure whether to keep them.

closes #874

Copy link
Collaborator

@simoneb simoneb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's call this redaction rather than censorship :) See e.g. bottom of this page https://www.fastify.io/docs/latest/Reference/Logging/

docs/custom-directive.md Outdated Show resolved Hide resolved
docs/custom-directive.md Outdated Show resolved Hide resolved
docs/custom-directive.md Outdated Show resolved Hide resolved
docs/custom-directive.md Outdated Show resolved Hide resolved
docs/custom-directive.md Outdated Show resolved Hide resolved
@brainrepo
Copy link
Contributor Author

Thank you, All your comments make sense, I am going to implement your suggestions.

@brainrepo brainrepo requested a review from simoneb April 18, 2023 13:45
Copy link
Collaborator

@simoneb simoneb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks better but I still think that the use case you're using is not fitting. Maybe It wasn't clear in my previous comment but you can't redact something that you don't know what will be. You will need to redact the whole field instead. So I imagine the @redact directive to be applied to all fields of a type which possibly contain sensitive information, and they will redact the content of the whole field. Does this make sense now?

docs/custom-directive.md Outdated Show resolved Hide resolved
docs/custom-directive.md Outdated Show resolved Hide resolved
examples/custom-directive.js Outdated Show resolved Hide resolved
@brainrepo brainrepo requested a review from simoneb April 19, 2023 10:20
Copy link
Collaborator

@simoneb simoneb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some comments, but looking better now, thanks

docs/custom-directive.md Outdated Show resolved Hide resolved
docs/custom-directive.md Outdated Show resolved Hide resolved
docs/custom-directive.md Outdated Show resolved Hide resolved
examples/custom-directive.js Show resolved Hide resolved
@brainrepo
Copy link
Contributor Author

I implemented the suggestions

@brainrepo brainrepo marked this pull request as ready for review April 19, 2023 12:35
@brainrepo brainrepo requested a review from simoneb April 19, 2023 12:36
@simoneb
Copy link
Collaborator

simoneb commented Apr 24, 2023

@mcollina this looks good to me but I'd like another pair of eyes before we merge this

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The `mapSchema` function applies each callback function to the corresponding type definition in the schema, creating a new schema with the modified type definitions. The function also provides access to the field resolvers of each object type, allowing you to alter the behaviour of the fields in the schema.

```js
const { mapSchema, getDirective, MapperKind } = require('@graphql-tools/utils')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should document somewhere what version of graphql-tools this guide refers to graphql-tools.

I would also prefer to not use graphql-tools here or show both examples.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opted for the use of graphql tools because, being an example, I wanted to keep it as lean as possible. Inserting a code snippet (generic) for creating an executable schema or for decorating resolvers, risks being misleading for the purpose of the example, making it more complex to understand and also to reuse.
Instead, if I adopt the strategy to modify the schema ad hoc for the redact directive, we might reduce the amount of code but make the example hard to reuse.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with Mauro, we have discussed in detail and replicating the features coming from the graphql-tools library for the sake of this example is just not worth it, and quite frankly in general it's not worth it. Nevertheless since this issue comes up quite often, we'll come up with a couple of ideas that we'll open as issues in this repo:

  • one specific to this request, a way built-into mercurius (or a plugin) to make it easier to define and attach custom directives
  • one generic about creating a new library alternative to graphql-tools with similar features

Copy link
Contributor Author

@brainrepo brainrepo Apr 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I have provided a more detailed argument on this issue. #989

Copy link
Collaborator

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina mcollina merged commit a5ed5b0 into mercurius-js:master May 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to implement custom graphql directives with mercurius, is there a guide or minimal example?
3 participants