group
Library supporting groupByDirective
for grouping GraphQL schema entities.
Seeβ
getGroupName()β
function getGroupName<T>(type, groupByDirective): Maybe<string>
Gets the group name for a schema type based on the directive information.
Type Parametersβ
β’ T
Parametersβ
β’ type: T
a GraphQL schema named type
β’ groupByDirective: Maybe
<GroupByDirectiveOptions
>
the groupByDirective
option.
Returnsβ
Maybe
<string
>
the group name matching the type, or groupByDirective.fallback
if no match found.
Exampleβ
import { buildSchema } from "graphql";
import { getGroupName } from "@graphql-markdown/utils/groups";
const schema = buildSchema(`
directive @doc(
category: String
) on OBJECT | INPUT_OBJECT | UNION | ENUM | INTERFACE | FIELD_DEFINITION | ARGUMENT_DEFINITION
type Unicorn {
name: String!
}
type Bird @doc(category: "animal") {
name: String!
}
type Fish {
name: String!
}
type Elf @doc(category: "fantasy") {
name: String!
}
type Query {
Fish: [Fish!]! @doc(category: "animal")
}
`);
const groupOptions = {
fallback: "common",
directive: "doc",
field: "category",
}
getGroupName(schema.getType("Bird"), groupOptions); // Expected result: "animal"
getGroupName(schema.getType("Unicorn"), groupOptions); // Expected result: "common"
Defined inβ
packages/graphql/src/group.ts:72
getGroups()β
function getGroups(schemaMap, groupByDirective): Maybe<Partial<Record<SchemaEntity, Record<string, Maybe<string>>>>>
Parses a GraphQL schema to build a map of entities with matching groupByDirective
option.
Parametersβ
β’ schemaMap: SchemaMap
the GraphQL schema map returned by introspection!getSchemaMap
β’ groupByDirective: Maybe
<GroupByDirectiveOptions
>
the groupByDirective
option.
Returnsβ
Maybe
<Partial
<Record
<SchemaEntity
, Record
<string
, Maybe
<string
>>>>>
a map of entities with matching group name.
Exampleβ
import { buildSchema } from "graphql";
import { getGroups } from "@graphql-markdown/utils/groups";
const schema = buildSchema(`
directive @doc(
category: String
) on OBJECT | INPUT_OBJECT | UNION | ENUM | INTERFACE | FIELD_DEFINITION | ARGUMENT_DEFINITION
type Unicorn {
name: String!
}
type Bird @doc(category: "animal") {
name: String!
}
type Fish {
name: String!
}
type Elf @doc(category: "fantasy") {
name: String!
}
type Query {
Fish: [Fish!]! @doc(category: "animal")
}
`);
const schemaMap = {
objects: schema.getTypeMap(),
queries: schema.getQueryType()?.getFields(),
};
const groupOptions = {
fallback: "common",
directive: "doc",
field: "category",
}
const groupsMap = getGroups(schemaMap, groupOptions);
// Expected result: {
// "objects": {
// "Bird": "animal",
// "Boolean": "common",
// "Elf": "fantasy",
// "Fish": "common",
// "Query": "common",
// "String": "common",
// "Unicorn": "common",
// },
// "queries": {
// "Fish": "animal",
// },
// }
Defined inβ
packages/graphql/src/group.ts:173