@graphql-markdown/graphql
Advanced tools
Comparing version
@@ -17,2 +17,40 @@ /** | ||
/** | ||
* Checks if a directive name is referenced in `customDirective` option. | ||
* | ||
* @param schemaDirectiveName - the GraphQL directive name. | ||
* @param customDirectiveOptions - the `customDirective` option. | ||
* | ||
* @returns `true` if the directive is declared or `*` is declared in `customDirective` option, else `false`. | ||
* | ||
*/ | ||
export declare const isCustomDirective: (schemaDirectiveName: DirectiveName, customDirectiveOptions: CustomDirective) => boolean; | ||
/** | ||
* Returns a record set of custom handlers from a directive by name. | ||
* | ||
* @param schemaDirectiveName - the GraphQL directive name. | ||
* @param customDirectiveOptions - the `customDirective` option. | ||
* | ||
* @returns a record set of custom handlers for the matching directive (or if `*` is declared), or undefined if no match. | ||
* | ||
* @example | ||
* ```js | ||
* 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", | ||
* // } | ||
* ``` | ||
* | ||
*/ | ||
export declare const getCustomDirectiveOptions: (schemaDirectiveName: DirectiveName, customDirectiveOptions: CustomDirective) => Maybe<CustomDirectiveOptions>; | ||
/** | ||
* Returns a custom directives map with custom handlers from `customDirective`. | ||
@@ -76,42 +114,4 @@ * | ||
*/ | ||
export declare function getCustomDirectives({ directives: schemaDirectives }: Pick<SchemaMap, "directives">, customDirectiveOptions?: CustomDirective): Maybe<CustomDirectiveMap>; | ||
export declare const getCustomDirectives: ({ directives: schemaDirectives }: Pick<SchemaMap, "directives">, customDirectiveOptions?: CustomDirective) => Maybe<CustomDirectiveMap>; | ||
/** | ||
* Returns a record set of custom handlers from a directive by name. | ||
* | ||
* @param schemaDirectiveName - the GraphQL directive name. | ||
* @param customDirectiveOptions - the `customDirective` option. | ||
* | ||
* @returns a record set of custom handlers for the matching directive (or if `*` is declared), or undefined if no match. | ||
* | ||
* @example | ||
* ```js | ||
* 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", | ||
* // } | ||
* ``` | ||
* | ||
*/ | ||
export declare function getCustomDirectiveOptions(schemaDirectiveName: DirectiveName, customDirectiveOptions: CustomDirective): Maybe<CustomDirectiveOptions>; | ||
/** | ||
* Checks if a directive name is referenced in `customDirective` option. | ||
* | ||
* @param schemaDirectiveName - the GraphQL directive name. | ||
* @param customDirectiveOptions - the `customDirective` option. | ||
* | ||
* @returns `true` if the directive is declared or `*` is declared in `customDirective` option, else `false`. | ||
* | ||
*/ | ||
export declare function isCustomDirective(schemaDirectiveName: DirectiveName, customDirectiveOptions: CustomDirective): boolean; | ||
/** | ||
* Returns a map of custom directives for a schema entity. | ||
@@ -172,2 +172,2 @@ * | ||
*/ | ||
export declare function getConstDirectiveMap(entity: unknown, customDirectiveMap: Maybe<CustomDirectiveMap>): Maybe<CustomDirectiveMap>; | ||
export declare const getConstDirectiveMap: (entity: unknown, customDirectiveMap: Maybe<CustomDirectiveMap>) => Maybe<CustomDirectiveMap>; |
@@ -10,3 +10,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getConstDirectiveMap = exports.isCustomDirective = exports.getCustomDirectiveOptions = exports.getCustomDirectives = exports.WILDCARD_DIRECTIVE = void 0; | ||
exports.getConstDirectiveMap = exports.getCustomDirectives = exports.getCustomDirectiveOptions = exports.isCustomDirective = exports.WILDCARD_DIRECTIVE = void 0; | ||
const utils_1 = require("@graphql-markdown/utils"); | ||
@@ -22,2 +22,53 @@ const introspection_1 = require("./introspection"); | ||
/** | ||
* Checks if a directive name is referenced in `customDirective` option. | ||
* | ||
* @param schemaDirectiveName - the GraphQL directive name. | ||
* @param customDirectiveOptions - the `customDirective` option. | ||
* | ||
* @returns `true` if the directive is declared or `*` is declared in `customDirective` option, else `false`. | ||
* | ||
*/ | ||
const isCustomDirective = (schemaDirectiveName, customDirectiveOptions) => { | ||
return (schemaDirectiveName in customDirectiveOptions || | ||
exports.WILDCARD_DIRECTIVE in customDirectiveOptions); | ||
}; | ||
exports.isCustomDirective = isCustomDirective; | ||
/** | ||
* Returns a record set of custom handlers from a directive by name. | ||
* | ||
* @param schemaDirectiveName - the GraphQL directive name. | ||
* @param customDirectiveOptions - the `customDirective` option. | ||
* | ||
* @returns a record set of custom handlers for the matching directive (or if `*` is declared), or undefined if no match. | ||
* | ||
* @example | ||
* ```js | ||
* 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", | ||
* // } | ||
* ``` | ||
* | ||
*/ | ||
const getCustomDirectiveOptions = (schemaDirectiveName, customDirectiveOptions) => { | ||
if (schemaDirectiveName in customDirectiveOptions) { | ||
return customDirectiveOptions[schemaDirectiveName]; | ||
} | ||
if (exports.WILDCARD_DIRECTIVE in customDirectiveOptions) { | ||
return customDirectiveOptions[exports.WILDCARD_DIRECTIVE]; | ||
} | ||
return undefined; | ||
}; | ||
exports.getCustomDirectiveOptions = getCustomDirectiveOptions; | ||
/** | ||
* Returns a custom directives map with custom handlers from `customDirective`. | ||
@@ -81,3 +132,3 @@ * | ||
*/ | ||
function getCustomDirectives({ directives: schemaDirectives }, customDirectiveOptions) { | ||
const getCustomDirectives = ({ directives: schemaDirectives }, customDirectiveOptions) => { | ||
const customDirectives = {}; | ||
@@ -89,6 +140,6 @@ if (typeof schemaDirectives !== "object" || | ||
for (const schemaDirectiveName in schemaDirectives) { | ||
if (!isCustomDirective(schemaDirectiveName, customDirectiveOptions)) { | ||
if (!(0, exports.isCustomDirective)(schemaDirectiveName, customDirectiveOptions)) { | ||
continue; | ||
} | ||
const directiveOptions = getCustomDirectiveOptions(schemaDirectiveName, customDirectiveOptions); | ||
const directiveOptions = (0, exports.getCustomDirectiveOptions)(schemaDirectiveName, customDirectiveOptions); | ||
if (typeof directiveOptions !== "object") { | ||
@@ -103,56 +154,5 @@ continue; | ||
return (0, utils_1.isEmpty)(customDirectives) ? undefined : customDirectives; | ||
} | ||
}; | ||
exports.getCustomDirectives = getCustomDirectives; | ||
/** | ||
* Returns a record set of custom handlers from a directive by name. | ||
* | ||
* @param schemaDirectiveName - the GraphQL directive name. | ||
* @param customDirectiveOptions - the `customDirective` option. | ||
* | ||
* @returns a record set of custom handlers for the matching directive (or if `*` is declared), or undefined if no match. | ||
* | ||
* @example | ||
* ```js | ||
* 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", | ||
* // } | ||
* ``` | ||
* | ||
*/ | ||
function getCustomDirectiveOptions(schemaDirectiveName, customDirectiveOptions) { | ||
if (schemaDirectiveName in customDirectiveOptions) { | ||
return customDirectiveOptions[schemaDirectiveName]; | ||
} | ||
if (exports.WILDCARD_DIRECTIVE in customDirectiveOptions) { | ||
return customDirectiveOptions[exports.WILDCARD_DIRECTIVE]; | ||
} | ||
return undefined; | ||
} | ||
exports.getCustomDirectiveOptions = getCustomDirectiveOptions; | ||
/** | ||
* Checks if a directive name is referenced in `customDirective` option. | ||
* | ||
* @param schemaDirectiveName - the GraphQL directive name. | ||
* @param customDirectiveOptions - the `customDirective` option. | ||
* | ||
* @returns `true` if the directive is declared or `*` is declared in `customDirective` option, else `false`. | ||
* | ||
*/ | ||
function isCustomDirective(schemaDirectiveName, customDirectiveOptions) { | ||
return (schemaDirectiveName in customDirectiveOptions || | ||
exports.WILDCARD_DIRECTIVE in customDirectiveOptions); | ||
} | ||
exports.isCustomDirective = isCustomDirective; | ||
/** | ||
* Returns a map of custom directives for a schema entity. | ||
@@ -213,7 +213,10 @@ * | ||
*/ | ||
function getConstDirectiveMap(entity, customDirectiveMap) { | ||
const getConstDirectiveMap = (entity, customDirectiveMap) => { | ||
if (!customDirectiveMap || (0, utils_1.isEmpty)(customDirectiveMap)) { | ||
return undefined; | ||
} | ||
const constDirectives = (0, introspection_1.getDirective)(entity, Object.keys(customDirectiveMap)); | ||
const directiveList = Object.values(customDirectiveMap).map((directiveMapItem) => { | ||
return directiveMapItem.type; | ||
}); | ||
const constDirectives = (0, introspection_1.getDirective)(entity, directiveList); | ||
if (constDirectives.length === 0) { | ||
@@ -227,3 +230,3 @@ return undefined; | ||
}, {}); | ||
} | ||
}; | ||
exports.getConstDirectiveMap = getConstDirectiveMap; |
@@ -19,5 +19,5 @@ /** | ||
*/ | ||
export declare function getFormattedDefaultValue<T>({ type, defaultValue, }: { | ||
export declare const getFormattedDefaultValue: <T>({ type, defaultValue, }: { | ||
type: Maybe<GraphQLType>; | ||
defaultValue: T; | ||
}): Maybe<T | string>; | ||
}) => Maybe<string | T>; |
@@ -11,25 +11,28 @@ "use strict"; | ||
/** | ||
* Returns a printable formatted value for a GraphQL type. | ||
* This is the generic function. | ||
* Format an enum or scalar GraphQL type value into a printable equivalent based on the type. | ||
* | ||
* @param entity - the GraphQL schema entity processed. | ||
* @param entity.type - the GraphQL schema type. | ||
* @param entity.defaultValue - the GraphQL schema type's value to be formatted. | ||
* @internal | ||
* | ||
* @param type - the GraphQL schema type. | ||
* @param defaultValue - the GraphQL schema type's value to be processed | ||
* | ||
* @returns a printable formatted value. | ||
* | ||
*/ | ||
function getFormattedDefaultValue({ type, defaultValue, }) { | ||
if (typeof type === "undefined" || | ||
type === null || | ||
typeof defaultValue === "undefined" || | ||
defaultValue === null) { | ||
return undefined; | ||
const _formatDefaultValue = (type, defaultValue) => { | ||
if ((0, graphql_1.isEnumType)(type)) { | ||
return defaultValue; | ||
} | ||
if ((0, graphql_1.isListType)(type)) { | ||
return __formatListDefaultValues((0, graphql_1.getNamedType)(type), defaultValue); | ||
switch (type) { | ||
case graphql_1.GraphQLInt: | ||
case graphql_1.GraphQLFloat: | ||
case graphql_1.GraphQLBoolean: | ||
return defaultValue; | ||
case graphql_1.GraphQLID: | ||
case graphql_1.GraphQLString: | ||
return `"${defaultValue}"`; | ||
default: | ||
return defaultValue; | ||
} | ||
return __formatDefaultValue(type, defaultValue); | ||
} | ||
exports.getFormattedDefaultValue = getFormattedDefaultValue; | ||
}; | ||
/** | ||
@@ -46,3 +49,3 @@ * Format a list GraphQL type value into a printable equivalent. | ||
*/ | ||
function __formatListDefaultValues(type, defaultValue) { | ||
const _formatListDefaultValues = (type, defaultValue) => { | ||
if (typeof type === "undefined" || type === null) { | ||
@@ -54,31 +57,32 @@ return ""; | ||
: [defaultValue]; | ||
const defaultValuesString = defaultValues.map((defaultValue) => getFormattedDefaultValue({ type, defaultValue })); | ||
const defaultValuesString = defaultValues.map((defaultValue) => { | ||
// eslint-disable-next-line @typescript-eslint/no-use-before-define | ||
return (0, exports.getFormattedDefaultValue)({ type, defaultValue }); | ||
}); | ||
return `[${defaultValuesString.join(", ")}]`; | ||
} | ||
}; | ||
/** | ||
* Format an enum or scalar GraphQL type value into a printable equivalent based on the type. | ||
* Returns a printable formatted value for a GraphQL type. | ||
* This is the generic function. | ||
* | ||
* @internal | ||
* @param entity - the GraphQL schema entity processed. | ||
* @param entity.type - the GraphQL schema type. | ||
* @param entity.defaultValue - the GraphQL schema type's value to be formatted. | ||
* | ||
* @param type - the GraphQL schema type. | ||
* @param defaultValue - the GraphQL schema type's value to be processed | ||
* | ||
* @returns a printable formatted value. | ||
* | ||
*/ | ||
function __formatDefaultValue(type, defaultValue) { | ||
if ((0, graphql_1.isEnumType)(type)) { | ||
return defaultValue; | ||
const getFormattedDefaultValue = ({ type, defaultValue, }) => { | ||
if (typeof type === "undefined" || | ||
type === null || | ||
// eslint-disable-next-line @typescript-eslint/prefer-optional-chain | ||
typeof defaultValue === "undefined" || | ||
defaultValue === null) { | ||
return undefined; | ||
} | ||
switch (type) { | ||
case graphql_1.GraphQLInt: | ||
case graphql_1.GraphQLFloat: | ||
case graphql_1.GraphQLBoolean: | ||
return defaultValue; | ||
case graphql_1.GraphQLID: | ||
case graphql_1.GraphQLString: | ||
return `"${defaultValue}"`; | ||
default: | ||
return defaultValue; | ||
if ((0, graphql_1.isListType)(type)) { | ||
return _formatListDefaultValues((0, graphql_1.getNamedType)(type), defaultValue); | ||
} | ||
} | ||
return _formatDefaultValue(type, defaultValue); | ||
}; | ||
exports.getFormattedDefaultValue = getFormattedDefaultValue; |
@@ -10,8 +10,8 @@ /** | ||
/** | ||
* Parses a GraphQL schema to build a map of entities with matching `groupByDirective` option. | ||
* Gets the group name for a schema type based on the directive information. | ||
* | ||
* @param schemaMap - the GraphQL schema map returned by {@link introspection!getSchemaMap} | ||
* @param type - a GraphQL schema named type | ||
* @param groupByDirective - the `groupByDirective` option. | ||
* | ||
* @returns a map of entities with matching group name. | ||
* @returns the group name matching the type, or `groupByDirective.fallback` if no match found. | ||
* | ||
@@ -21,3 +21,3 @@ * @example | ||
* import { buildSchema } from "graphql"; | ||
* import { getGroups } from "@graphql-markdown/utils/groups"; | ||
* import { getGroupName } from "@graphql-markdown/utils/groups"; | ||
* | ||
@@ -45,8 +45,2 @@ * const schema = buildSchema(` | ||
* | ||
* | ||
* const schemaMap = { | ||
* objects: schema.getTypeMap(), | ||
* queries: schema.getQueryType()?.getFields(), | ||
* }; | ||
* | ||
* const groupOptions = { | ||
@@ -58,29 +52,16 @@ * fallback: "common", | ||
* | ||
* const groupsMap = getGroups(schemaMap, groupOptions); | ||
* getGroupName(schema.getType("Bird"), groupOptions); // Expected result: "animal" | ||
* | ||
* // Expected result: { | ||
* // "objects": { | ||
* // "Bird": "animal", | ||
* // "Boolean": "common", | ||
* // "Elf": "fantasy", | ||
* // "Fish": "common", | ||
* // "Query": "common", | ||
* // "String": "common", | ||
* // "Unicorn": "common", | ||
* // }, | ||
* // "queries": { | ||
* // "Fish": "animal", | ||
* // }, | ||
* // } | ||
* getGroupName(schema.getType("Unicorn"), groupOptions); // Expected result: "common" | ||
* | ||
* ``` | ||
* | ||
*/ | ||
export declare function getGroups(schemaMap: SchemaMap, groupByDirective: Maybe<GroupByDirectiveOptions>): Maybe<SchemaEntitiesGroupMap>; | ||
export declare const getGroupName: <T>(type: T, groupByDirective: Maybe<GroupByDirectiveOptions>) => Maybe<string>; | ||
/** | ||
* Gets the group name for a schema type based on the directive information. | ||
* Parses a GraphQL schema to build a map of entities with matching `groupByDirective` option. | ||
* | ||
* @param type - a GraphQL schema named type | ||
* @param schemaMap - the GraphQL schema map returned by {@link introspection!getSchemaMap} | ||
* @param groupByDirective - the `groupByDirective` option. | ||
* | ||
* @returns the group name matching the type, or `groupByDirective.fallback` if no match found. | ||
* @returns a map of entities with matching group name. | ||
* | ||
@@ -90,3 +71,3 @@ * @example | ||
* import { buildSchema } from "graphql"; | ||
* import { getGroupName } from "@graphql-markdown/utils/groups"; | ||
* import { getGroups } from "@graphql-markdown/utils/groups"; | ||
* | ||
@@ -114,2 +95,8 @@ * const schema = buildSchema(` | ||
* | ||
* | ||
* const schemaMap = { | ||
* objects: schema.getTypeMap(), | ||
* queries: schema.getQueryType()?.getFields(), | ||
* }; | ||
* | ||
* const groupOptions = { | ||
@@ -121,8 +108,21 @@ * fallback: "common", | ||
* | ||
* getGroupName(schema.getType("Bird"), groupOptions); // Expected result: "animal" | ||
* const groupsMap = getGroups(schemaMap, groupOptions); | ||
* | ||
* getGroupName(schema.getType("Unicorn"), groupOptions); // Expected result: "common" | ||
* // Expected result: { | ||
* // "objects": { | ||
* // "Bird": "animal", | ||
* // "Boolean": "common", | ||
* // "Elf": "fantasy", | ||
* // "Fish": "common", | ||
* // "Query": "common", | ||
* // "String": "common", | ||
* // "Unicorn": "common", | ||
* // }, | ||
* // "queries": { | ||
* // "Fish": "animal", | ||
* // }, | ||
* // } | ||
* ``` | ||
* | ||
* ``` | ||
*/ | ||
export declare function getGroupName(type: unknown, groupByDirective: Maybe<GroupByDirectiveOptions>): Maybe<string>; | ||
export declare const getGroups: (schemaMap: SchemaMap, groupByDirective: Maybe<GroupByDirectiveOptions>) => Maybe<SchemaEntitiesGroupMap>; |
@@ -10,11 +10,12 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getGroupName = exports.getGroups = void 0; | ||
exports.getGroups = exports.getGroupName = void 0; | ||
const graphql_1 = require("graphql"); | ||
const introspection_1 = require("./introspection"); | ||
/** | ||
* Parses a GraphQL schema to build a map of entities with matching `groupByDirective` option. | ||
* Gets the group name for a schema type based on the directive information. | ||
* | ||
* @param schemaMap - the GraphQL schema map returned by {@link introspection!getSchemaMap} | ||
* @param type - a GraphQL schema named type | ||
* @param groupByDirective - the `groupByDirective` option. | ||
* | ||
* @returns a map of entities with matching group name. | ||
* @returns the group name matching the type, or `groupByDirective.fallback` if no match found. | ||
* | ||
@@ -24,3 +25,3 @@ * @example | ||
* import { buildSchema } from "graphql"; | ||
* import { getGroups } from "@graphql-markdown/utils/groups"; | ||
* import { getGroupName } from "@graphql-markdown/utils/groups"; | ||
* | ||
@@ -48,8 +49,2 @@ * const schema = buildSchema(` | ||
* | ||
* | ||
* const schemaMap = { | ||
* objects: schema.getTypeMap(), | ||
* queries: schema.getQueryType()?.getFields(), | ||
* }; | ||
* | ||
* const groupOptions = { | ||
@@ -61,47 +56,39 @@ * fallback: "common", | ||
* | ||
* const groupsMap = getGroups(schemaMap, groupOptions); | ||
* getGroupName(schema.getType("Bird"), groupOptions); // Expected result: "animal" | ||
* | ||
* // Expected result: { | ||
* // "objects": { | ||
* // "Bird": "animal", | ||
* // "Boolean": "common", | ||
* // "Elf": "fantasy", | ||
* // "Fish": "common", | ||
* // "Query": "common", | ||
* // "String": "common", | ||
* // "Unicorn": "common", | ||
* // }, | ||
* // "queries": { | ||
* // "Fish": "animal", | ||
* // }, | ||
* // } | ||
* getGroupName(schema.getType("Unicorn"), groupOptions); // Expected result: "common" | ||
* | ||
* ``` | ||
* | ||
*/ | ||
function getGroups(schemaMap, groupByDirective) { | ||
const groups = {}; | ||
if (!groupByDirective) { | ||
const getGroupName = (type, groupByDirective) => { | ||
if (!type || !groupByDirective) { | ||
return undefined; | ||
} | ||
Object.keys(schemaMap).forEach((typeName) => { | ||
const rootType = schemaMap[typeName]; | ||
if (rootType) { | ||
if (typeof groups[typeName] === "undefined") { | ||
groups[typeName] = {}; | ||
} | ||
Object.keys(rootType).forEach((type) => { | ||
groups[typeName][type] = getGroupName(rootType[type], groupByDirective); | ||
}); | ||
if (!(0, introspection_1.hasAstNode)(type)) { | ||
return groupByDirective.fallback; | ||
} | ||
const allDirectives = type.astNode.directives; | ||
if (!Array.isArray(allDirectives)) { | ||
return groupByDirective.fallback; | ||
} | ||
for (const directive of allDirectives) { | ||
if (!directive.arguments || | ||
directive.name.value !== groupByDirective.directive) { | ||
continue; | ||
} | ||
}); | ||
return groups; | ||
} | ||
exports.getGroups = getGroups; | ||
const field = directive.arguments.find(({ name, value }) => { | ||
return (name.value === groupByDirective.field && value.kind === graphql_1.Kind.STRING); | ||
}); | ||
return field?.value.value; | ||
} | ||
return groupByDirective.fallback; | ||
}; | ||
exports.getGroupName = getGroupName; | ||
/** | ||
* Gets the group name for a schema type based on the directive information. | ||
* Parses a GraphQL schema to build a map of entities with matching `groupByDirective` option. | ||
* | ||
* @param type - a GraphQL schema named type | ||
* @param schemaMap - the GraphQL schema map returned by {@link introspection!getSchemaMap} | ||
* @param groupByDirective - the `groupByDirective` option. | ||
* | ||
* @returns the group name matching the type, or `groupByDirective.fallback` if no match found. | ||
* @returns a map of entities with matching group name. | ||
* | ||
@@ -111,3 +98,3 @@ * @example | ||
* import { buildSchema } from "graphql"; | ||
* import { getGroupName } from "@graphql-markdown/utils/groups"; | ||
* import { getGroups } from "@graphql-markdown/utils/groups"; | ||
* | ||
@@ -135,2 +122,8 @@ * const schema = buildSchema(` | ||
* | ||
* | ||
* const schemaMap = { | ||
* objects: schema.getTypeMap(), | ||
* queries: schema.getQueryType()?.getFields(), | ||
* }; | ||
* | ||
* const groupOptions = { | ||
@@ -142,28 +135,39 @@ * fallback: "common", | ||
* | ||
* getGroupName(schema.getType("Bird"), groupOptions); // Expected result: "animal" | ||
* const groupsMap = getGroups(schemaMap, groupOptions); | ||
* | ||
* getGroupName(schema.getType("Unicorn"), groupOptions); // Expected result: "common" | ||
* // Expected result: { | ||
* // "objects": { | ||
* // "Bird": "animal", | ||
* // "Boolean": "common", | ||
* // "Elf": "fantasy", | ||
* // "Fish": "common", | ||
* // "Query": "common", | ||
* // "String": "common", | ||
* // "Unicorn": "common", | ||
* // }, | ||
* // "queries": { | ||
* // "Fish": "animal", | ||
* // }, | ||
* // } | ||
* ``` | ||
* | ||
* ``` | ||
*/ | ||
function getGroupName(type, groupByDirective) { | ||
if (!type || !groupByDirective) { | ||
const getGroups = (schemaMap, groupByDirective) => { | ||
const groups = {}; | ||
if (!groupByDirective) { | ||
return undefined; | ||
} | ||
if (!(0, introspection_1.hasAstNode)(type)) { | ||
return groupByDirective.fallback; | ||
} | ||
const allDirectives = type.astNode.directives; | ||
if (!Array.isArray(allDirectives)) { | ||
return groupByDirective.fallback; | ||
} | ||
for (const directive of allDirectives) { | ||
if (directive.name.value !== groupByDirective.directive) { | ||
continue; | ||
Object.keys(schemaMap).forEach((typeName) => { | ||
const rootType = schemaMap[typeName]; | ||
if (rootType) { | ||
if (typeof groups[typeName] === "undefined") { | ||
groups[typeName] = {}; | ||
} | ||
Object.keys(rootType).forEach((type) => { | ||
groups[typeName][type] = (0, exports.getGroupName)(rootType[type], groupByDirective); | ||
}); | ||
} | ||
const field = directive.arguments.find(({ name }) => name.value === groupByDirective.field); | ||
return field.value.value; | ||
} | ||
return groupByDirective.fallback; | ||
} | ||
exports.getGroupName = getGroupName; | ||
}); | ||
return groups; | ||
}; | ||
exports.getGroups = getGroups; |
@@ -15,3 +15,3 @@ /** | ||
*/ | ||
export declare function isGraphQLFieldType(type: unknown): type is GraphQLField<unknown, unknown, unknown>; | ||
export declare const isGraphQLFieldType: (type: unknown) => type is GraphQLField<unknown, unknown, unknown>; | ||
/** | ||
@@ -25,3 +25,3 @@ * Checks if a GraphQL named type is of generic type `T`. | ||
*/ | ||
export declare function instanceOf<T>(obj: unknown, type: new () => T): obj is T; | ||
export declare const instanceOf: <T>(obj: unknown, type: new () => T) => obj is T; | ||
/** | ||
@@ -34,3 +34,3 @@ * Checks if a GraphQL named type is deprecated. | ||
*/ | ||
export declare function isDeprecated<T>(obj: T): obj is DeprecatedType<T>; | ||
export declare const isDeprecated: <T>(obj: T) => obj is DeprecatedType<T>; | ||
/** | ||
@@ -42,2 +42,2 @@ * Checks if a GraphQL type a GraphQL operation (query, mutation, subscription). | ||
*/ | ||
export declare function isOperation(type: unknown): type is GraphQLOperationType; | ||
export declare const isOperation: (type: unknown) => type is GraphQLOperationType; |
@@ -22,3 +22,3 @@ "use strict"; | ||
*/ | ||
function isGraphQLFieldType(type) { | ||
const isGraphQLFieldType = (type) => { | ||
return (typeof type === "object" && | ||
@@ -28,3 +28,3 @@ type !== null && | ||
type.args.length > 0); | ||
} | ||
}; | ||
exports.isGraphQLFieldType = isGraphQLFieldType; | ||
@@ -39,3 +39,3 @@ /** | ||
*/ | ||
function instanceOf(obj, type) { | ||
const instanceOf = (obj, type) => { | ||
try { | ||
@@ -45,3 +45,3 @@ const expect = type.name; | ||
? false | ||
: obj.constructor.name == expect; | ||
: obj.constructor.name === expect; | ||
} | ||
@@ -51,3 +51,3 @@ catch (_) { | ||
} | ||
} | ||
}; | ||
exports.instanceOf = instanceOf; | ||
@@ -61,3 +61,3 @@ /** | ||
*/ | ||
function isDeprecated(obj) { | ||
const isDeprecated = (obj) => { | ||
return (typeof obj === "object" && | ||
@@ -67,3 +67,3 @@ obj !== null && | ||
("deprecationReason" in obj && typeof obj.deprecationReason === "string"))); | ||
} | ||
}; | ||
exports.isDeprecated = isDeprecated; | ||
@@ -76,5 +76,5 @@ /** | ||
*/ | ||
function isOperation(type) { | ||
const isOperation = (type) => { | ||
return typeof type === "object" && type !== null && "type" in type; | ||
} | ||
}; | ||
exports.isOperation = isOperation; |
@@ -1,4 +0,10 @@ | ||
import type { GraphQLSchema, GraphQLInputFieldMap, GraphQLFieldMap } from "graphql"; | ||
import { GraphQLDirective } from "graphql"; | ||
import type { AstNodeType, GraphQLOperationType, Maybe, SchemaMap } from "@graphql-markdown/types"; | ||
/** | ||
* Library for introspecting a GraphQL schema. | ||
* The entry point method is {@link getSchemaMap}. | ||
* | ||
* @packageDocumentation | ||
*/ | ||
import type { GraphQLSchema, GraphQLInputFieldMap, GraphQLFieldMap, GraphQLDirective } from "graphql"; | ||
import { DirectiveLocation } from "graphql"; | ||
import type { ASTNode, AstNodeType, GraphQLOperationType, Maybe, SchemaMap } from "@graphql-markdown/types"; | ||
export { getDirectiveValues, getNamedType, printSchema } from "graphql"; | ||
@@ -19,3 +25,3 @@ /** | ||
*/ | ||
export declare function getTypeFromSchema<T>(schema: Maybe<GraphQLSchema>, type: unknown): Maybe<Record<string, T>>; | ||
export declare const getTypeFromSchema: <T>(schema: Maybe<GraphQLSchema>, type: unknown) => Maybe<Record<string, T>>; | ||
/** | ||
@@ -31,3 +37,13 @@ * Type guard for type with an AST node property. | ||
*/ | ||
export declare function hasAstNode<T>(node: T): node is AstNodeType<T>; | ||
export declare const hasAstNode: <T>(node: T) => node is AstNodeType<T>; | ||
export declare const getDirectiveLocationForASTPath: (appliedTo: Maybe<ASTNode>) => DirectiveLocation; | ||
/** Check if a directive can be applied to specific schema entity location. | ||
* | ||
* @param entity - a GraphQL schema entity. | ||
* @param directive - a directive name. | ||
* | ||
* @returns `true` if the entity is a valid directive location, else `false`. | ||
* | ||
*/ | ||
export declare const isValidDirectiveLocation: (entity: unknown, directive: GraphQLDirective) => boolean; | ||
/** | ||
@@ -38,2 +54,3 @@ * Checks if a schema entity as a directive belonging to a defined set. | ||
* @param directives - a directive name or a list of directive names. | ||
* @param fallback - default value if the entity type is not a valid location for directives. | ||
* | ||
@@ -43,3 +60,3 @@ * @returns `true` if the entity has at least one directive matching, else `false`. | ||
*/ | ||
export declare function hasDirective(entity: unknown, directives: Maybe<string[] | string>): boolean; | ||
export declare const hasDirective: (entity: unknown, directives: Maybe<GraphQLDirective[]>, fallback?: boolean) => boolean; | ||
/** | ||
@@ -54,25 +71,25 @@ * Returns a schema entity's list of directives matching a defined set. | ||
*/ | ||
export declare function getDirective(entity: unknown, directives: Maybe<string[] | string>): GraphQLDirective[]; | ||
export declare const getDirective: (entity: unknown, directives: Maybe<GraphQLDirective[]>) => GraphQLDirective[]; | ||
/** | ||
* Returns one directive's argument's value linked to a GraphQL schema type. | ||
* It calls {@link getTypeDirectiveValues} and returns a matching record. | ||
* Returns all directive's arguments' values linked to a GraphQL schema type. | ||
* | ||
* @param directive - a GraphQL directive defined in the schema. | ||
* @param type - the GraphQL schema type to parse. | ||
* @param argName - the name of the GraphQL directive argument to fetch the value from. | ||
* | ||
* @returns a record k/v with `argName` as key and the argument's value. | ||
* @returns a record k/v with arguments' name as keys and arguments' value. | ||
* | ||
*/ | ||
export declare function getTypeDirectiveArgValue(directive: GraphQLDirective, node: unknown, argName: string): Maybe<Record<string, unknown>>; | ||
export declare const getTypeDirectiveValues: (directive: GraphQLDirective, type: unknown) => Maybe<Record<string, unknown>>; | ||
/** | ||
* Returns all directive's arguments' values linked to a GraphQL schema type. | ||
* Returns one directive's argument's value linked to a GraphQL schema type. | ||
* It calls {@link getTypeDirectiveValues} and returns a matching record. | ||
* | ||
* @param directive - a GraphQL directive defined in the schema. | ||
* @param type - the GraphQL schema type to parse. | ||
* @param argName - the name of the GraphQL directive argument to fetch the value from. | ||
* | ||
* @returns a record k/v with arguments' name as keys and arguments' value. | ||
* @returns a record k/v with `argName` as key and the argument's value. | ||
* | ||
*/ | ||
export declare function getTypeDirectiveValues(directive: GraphQLDirective, type: unknown): Maybe<Record<string, unknown>>; | ||
export declare const getTypeDirectiveArgValue: (directive: GraphQLDirective, node: unknown, argName: string) => Maybe<Record<string, unknown>>; | ||
/** | ||
@@ -92,9 +109,4 @@ * Returns the fields from a GraphQL schema type. | ||
*/ | ||
export declare function __getFields<T, V>(type: T, | ||
export declare const _getFields: <T, V>(type: T, processor?: ((fieldMap: Record<string, unknown>) => V) | undefined, fallback?: V | undefined) => GraphQLFieldMap<unknown, unknown> | GraphQLInputFieldMap | V; | ||
/** | ||
* @param fieldMap - a field map to be processed. | ||
* @returns a new field map. | ||
*/ | ||
processor?: (fieldMap: Record<string, unknown>) => V, fallback?: V): GraphQLFieldMap<unknown, unknown> | GraphQLInputFieldMap | V; | ||
/** | ||
* Returns fields map for a GraphQL operation type (query, mutation, subscription...). | ||
@@ -111,3 +123,3 @@ * | ||
*/ | ||
export declare function getOperation(operationType?: unknown): Record<string, GraphQLOperationType>; | ||
export declare const getOperation: (operationType?: unknown) => Record<string, GraphQLOperationType>; | ||
/** | ||
@@ -123,3 +135,3 @@ * Returns fields map for a GraphQL schema type. | ||
*/ | ||
export declare function getFields(type: unknown): unknown[]; | ||
export declare const getFields: (type: unknown) => unknown[]; | ||
/** | ||
@@ -134,3 +146,3 @@ * Resolves the name of a GraphQL schema type. | ||
*/ | ||
export declare function getTypeName(type: unknown, defaultName?: string): string; | ||
export declare const getTypeName: (type: unknown, defaultName?: string) => string; | ||
/** | ||
@@ -204,2 +216,2 @@ * Returns an introspection map of the GraphQL schema. | ||
*/ | ||
export declare function getSchemaMap(schema: Maybe<GraphQLSchema>): SchemaMap; | ||
export declare const getSchemaMap: (schema: Maybe<GraphQLSchema>) => SchemaMap; |
"use strict"; | ||
/** | ||
* Library for introspecting a GraphQL schema. | ||
* The entry point method is {@link getSchemaMap}. | ||
* | ||
* @packageDocumentation | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getSchemaMap = exports.getTypeName = exports.getFields = exports.getOperation = exports.__getFields = exports.getTypeDirectiveValues = exports.getTypeDirectiveArgValue = exports.getDirective = exports.hasDirective = exports.hasAstNode = exports.getTypeFromSchema = exports.printSchema = exports.getNamedType = exports.getDirectiveValues = void 0; | ||
exports.getSchemaMap = exports.getTypeName = exports.getFields = exports.getOperation = exports._getFields = exports.getTypeDirectiveArgValue = exports.getTypeDirectiveValues = exports.getDirective = exports.hasDirective = exports.isValidDirectiveLocation = exports.getDirectiveLocationForASTPath = exports.hasAstNode = exports.getTypeFromSchema = exports.printSchema = exports.getNamedType = exports.getDirectiveValues = void 0; | ||
const graphql_1 = require("graphql"); | ||
@@ -25,3 +31,3 @@ const utils_1 = require("@graphql-markdown/utils"); | ||
*/ | ||
function getTypeFromSchema(schema, type) { | ||
const getTypeFromSchema = (schema, type) => { | ||
if (!schema) { | ||
@@ -41,6 +47,12 @@ return undefined; | ||
return Object.keys(typeMap) | ||
.filter((key) => excludeListRegExp.test(key)) | ||
.filter((key) => (0, guard_1.instanceOf)(typeMap[key], type)) | ||
.reduce((res, key) => ({ ...res, [key]: typeMap[key] }), {}); | ||
} | ||
.filter((key) => { | ||
return excludeListRegExp.test(key); | ||
}) | ||
.filter((key) => { | ||
return (0, guard_1.instanceOf)(typeMap[key], type); | ||
}) | ||
.reduce((res, key) => { | ||
return { ...res, [key]: typeMap[key] }; | ||
}, {}); | ||
}; | ||
exports.getTypeFromSchema = getTypeFromSchema; | ||
@@ -57,6 +69,63 @@ /** | ||
*/ | ||
function hasAstNode(node) { | ||
const hasAstNode = (node) => { | ||
return typeof node["astNode"] === "object"; | ||
} | ||
}; | ||
exports.hasAstNode = hasAstNode; | ||
const getDirectiveLocationForASTPath = (appliedTo) => { | ||
if (!appliedTo || !("kind" in appliedTo)) { | ||
throw new Error("Unexpected kind: " + String(appliedTo)); | ||
} | ||
switch (appliedTo.kind) { | ||
case graphql_1.Kind.FIELD: | ||
return graphql_1.DirectiveLocation.FIELD; | ||
case graphql_1.Kind.SCHEMA_DEFINITION: | ||
case graphql_1.Kind.SCHEMA_EXTENSION: | ||
return graphql_1.DirectiveLocation.SCHEMA; | ||
case graphql_1.Kind.SCALAR_TYPE_DEFINITION: | ||
case graphql_1.Kind.SCALAR_TYPE_EXTENSION: | ||
return graphql_1.DirectiveLocation.SCALAR; | ||
case graphql_1.Kind.OBJECT_TYPE_DEFINITION: | ||
case graphql_1.Kind.OBJECT_TYPE_EXTENSION: | ||
return graphql_1.DirectiveLocation.OBJECT; | ||
case graphql_1.Kind.FIELD_DEFINITION: | ||
return graphql_1.DirectiveLocation.FIELD_DEFINITION; | ||
case graphql_1.Kind.INTERFACE_TYPE_DEFINITION: | ||
case graphql_1.Kind.INTERFACE_TYPE_EXTENSION: | ||
return graphql_1.DirectiveLocation.INTERFACE; | ||
case graphql_1.Kind.UNION_TYPE_DEFINITION: | ||
case graphql_1.Kind.UNION_TYPE_EXTENSION: | ||
return graphql_1.DirectiveLocation.UNION; | ||
case graphql_1.Kind.ENUM_TYPE_DEFINITION: | ||
case graphql_1.Kind.ENUM_TYPE_EXTENSION: | ||
return graphql_1.DirectiveLocation.ENUM; | ||
case graphql_1.Kind.ENUM_VALUE_DEFINITION: | ||
return graphql_1.DirectiveLocation.ENUM_VALUE; | ||
case graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION: | ||
case graphql_1.Kind.INPUT_OBJECT_TYPE_EXTENSION: | ||
return graphql_1.DirectiveLocation.INPUT_OBJECT; | ||
case graphql_1.Kind.INPUT_VALUE_DEFINITION: | ||
case graphql_1.Kind.ARGUMENT: | ||
return graphql_1.DirectiveLocation.ARGUMENT_DEFINITION; | ||
// Not reachable, all possible types have been considered. | ||
default: | ||
throw new Error("Unexpected kind: " + String(appliedTo.kind)); | ||
} | ||
}; | ||
exports.getDirectiveLocationForASTPath = getDirectiveLocationForASTPath; | ||
/** Check if a directive can be applied to specific schema entity location. | ||
* | ||
* @param entity - a GraphQL schema entity. | ||
* @param directive - a directive name. | ||
* | ||
* @returns `true` if the entity is a valid directive location, else `false`. | ||
* | ||
*/ | ||
const isValidDirectiveLocation = (entity, directive) => { | ||
if (!(0, exports.hasAstNode)(entity)) { | ||
return false; | ||
} | ||
const location = (0, exports.getDirectiveLocationForASTPath)(entity.astNode); | ||
return directive.locations.includes(location); | ||
}; | ||
exports.isValidDirectiveLocation = isValidDirectiveLocation; | ||
/** | ||
@@ -67,2 +136,3 @@ * Checks if a schema entity as a directive belonging to a defined set. | ||
* @param directives - a directive name or a list of directive names. | ||
* @param fallback - default value if the entity type is not a valid location for directives. | ||
* | ||
@@ -72,11 +142,20 @@ * @returns `true` if the entity has at least one directive matching, else `false`. | ||
*/ | ||
function hasDirective(entity, directives) { | ||
if (!hasAstNode(entity) || | ||
const hasDirective = (entity, directives, fallback = false) => { | ||
if (!(0, exports.hasAstNode)(entity) || | ||
!directives || | ||
!Array.isArray(entity.astNode.directives)) { | ||
return false; | ||
!Array.isArray(entity.astNode.directives) || | ||
directives.length < 1) { | ||
return fallback; | ||
} | ||
const directiveList = Array.isArray(directives) ? directives : [directives]; // backward_compatibility | ||
return (entity.astNode.directives.findIndex((directiveNode) => directiveList.includes(directiveNode.name.value)) > -1); | ||
} | ||
return (directives.findIndex((directive) => { | ||
// if the directive location is not applicable to entity then skip it | ||
if (!(0, exports.isValidDirectiveLocation)(entity, directive)) { | ||
return fallback; | ||
} | ||
return (entity.astNode.directives && | ||
entity.astNode.directives.findIndex((directiveNode) => { | ||
return directive.name === directiveNode.name.value; | ||
}) > -1); | ||
}) > -1); | ||
}; | ||
exports.hasDirective = hasDirective; | ||
@@ -92,4 +171,4 @@ /** | ||
*/ | ||
function getDirective(entity, directives) { | ||
if (!hasAstNode(entity) || | ||
const getDirective = (entity, directives) => { | ||
if (!(0, exports.hasAstNode)(entity) || | ||
!directives || | ||
@@ -99,49 +178,46 @@ !Array.isArray(entity.astNode.directives)) { | ||
} | ||
const directiveList = Array.isArray(directives) ? directives : [directives]; // backward_compatibility | ||
return entity.astNode.directives | ||
.filter((directiveNode) => directiveList.includes(directiveNode.name.value)) | ||
.map((directiveNode) => new graphql_1.GraphQLDirective({ | ||
name: directiveNode.name.value, | ||
description: directiveNode.description?.value, | ||
locations: [], | ||
extensions: undefined, | ||
astNode: directiveNode, | ||
})); | ||
} | ||
return directives.filter((directive) => { | ||
return ((entity.astNode.directives && | ||
entity.astNode.directives.findIndex((directiveNode) => { | ||
return directiveNode.name.value === directive.name; | ||
}) > -1) ?? | ||
false); | ||
}); | ||
}; | ||
exports.getDirective = getDirective; | ||
/** | ||
* Returns one directive's argument's value linked to a GraphQL schema type. | ||
* It calls {@link getTypeDirectiveValues} and returns a matching record. | ||
* Returns all directive's arguments' values linked to a GraphQL schema type. | ||
* | ||
* @param directive - a GraphQL directive defined in the schema. | ||
* @param type - the GraphQL schema type to parse. | ||
* @param argName - the name of the GraphQL directive argument to fetch the value from. | ||
* | ||
* @returns a record k/v with `argName` as key and the argument's value. | ||
* @returns a record k/v with arguments' name as keys and arguments' value. | ||
* | ||
*/ | ||
function getTypeDirectiveArgValue(directive, node, argName) { | ||
const args = getTypeDirectiveValues(directive, node); | ||
if (!args || !args[argName]) { | ||
throw new Error(`Directive argument '${argName}' not found!`); | ||
const getTypeDirectiveValues = (directive, type) => { | ||
if ((0, exports.hasAstNode)(type)) { | ||
return (0, graphql_1.getDirectiveValues)(directive, type.astNode); | ||
} | ||
return args[argName]; | ||
} | ||
exports.getTypeDirectiveArgValue = getTypeDirectiveArgValue; | ||
return (0, graphql_1.getDirectiveValues)(directive, type); | ||
}; | ||
exports.getTypeDirectiveValues = getTypeDirectiveValues; | ||
/** | ||
* Returns all directive's arguments' values linked to a GraphQL schema type. | ||
* Returns one directive's argument's value linked to a GraphQL schema type. | ||
* It calls {@link getTypeDirectiveValues} and returns a matching record. | ||
* | ||
* @param directive - a GraphQL directive defined in the schema. | ||
* @param type - the GraphQL schema type to parse. | ||
* @param argName - the name of the GraphQL directive argument to fetch the value from. | ||
* | ||
* @returns a record k/v with arguments' name as keys and arguments' value. | ||
* @returns a record k/v with `argName` as key and the argument's value. | ||
* | ||
*/ | ||
function getTypeDirectiveValues(directive, type) { | ||
if (hasAstNode(type)) { | ||
return (0, graphql_1.getDirectiveValues)(directive, type.astNode); | ||
const getTypeDirectiveArgValue = (directive, node, argName) => { | ||
const args = (0, exports.getTypeDirectiveValues)(directive, node); | ||
if (!args?.[argName]) { | ||
throw new Error(`Directive argument '${argName}' not found!`); | ||
} | ||
return (0, graphql_1.getDirectiveValues)(directive, type); | ||
} | ||
exports.getTypeDirectiveValues = getTypeDirectiveValues; | ||
return args[argName]; | ||
}; | ||
exports.getTypeDirectiveArgValue = getTypeDirectiveArgValue; | ||
/** | ||
@@ -161,3 +237,3 @@ * Returns the fields from a GraphQL schema type. | ||
*/ | ||
function __getFields(type, | ||
const _getFields = (type, | ||
/** | ||
@@ -167,3 +243,3 @@ * @param fieldMap - a field map to be processed. | ||
*/ | ||
processor, fallback) { | ||
processor, fallback) => { | ||
if (!(typeof type === "object" && | ||
@@ -180,4 +256,4 @@ type !== null && | ||
return fieldMap; | ||
} | ||
exports.__getFields = __getFields; | ||
}; | ||
exports._getFields = _getFields; | ||
/** | ||
@@ -195,5 +271,9 @@ * Returns fields map for a GraphQL operation type (query, mutation, subscription...). | ||
*/ | ||
function getOperation(operationType) { | ||
return __getFields(operationType, (fieldMap) => Object.keys(fieldMap).reduce((res, key) => ({ ...res, [key]: fieldMap[key] }), {}), {}); | ||
} | ||
const getOperation = (operationType) => { | ||
return (0, exports._getFields)(operationType, (fieldMap) => { | ||
return Object.keys(fieldMap).reduce((res, key) => { | ||
return { ...res, [key]: fieldMap[key] }; | ||
}, {}); | ||
}, {}); | ||
}; | ||
exports.getOperation = getOperation; | ||
@@ -210,9 +290,11 @@ /** | ||
*/ | ||
function getFields(type) { | ||
return __getFields(type, (fieldMap) => { | ||
const getFields = (type) => { | ||
return (0, exports._getFields)(type, (fieldMap) => { | ||
const res = []; | ||
Object.keys(fieldMap).forEach((name) => res.push(fieldMap[name])); | ||
Object.keys(fieldMap).forEach((name) => { | ||
return res.push(fieldMap[name]); | ||
}); | ||
return res; | ||
}, []); | ||
} | ||
}; | ||
exports.getFields = getFields; | ||
@@ -228,3 +310,3 @@ /** | ||
*/ | ||
function getTypeName(type, defaultName = "") { | ||
const getTypeName = (type, defaultName = "") => { | ||
if (!(typeof type === "object" && type !== null)) { | ||
@@ -240,3 +322,3 @@ return defaultName; | ||
return defaultName; | ||
} | ||
}; | ||
exports.getTypeName = getTypeName; | ||
@@ -311,16 +393,16 @@ /** | ||
*/ | ||
function getSchemaMap(schema) { | ||
const getSchemaMap = (schema) => { | ||
return { | ||
["queries"]: getOperation(schema?.getQueryType() ?? undefined), | ||
["mutations"]: getOperation(schema?.getMutationType() ?? undefined), | ||
["subscriptions"]: getOperation(schema?.getSubscriptionType() ?? undefined), | ||
["queries"]: (0, exports.getOperation)(schema?.getQueryType() ?? undefined), | ||
["mutations"]: (0, exports.getOperation)(schema?.getMutationType() ?? undefined), | ||
["subscriptions"]: (0, exports.getOperation)(schema?.getSubscriptionType() ?? undefined), | ||
["directives"]: (0, utils_1.convertArrayToMapObject)(schema?.getDirectives()), | ||
["objects"]: getTypeFromSchema(schema, graphql_1.GraphQLObjectType), | ||
["unions"]: getTypeFromSchema(schema, graphql_1.GraphQLUnionType), | ||
["interfaces"]: getTypeFromSchema(schema, graphql_1.GraphQLInterfaceType), | ||
["enums"]: getTypeFromSchema(schema, graphql_1.GraphQLEnumType), | ||
["inputs"]: getTypeFromSchema(schema, graphql_1.GraphQLInputObjectType), | ||
["scalars"]: getTypeFromSchema(schema, graphql_1.GraphQLScalarType), | ||
["objects"]: (0, exports.getTypeFromSchema)(schema, graphql_1.GraphQLObjectType), | ||
["unions"]: (0, exports.getTypeFromSchema)(schema, graphql_1.GraphQLUnionType), | ||
["interfaces"]: (0, exports.getTypeFromSchema)(schema, graphql_1.GraphQLInterfaceType), | ||
["enums"]: (0, exports.getTypeFromSchema)(schema, graphql_1.GraphQLEnumType), | ||
["inputs"]: (0, exports.getTypeFromSchema)(schema, graphql_1.GraphQLInputObjectType), | ||
["scalars"]: (0, exports.getTypeFromSchema)(schema, graphql_1.GraphQLScalarType), | ||
}; | ||
} | ||
}; | ||
exports.getSchemaMap = getSchemaMap; |
@@ -28,3 +28,3 @@ /** | ||
*/ | ||
export declare function loadSchema(schemaLocation: string, options: LoadSchemaOptions & { | ||
export declare const loadSchema: (schemaLocation: string, options: LoadSchemaOptions & { | ||
/** | ||
@@ -35,3 +35,3 @@ * @param rootTypes - optional `rootTypes` schema override | ||
rootTypes?: Partial<Record<OperationTypeNode, string>>; | ||
}): Promise<GraphQLSchema>; | ||
}) => Promise<GraphQLSchema>; | ||
/** | ||
@@ -63,2 +63,2 @@ * Asynchronously returns a valid loaders list for {@link loadSchema} based on the plugin config. | ||
*/ | ||
export declare function getDocumentLoaders(loadersList: Maybe<LoaderOption>): Promise<Maybe<LoadSchemaOptions>>; | ||
export declare const getDocumentLoaders: (loadersList: Maybe<LoaderOption>) => Promise<Maybe<LoadSchemaOptions>>; |
@@ -54,3 +54,3 @@ "use strict"; | ||
*/ | ||
async function loadSchema(schemaLocation, options) { | ||
const loadSchema = async (schemaLocation, options) => { | ||
let rootTypes = undefined; | ||
@@ -72,3 +72,3 @@ if (typeof options !== "undefined" && "rootTypes" in options) { | ||
return new graphql_1.GraphQLSchema(config); | ||
} | ||
}; | ||
exports.loadSchema = loadSchema; | ||
@@ -101,3 +101,3 @@ /** | ||
*/ | ||
async function getDocumentLoaders(loadersList) { | ||
const getDocumentLoaders = async (loadersList) => { | ||
const loaders = []; | ||
@@ -126,3 +126,3 @@ const loaderOptions = {}; | ||
return { ...loaderOptions, loaders }; | ||
} | ||
}; | ||
exports.getDocumentLoaders = getDocumentLoaders; |
@@ -29,3 +29,3 @@ "use strict"; | ||
*/ | ||
function mapRelationOf(type, relations, schemaMap, callback) { | ||
const mapRelationOf = (type, relations, schemaMap, callback) => { | ||
if (!schemaMap) { | ||
@@ -46,3 +46,3 @@ return {}; | ||
return relations; | ||
} | ||
}; | ||
/** | ||
@@ -74,6 +74,8 @@ * Returns a map of operations (queries, mutations, subscriptions) where the GraphQL schema type is the return type. | ||
type.name) { | ||
if (!results.find((r) => typeof r === "object" && | ||
r !== null && | ||
"name" in r && | ||
r.name === relationName)) { | ||
if (!results.find((r) => { | ||
return (typeof r === "object" && | ||
r !== null && | ||
"name" in r && | ||
r.name === relationName); | ||
})) { | ||
results.push(relationType); | ||
@@ -119,3 +121,3 @@ } | ||
: {}; | ||
const fieldMap = (0, introspection_1.__getFields)(relationType, undefined, {}); | ||
const fieldMap = (0, introspection_1._getFields)(relationType, undefined, {}); | ||
const fields = Object.assign({}, paramFieldArgs, fieldMap); | ||
@@ -125,4 +127,6 @@ for (const fieldDef of Object.values(fields)) { | ||
(0, graphql_1.getNamedType)(fieldDef.type)?.name === type.name) { | ||
if (!results.find((r) => r.toString() === key || | ||
(typeof r === "object" && "name" in r && r.name === key))) { | ||
if (!results.find((r) => { | ||
return (String(r) === key || | ||
(typeof r === "object" && "name" in r && r.name === key)); | ||
})) { | ||
results.push(relationType); | ||
@@ -158,4 +162,8 @@ } | ||
(0, graphql_1.isUnionType)(relationType) && | ||
relationType.getTypes().find((subType) => subType.name === type.name)) { | ||
if (!results.find((r) => typeof r === "object" && "name" in r && r.name === relationName)) { | ||
relationType.getTypes().find((subType) => { | ||
return subType.name === type.name; | ||
})) { | ||
if (!results.find((r) => { | ||
return (typeof r === "object" && "name" in r && r.name === relationName); | ||
})) { | ||
results.push(relationType); | ||
@@ -191,4 +199,8 @@ } | ||
((0, graphql_1.isObjectType)(relationType) || (0, graphql_1.isInterfaceType)(relationType)) && | ||
relationType.getInterfaces().find((subType) => subType.name === type.name)) { | ||
if (!results.find((r) => typeof r === "object" && "name" in r && r.name === relationName)) { | ||
relationType.getInterfaces().find((subType) => { | ||
return subType.name === type.name; | ||
})) { | ||
if (!results.find((r) => { | ||
return (typeof r === "object" && "name" in r && r.name === relationName); | ||
})) { | ||
results.push(relationType); | ||
@@ -195,0 +207,0 @@ } |
@@ -8,7 +8,7 @@ { | ||
}, | ||
"version": "1.0.0-next.0", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"repository": { | ||
"type": "git", | ||
"url": "graphql-markdown/graphql-markdown", | ||
"url": "git+https://github.com/graphql-markdown/graphql-markdown.git", | ||
"directory": "packages/graphql" | ||
@@ -60,6 +60,6 @@ }, | ||
"dependencies": { | ||
"@graphql-markdown/utils": "^1.6.0-next.0" | ||
"@graphql-markdown/utils": "^1.6.0" | ||
}, | ||
"devDependencies": { | ||
"@graphql-markdown/types": "^1.0.0-next.0" | ||
"@graphql-markdown/types": "^1.0.0" | ||
}, | ||
@@ -66,0 +66,0 @@ "directories": { |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
73034
7.2%2028
6.12%0
-100%