Rename Transform
The rename
transform allows you to rename:
- GraphQL types
- GraphQL fields
- GraphQL field arguments
The rename
transform helps you avoid conflicting names, simplify complicated names and make
queries look more like mutations.
rename
elements can contain arrays of individual renaming operations, defined in separate renames
objects. Each of these objects must define the from and to values.
Installation
npm i @graphql-mesh/transform-rename
How to use?
Add the following configuration to your Mesh config file:
transforms:
- rename:
mode: bare | wrap
renames:
# rename type
- from:
type: ApiUser
to:
type: User
# rename type fields
- from:
type: Query
field: apiUser
to:
type: Query
field: user
# rename field arguments
- from:
type: Query
field: user
argument: user_id
to:
type: Query
field: user
argument: userId
type
and field
are mandatory to rename a field argument with argument
.
RegEx example
You can use RegEx flags to enable the use of regular expressions when renaming using this transform. This way you can rename multiple types, fields or both. For example, you could use the key value pair field: api(.*) in the from object to rename any field of the corresponding type that begins with "api".
- rename:
mode: bare | wrap
renames:
- from:
type: Api(.*)
to:
type: $1
useRegExpForTypes: true
- from:
type: Query
field: api(.*)
to:
type: Query
field: $1
useRegExpForFields: true
For information about "bare" and "wrap" modes, please read the dedicated section.
Config API Reference
mode
(type:String (bare | wrap)
) - Specify to apply rename transforms to bare schema or by wrapping original schemarenames
(type:Array of Object
, required) - Array of rename rules:from
(type:Object
, required):type
(type:String
)field
(type:String
)argument
(type:String
)
to
(type:Object
, required):type
(type:String
)field
(type:String
)argument
(type:String
)
useRegExpForTypes
(type:Boolean
) - Use Regular Expression for type namesuseRegExpForFields
(type:Boolean
) - Use Regular Expression for field namesuseRegExpForArguments
(type:Boolean
) - Use Regular Expression for field namesregExpFlags
(type:String
) - Flags to use in the Regular ExpressionincludeDefaults
(type:Boolean
) - Flag to indicate whether certain default types (built-ins, scalars and other types specified an exclusion list) should be renamed or not.
@default: false