createInputDirective()
createInputDirective<
DirectiveConfig
,Context
>(options
): (schema
) =>GraphQLSchema
Creates a schema transformer that applies an input directive to a GraphQL schema. The directive can be used to validate or transform input fields, arguments, and input types. See https://baeta.io/docs/guides/directives and https://baeta.io/docs/guides/input-directives
Type Parameters
Type Parameter | Default type | Description |
---|---|---|
|
‐ |
Type of the directive configuration object |
|
|
Type of the GraphQL context |
Parameters
Parameter | Type | Description |
---|---|---|
|
|
Configuration options for the input directive |
Returns
A function that transforms a GraphQL schema by applying the directive
(
schema
):GraphQLSchema
Parameters
Parameter | Type |
---|---|
|
|
Returns
GraphQLSchema
Example
const trimDirective = createInputDirective<TrimArgs>({
name: "trim",
target: "scalar",
resolve(params) {
const value = params.getValue();
if (typeof value !== "string") {
return;
}
const config = params.directiveConfig;
if (config.start === true && config.end !== true) {
return params.setValue(value.trimStart());
}
if (config.end === true && config.start !== true) {
return params.setValue(value.trimEnd());
}
params.setValue(value.trim());
},
});