directive
Library supporting customDirective for directive based customization.
Seeβ
WILDCARD_DIRECTIVEβ
const WILDCARD_DIRECTIVE: "*";
Defined in: packages/graphql/src/directive.ts:29
Wildcard * character for matching any directive name.
See getCustomDirectiveOptions, isCustomDirective
getConstDirectiveMap()β
function getConstDirectiveMap(entity, customDirectiveMap): Maybe<CustomDirectiveMap>;
Defined in: packages/graphql/src/directive.ts:249
Returns a map of custom directives for a schema entity.
Parametersβ
entityβ
unknown
a GraphQL schema entity.
customDirectiveMapβ
Maybe<CustomDirectiveMap>
a custom directive map (see getCustomDirectives).
Returnsβ
Maybe<CustomDirectiveMap>
a map of GraphQL directives matching the custom directives defined, else undefined.
Exampleβ
import { buildSchema } from "graphql";
import { getConstDirectiveMap } from "@graphql-markdown/utils/directive";
const schema = buildSchema(`
    directive @testA(
      arg: ArgEnum = ARGA
    ) on OBJECT | FIELD_DEFINITION
    directive @testB(
      argA: Int!,
      argB: [String!]
    ) on FIELD_DEFINITION
    enum ArgEnum {
      ARGA
      ARGB
      ARGC
    }
    type Test @testA {
      id: ID!
      fieldA: [String!]
        @testA(arg: ARGC)
        @testB(argA: 10, argB: ["testArgB"])
    }
    type TestWithoutDirective {
      id: ID!
    }
  `);
const customDirectives = {
  testA: {
    type: schema.getDirective("testA"),
    descriptor: (_, constDirectiveType) => `${constDirectiveType.name}`;
  },
};
const map = getConstDirectiveMap(schema.getType("Test"), customDirectives);
// Expected result: {
//   "descriptor": (_, constDirectiveType) => `${constDirectiveType.name}`,
//   "type": schema.getDirective("testA"),
// }
getCustomDirectiveOptions()β
function getCustomDirectiveOptions(schemaDirectiveName, customDirectiveOptions): Maybe<Partial<Record<CustomDirectiveResolver, CustomDirectiveFunction>>>;
Defined in: packages/graphql/src/directive.ts:77
Returns a record set of custom handlers from a directive by name.
Parametersβ
schemaDirectiveNameβ
DirectiveName
the GraphQL directive name.
customDirectiveOptionsβ
CustomDirective
the customDirective option.
Returnsβ
Maybe<Partial<Record<CustomDirectiveResolver, CustomDirectiveFunction>>>
a record set of custom handlers for the matching directive (or if * is declared), or undefined if no match.
Exampleβ
import { getCustomDirectiveOptions } from "@graphql-markdown/utils/directive";
const customDirectiveOptions = {
  "*": {
    descriptor: (_, constDirectiveType) => `Wildcard ${constDirectiveType.name}`;
  },
};
const customDirectives = getCustomDirectiveOptions("testB", customDirectiveOptions);
// Expected result: {
//   "descriptor": (_, constDirectiveType) => `Wildcard ${constDirectiveType.name}`,
//   "type": "@testB",
// }
getCustomDirectives()β
function getCustomDirectives(schemaMap, customDirectiveOptions?): Maybe<CustomDirectiveMap>;
Defined in: packages/graphql/src/directive.ts:151
Returns a custom directives map with custom handlers from customDirective.
Parametersβ
schemaMapβ
Pick<SchemaMap, "directives">
the GraphQL schema map returned by introspection!getSchemaMap
customDirectiveOptions?β
Maybe<CustomDirective>
the customDirective option.
Returnsβ
Maybe<CustomDirectiveMap>
a custom directive map, or undefined if no match.
Exampleβ
import { buildSchema } from "graphql";
import { getCustomDirectives } from "@graphql-markdown/utils/directive";
const schema = buildSchema(`
  directive @testA(
    arg: ArgEnum = ARGA
  ) on OBJECT | FIELD_DEFINITION
  directive @testB(
    argA: Int!,
    argB: [String!]
  ) on FIELD_DEFINITION
  enum ArgEnum {
    ARGA
    ARGB
    ARGC
  }
`);
const schemaMap = {
  directives: {
    testA: schema.getDirective("testA"),
    testB: schema.getDirective("testB"),
  },
};
const customDirectiveOptions = {
  testA: {
    descriptor: (_, constDirectiveType) => `Named directive ${constDirectiveType.name}`;
  },
  "*": {
    descriptor: (_, constDirectiveType) => `Wildcard ${constDirectiveType.name}`;
  },
};
const customDirectives = getCustomDirectives(schemaMap, customDirectiveOptions);
// Expected result: {
//   "testA": {
//     "descriptor": (_, constDirectiveType) => `Named directive ${constDirectiveType.name}`,
//     "type": "@testA",
//   },
//   "testB": {
//     "descriptor": (_, constDirectiveType) => `Wildcard ${constDirectiveType.name}`,
//     "type": "@testB",
//   },
// }
isCustomDirective()β
function isCustomDirective(schemaDirectiveName, customDirectiveOptions): boolean;
Defined in: packages/graphql/src/directive.ts:40
Checks if a directive name is referenced in customDirective option.
Parametersβ
schemaDirectiveNameβ
DirectiveName
the GraphQL directive name.
customDirectiveOptionsβ
CustomDirective
the customDirective option.
Returnsβ
boolean
true if the directive is declared or * is declared in customDirective option, else false.