6 Mar 2023
type Profile {
id: Int!
email: String! @maskedEmail
name: String!
birthday: DateTime! @yearOnly
}
import {
createTransformerDirective,
TransformerDirectiveFunc,
} from "@redwoodjs/graphql-server";
import { logger } from "src/lib/logger";
export const schema = gql`
"""
Use @maskedEmail to transform the resolved value to return a modified result.
"""
directive @maskedEmail on FIELD_DEFINITION
`;
const transform: TransformerDirectiveFunc = ({ context, resolvedValue }) => {
const [username, domain] = resolvedValue.split("@");
const maskedUsername = `${username.slice(0, 1)}${"*".repeat(
username.length - 1
)}`;
const maskedTld = domain.split(/\./).pop();
const maskedDomain = `${"*".repeat(
domain.length - maskedTld.length - 1
)}.${maskedTld}`;
const masked = `${maskedUsername}@${maskedDomain}`;
return masked;
};
const maskedEmail = createTransformerDirective(schema, transform);
export default maskedEmail;