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: improve custom directives doc with federation #1009

Merged
merged 3 commits into from
Aug 16, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 92 additions & 3 deletions docs/custom-directive.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ const { mapSchema, getDirective, MapperKind } = require("@graphql-tools/utils");
const PHONE_REGEXP = /(?:\+?\d{2}[ -]?\d{3}[ -]?\d{5}|\d{4})/g;
const EMAIL_REGEXP = /([^\s@])+@[^\s@]+\.[^\s@]+/g;

const redactionSchemaTransformer = (schema) =>
const redactionSchemaTransformer = schema =>
mapSchema(schema, {
// When parsing the schema we find a FIELD
[MapperKind.FIELD]: (fieldConfig) => {
[MapperKind.FIELD]: fieldConfig => {
// Get the directive information
const redactDirective = getDirective(schema, fieldConfig, "redact")?.[0];
if (redactDirective) {
Expand All @@ -77,7 +77,7 @@ const redactionSchemaTransformer = (schema) =>
case "email":
return value.replace(EMAIL_REGEXP, "****@*****.***");
case "phone":
return value.replace(PHONE_REGEXP, (m) => "*".repeat(m.length));
return value.replace(PHONE_REGEXP, m => "*".repeat(m.length));
default:
return value;
}
Expand Down Expand Up @@ -127,3 +127,92 @@ app.register(mercurius, {
## Example

We have a runnable example on "example/custom-directive.js"

# Federation and Custom Directives
Puppo marked this conversation as resolved.
Show resolved Hide resolved

If we are using Federation, we must follow a different approach to create custom directives.
Copy link

Choose a reason for hiding this comment

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

It would be helpful to explain why our existing approach won't work if we are using federation. And also to introduce readers to what we will be doing next (i.e. demonstrate by creating a new @upper custom directive)

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 added more info and reviewed this part.
Let me know if it's better now, please.


Schema definitions and transformer use the same approach indicated above. But schema generation and transformation have different implementations.

## Schema Definition

The schema definition is the same as the one used in the previous example.

```js
const schema = `
directive @upper on FIELD_DEFINITION

extend type Query {
me: User
}

type User @key(fields: "id") {
id: ID!
name: String @upper
username: String
}`;
```

## Transformer

Also, the transformer follows the same approach used in the previous example.

```js
const { mapSchema, getDirective, MapperKind } = require("@graphql-tools/utils");

const uppercaseTransformer = schema =>
mapSchema(schema, {
[MapperKind.FIELD]: fieldConfig => {
const upperDirective = getDirective(schema, fieldConfig, "upper")?.[0];
if (upperDirective) {
fieldConfig.resolve = async (obj, _args, _ctx, info) => {
const value = obj[info.fieldName];
return typeof value === "string" ? value.toUpperCase() : value;
};
}
},
});
```

## Generate executable schema

This section starts to be different. First, we need to create the federation schema using the `buildFederationSchema` function from the `mercurius-federation` library; then, we can use the `makeExecutableSchema` function from the `@graphql-tools/schema` library to create the executable schema.
Puppo marked this conversation as resolved.
Show resolved Hide resolved

```js
const { buildFederationSchema } = require("mercurius-federation");
Puppo marked this conversation as resolved.
Show resolved Hide resolved
const {
printSchemaWithDirectives,
getResolversFromSchema,
} = require("@graphql-tools/utils");
const { mergeResolvers } = require("@graphql-tools/merge");
const { makeExecutableSchema } = require("@graphql-tools/schema");

const federationSchema = buildFederationSchema(schema);

const executableSchema = makeExecutableSchema({
typeDefs: printSchemaWithDirectives(federationSchema),
resolvers: mergeResolvers([
getResolversFromSchema(federationSchema),
resolvers,
]),
});
```

## Apply transformations to the executable schema

To apply the transformation, we have to use the mercurius plugin and pass the options:

- **schema**: with the executableSchema already generated
- **schemaTransforms**: with the transformer functions

```js
app.register(mercurius, {
schema: executableSchema,
schemaTransforms: [redactionSchemaTransformer],
graphiql: true,
});
```

## Example

We have a runnable example in the Federation repo that you can find here [examples/withCustomDirectives.js](https://github.com/mercurius-js/mercurius-federation/tree/main/examples/withCustomDirectives.js).
Loading