Modify Result Transform - is a transform for GraphQL Mesh that allows you to modify the responses of GraphQL queries. This transform provides various modifiers to alter data in responses, which can be useful for data masking, text replacement, date and time transformation, or executing custom JavaScript functions for dynamic data handling.
Before you can use the Modify Result Transform, you need to install it along with GraphQL Mesh if you haven't already done so. You can install these using npm or yarn.
npm install @dmamontov/graphql-mesh-modify-result-transform
or
yarn add @dmamontov/graphql-mesh-modify-result-transform
To make TypeScript recognize the Modify Result Transform, you need to add an alias in your tsconfig.json.
Add the following paths configuration under the compilerOptions in your tsconfig.json file:
{
"compilerOptions": {
"paths": {
"modify-result": ["./node_modules/@dmamontov/graphql-mesh-modify-result-transform"]
}
}
}
You need to include the Modify Result Transform in your GraphQL Mesh configuration file (usually .meshrc.yaml). Below is an example configuration that demonstrates how to use this transform:
transforms:
- modifyResult:
- typeName: Order
fields: [ status_code ]
modifiers:
- match: "^(DELIVERED|CANCELED|COMPLETED)$"
result: "STATUS_CODE_$1"
as: status_code_enum
- typeName: Order
fields: [ created_at ]
modifiers:
- to: timestamp
from: 'YYYY-MM-DD'
modify: '-3 hours'
- typeName: Order
fields: [ comment ]
modifiers:
- func: "value.trim()"
- typeName: Order
fields: [ card ]
modifiers:
- mask: "0000 **** **** 0000"
Remember, always test your configurations in a development environment before applying them in production to ensure that everything works as expected.