gql-types-generator
Advanced tools
Comparing version 1.3.1 to 1.4.0
@@ -140,7 +140,7 @@ "use strict"; | ||
schemaDefinition = types.reduce(function (acc, type) { | ||
// We parse only types used in schema. We can meet internal types. Internal | ||
// types dont have astNode | ||
// We parse only types defined in schema. We can meet internal types. | ||
// Internal types dont have astNode | ||
var parsed = utils_1.parseNamedType(type); | ||
if (parsed) { | ||
acc.push(utils_1.generateTSTypeDefinition(parsed, fileName, scalars)); | ||
acc.push(utils_1.generateTSTypeDefinition(parsed, scalars)); | ||
} | ||
@@ -178,3 +178,3 @@ return acc; | ||
var name = node.name, operation = node.operation; | ||
var ts = utils_1.generateGQLOperation(utils_1.parseOperationDefinitionNode(node, schema, operations), schemaFileName, !singleFile, wrapWithTag); | ||
var ts = utils_1.generateOperation(utils_1.parseOperationDefinitionNode(node, schema, operations), schemaFileName, wrapWithTag); | ||
acc.push({ operationName: name.value + utils_1.toCamelCase(operation), ts: ts }); | ||
@@ -181,0 +181,0 @@ } |
@@ -12,3 +12,3 @@ /** | ||
*/ | ||
export interface Scalars { | ||
export interface ScalarsMap { | ||
[name: string]: string | number; | ||
@@ -51,3 +51,3 @@ } | ||
operationsWrap?: boolean; | ||
scalars?: Scalars; | ||
scalars?: ScalarsMap; | ||
} | ||
@@ -70,3 +70,3 @@ /** | ||
removeDescription?: boolean; | ||
scalars?: Scalars; | ||
scalars?: ScalarsMap; | ||
} | ||
@@ -73,0 +73,0 @@ /** |
export * from './compilation'; | ||
export * from './gql'; | ||
export * from './schema'; | ||
export * from './parsing'; | ||
export * from './shared'; |
@@ -21,5 +21,19 @@ /** | ||
export interface MaybeDescription { | ||
description: string | null; | ||
description?: string; | ||
} | ||
/** | ||
* Adds import types field | ||
*/ | ||
export declare type WithImportTypes<Required extends boolean = false> = Required extends true ? { | ||
importTypes: string[]; | ||
} : { | ||
importTypes?: string[]; | ||
}; | ||
/** | ||
* Adds field name as required | ||
*/ | ||
export interface Named { | ||
name: string; | ||
} | ||
/** | ||
* Map of internal types of GQL with compiled TS types. States | ||
@@ -32,5 +46,5 @@ * which internal type converts to which TS type | ||
*/ | ||
export interface DefinitionWithRequiredTypes { | ||
requiredTypes: string[]; | ||
export interface DefinitionWithImportTypes { | ||
importTypes: string[]; | ||
definition: string; | ||
} |
@@ -1,54 +0,67 @@ | ||
import { ParsedGQLEnumType, ParsedGQLOperationDefinitionNode, ParsedGQLScalarType, ParsedGQLType, ParsedGQLObjectType, ParsedGQLInterfaceType, ParsedGQLUnionType, Scalars, ParsedGQLVariableDefinitions } from '../types'; | ||
import { ScalarsMap, Union, Enum, Entity, PreparedObject, Operation, PreparedObjectField, OperationRootNamespace, OperationNamespaceField, NamedGQLType, Scalar } from '../types'; | ||
/** | ||
* Universal TS definition generator | ||
* @param {ParsedGQLType} parsedType | ||
* @param schemaFileName | ||
* @param type | ||
* @param scalars | ||
* @returns {string} | ||
*/ | ||
export declare function generateTSTypeDefinition(parsedType: ParsedGQLType, schemaFileName: string, scalars: Scalars): string; | ||
export declare function generateTSTypeDefinition(type: NamedGQLType, scalars: ScalarsMap): string; | ||
/** | ||
* GQL interface or type => TS interface | ||
* @param {ParsedGQLObjectType | ParsedGQLInterfaceType} parsedType | ||
* @param schemaFileName | ||
* @param importsRequired | ||
* Converts prepared object field to string | ||
* @param {PreparedObjectField} field | ||
* @param includeDescription | ||
* @returns {string} | ||
*/ | ||
export declare function generateGQLInterface(parsedType: ParsedGQLObjectType | ParsedGQLInterfaceType, schemaFileName: string, importsRequired?: boolean): string; | ||
export declare function generatePreparedObjectField(field: PreparedObjectField, includeDescription?: boolean): string; | ||
/** | ||
* Converts prepared object to string | ||
* @param {PreparedObject} obj | ||
* @param includeDescription | ||
* @returns {string} | ||
*/ | ||
export declare function generatePreparedObject(obj: PreparedObject, includeDescription?: boolean): string; | ||
/** | ||
* GQL entity => TS interface + namespace? | ||
* @param {Entity} entity | ||
* @returns {string} | ||
*/ | ||
export declare function generateEntity(entity: Entity): string; | ||
/** | ||
* GQL enum => TS enum | ||
* @param {ParsedGQLEnumType} parsedType | ||
* @returns {string} | ||
* @param type | ||
*/ | ||
export declare function generateGQLEnum(parsedType: ParsedGQLEnumType): string; | ||
export declare function generateEnum(type: Enum): string; | ||
/** | ||
* GQL scalar => TS type | ||
* @param {ParsedGQLScalarType} parsedType | ||
* @param type | ||
* @param scalars | ||
* @returns {string} | ||
*/ | ||
export declare function generateGQLScalar(parsedType: ParsedGQLScalarType, scalars: Scalars): string; | ||
export declare function generateScalar(type: Scalar, scalars: ScalarsMap): string; | ||
/** | ||
* GQL union => TS type | ||
* @param {ParsedGQLUnionType} parsedType | ||
* @param schemaFileName | ||
* @returns {string} | ||
* @param type | ||
*/ | ||
export declare function generateGQLUnion(parsedType: ParsedGQLUnionType, schemaFileName: string): string; | ||
export declare function generateUnion(type: Union): string; | ||
/** | ||
* GQL variables => TS interface | ||
* @param {ParsedGQLVariableDefinitions} variables | ||
* @param {string} schemaFileName | ||
* @param {boolean} importsRequired | ||
* Generates operation namespace field | ||
* @param {OperationNamespaceField} field | ||
* @returns {string} | ||
*/ | ||
export declare function generateGQLOperationVariables(variables: ParsedGQLVariableDefinitions, schemaFileName: string, importsRequired?: boolean): string; | ||
export declare function generateOperationNamespaceField(field: OperationNamespaceField): string; | ||
/** | ||
* Generates operation root namespace | ||
* @param {OperationRootNamespace} nsp | ||
* @returns {string} | ||
*/ | ||
export declare function generateOperationRootNamespace(nsp: OperationRootNamespace): string; | ||
/** | ||
* GQL operation => TS interfaces | ||
* @returns {string} | ||
* @param parsedType | ||
* @param operation | ||
* @param schemaFileName | ||
* @param defaultExport | ||
* @param wrapWithTag | ||
*/ | ||
export declare function generateGQLOperation(parsedType: ParsedGQLOperationDefinitionNode, schemaFileName: string, defaultExport: boolean, wrapWithTag: boolean): string; | ||
export declare function generateOperation(operation: Operation, schemaFileName: string, wrapWithTag: boolean): string; |
"use strict"; | ||
var __spreadArrays = (this && this.__spreadArrays) || function () { | ||
for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length; | ||
for (var r = Array(s), k = 0, i = 0; i < il; i++) | ||
for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++) | ||
r[k] = a[j]; | ||
return r; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -13,75 +6,106 @@ var misc_1 = require("./misc"); | ||
* Universal TS definition generator | ||
* @param {ParsedGQLType} parsedType | ||
* @param schemaFileName | ||
* @param type | ||
* @param scalars | ||
* @returns {string} | ||
*/ | ||
function generateTSTypeDefinition(parsedType, schemaFileName, scalars) { | ||
if ('values' in parsedType) { | ||
return generateGQLEnum(parsedType); | ||
function generateTSTypeDefinition(type, scalars) { | ||
if (type.__type === 'union') { | ||
return generateUnion(type); | ||
} | ||
if ('types' in parsedType) { | ||
return generateGQLUnion(parsedType, schemaFileName); | ||
if (type.__type === 'scalar') { | ||
return generateScalar(type, scalars); | ||
} | ||
if ('fields' in parsedType) { | ||
return generateGQLInterface(parsedType, schemaFileName); | ||
if (type.__type === 'enum') { | ||
return generateEnum(type); | ||
} | ||
return generateGQLScalar(parsedType, scalars); | ||
return generateEntity(type); | ||
} | ||
exports.generateTSTypeDefinition = generateTSTypeDefinition; | ||
/** | ||
* GQL interface or type => TS interface | ||
* @param {ParsedGQLObjectType | ParsedGQLInterfaceType} parsedType | ||
* @param schemaFileName | ||
* @param importsRequired | ||
* Converts prepared object field to string | ||
* @param {PreparedObjectField} field | ||
* @param includeDescription | ||
* @returns {string} | ||
*/ | ||
function generateGQLInterface(parsedType, schemaFileName, importsRequired) { | ||
if (importsRequired === void 0) { importsRequired = false; } | ||
var name = parsedType.name, description = parsedType.description, fields = parsedType.fields; | ||
var _a = __spreadArrays(fields).reduce(function (acc, f) { | ||
var definition = f.definition, description = f.description, name = f.name, requiredTypes = f.requiredTypes; | ||
var fullDefinition = misc_1.formatDescription(description, 2) | ||
+ (" " + name + ": " + definition + ";\n"); | ||
acc.definition += fullDefinition; | ||
if (importsRequired) { | ||
for (var _i = 0, requiredTypes_1 = requiredTypes; _i < requiredTypes_1.length; _i++) { | ||
var type = requiredTypes_1[_i]; | ||
if (!acc.requiredTypes.includes(type)) { | ||
acc.requiredTypes.push(type); | ||
} | ||
} | ||
function generatePreparedObjectField(field, includeDescription) { | ||
if (includeDescription === void 0) { includeDescription = false; } | ||
var name = field.name, description = field.description, type = field.type; | ||
return (includeDescription ? misc_1.formatDescription(description) : '') | ||
+ (name + ": " + type + ";\n"); | ||
} | ||
exports.generatePreparedObjectField = generatePreparedObjectField; | ||
/** | ||
* Converts prepared object to string | ||
* @param {PreparedObject} obj | ||
* @param includeDescription | ||
* @returns {string} | ||
*/ | ||
function generatePreparedObject(obj, includeDescription) { | ||
if (includeDescription === void 0) { includeDescription = false; } | ||
var name = obj.name, description = obj.description, fields = obj.fields; | ||
var definition = fields.reduce(function (acc, f) { | ||
return acc + generatePreparedObjectField(f, false); | ||
}, ''); | ||
return (includeDescription ? misc_1.formatDescription(description) : '') | ||
+ ("export interface " + misc_1.toCamelCase(name) + " {\n") | ||
+ misc_1.withSpaces(definition, 2) | ||
+ '}\n'; | ||
} | ||
exports.generatePreparedObject = generatePreparedObject; | ||
/** | ||
* GQL entity => TS interface + namespace? | ||
* @param {Entity} entity | ||
* @returns {string} | ||
*/ | ||
function generateEntity(entity) { | ||
var namespace = entity.namespace, fields = entity.fields; | ||
var name = namespace.name, description = namespace.description, namespaceFields = namespace.fields; | ||
var nspDefinition = namespaceFields.reduce(function (acc, f, idx) { | ||
if (idx !== 0) { | ||
acc += '\n'; | ||
} | ||
acc += misc_1.formatDescription(f.description) | ||
+ ("export type " + f.name + " = " + f.type + ";\n"); | ||
if (f.args !== null && f.args.fields.length > 0) { | ||
var argsDefinition = generatePreparedObject(f.args); | ||
acc += "export namespace " + f.name + " {\n" | ||
+ misc_1.withSpaces(argsDefinition, 2) | ||
+ '\n}\n'; | ||
} | ||
return acc; | ||
}, { definition: '\n', requiredTypes: [] }), definition = _a.definition, requiredTypes = _a.requiredTypes; | ||
return misc_1.formatRequiredTypes(requiredTypes, schemaFileName) | ||
+ misc_1.formatDescription(description) | ||
+ ("export interface " + name + " {" + definition + "}"); | ||
}, ''); | ||
return misc_1.formatDescription(description) | ||
+ ("export namespace " + misc_1.toCamelCase(name) + " {\n") | ||
+ misc_1.withSpaces(nspDefinition, 2) | ||
+ "\n}\n\n" | ||
+ generatePreparedObject(fields, false); | ||
} | ||
exports.generateGQLInterface = generateGQLInterface; | ||
exports.generateEntity = generateEntity; | ||
/** | ||
* GQL enum => TS enum | ||
* @param {ParsedGQLEnumType} parsedType | ||
* @returns {string} | ||
* @param type | ||
*/ | ||
function generateGQLEnum(parsedType) { | ||
var name = parsedType.name, description = parsedType.description, values = parsedType.values; | ||
var content = values.reduce(function (acc, v) { | ||
var description = v.description, value = v.value; | ||
function generateEnum(type) { | ||
var name = type.name, description = type.description, values = type.values; | ||
var definition = values.reduce(function (acc, v) { | ||
var description = v.description, name = v.name; | ||
return acc | ||
+ misc_1.formatDescription(description, 2) | ||
+ (" " + value + " = \"" + value + "\",\n"); | ||
}, '\n'); | ||
+ misc_1.formatDescription(description) | ||
+ (name + " = '" + name + "',\n"); | ||
}, ''); | ||
return misc_1.formatDescription(description) | ||
+ ("export enum " + name + " {" + content + "}"); | ||
+ ("export enum " + name + " {\n") | ||
+ misc_1.withSpaces(definition, 2) | ||
+ '}\n'; | ||
} | ||
exports.generateGQLEnum = generateGQLEnum; | ||
exports.generateEnum = generateEnum; | ||
/** | ||
* GQL scalar => TS type | ||
* @param {ParsedGQLScalarType} parsedType | ||
* @param type | ||
* @param scalars | ||
* @returns {string} | ||
*/ | ||
function generateGQLScalar(parsedType, scalars) { | ||
var description = parsedType.description, name = parsedType.name; | ||
function generateScalar(type, scalars) { | ||
var description = type.description, name = type.name; | ||
var definition = 'any'; | ||
@@ -95,61 +119,72 @@ if (name in scalars) { | ||
} | ||
return misc_1.formatDescription(description) + ("export type " + name + " = " + definition + ";"); | ||
return misc_1.formatDescription(description) | ||
+ ("export type " + name + " = " + definition + ";\n"); | ||
} | ||
exports.generateGQLScalar = generateGQLScalar; | ||
exports.generateScalar = generateScalar; | ||
/** | ||
* GQL union => TS type | ||
* @param {ParsedGQLUnionType} parsedType | ||
* @param schemaFileName | ||
* @returns {string} | ||
* @param type | ||
*/ | ||
function generateGQLUnion(parsedType, schemaFileName) { | ||
var description = parsedType.description, name = parsedType.name, types = parsedType.types, requiredTypes = parsedType.requiredTypes; | ||
return misc_1.formatRequiredTypes(requiredTypes, schemaFileName) | ||
+ misc_1.formatDescription(description) | ||
+ ("export type " + name + " = " + types.join(' | ') + ";"); | ||
function generateUnion(type) { | ||
var description = type.description, name = type.name, types = type.types; | ||
return misc_1.formatDescription(description) | ||
+ ("export type " + name + " = " + types.join(' | ') + ";\n"); | ||
} | ||
exports.generateGQLUnion = generateGQLUnion; | ||
exports.generateUnion = generateUnion; | ||
/** | ||
* GQL variables => TS interface | ||
* @param {ParsedGQLVariableDefinitions} variables | ||
* @param {string} schemaFileName | ||
* @param {boolean} importsRequired | ||
* Generates operation namespace field | ||
* @param {OperationNamespaceField} field | ||
* @returns {string} | ||
*/ | ||
function generateGQLOperationVariables(variables, schemaFileName, importsRequired) { | ||
if (importsRequired === void 0) { importsRequired = false; } | ||
var name = variables.name, fields = variables.fields; | ||
var _a = __spreadArrays(fields).reduce(function (acc, f) { | ||
var definition = f.definition, name = f.name, requiredTypes = f.requiredTypes; | ||
acc.definition += " " + name + ": " + definition + ";\n"; | ||
if (importsRequired) { | ||
for (var _i = 0, requiredTypes_2 = requiredTypes; _i < requiredTypes_2.length; _i++) { | ||
var type = requiredTypes_2[_i]; | ||
if (!acc.requiredTypes.includes(type)) { | ||
acc.requiredTypes.push(type); | ||
} | ||
} | ||
function generateOperationNamespaceField(field) { | ||
var type = field.type, description = field.description, name = field.name, fields = field.fields; | ||
var result = misc_1.formatDescription(description); | ||
if ('definition' in type) { | ||
result += misc_1.formatDescription(description) | ||
+ ("export type " + name + " = " + type.definition + ";\n"); | ||
} | ||
else { | ||
result += generatePreparedObject(type, true); | ||
if (fields && fields.length > 0) { | ||
var content = fields.reduce(function (acc, f) { | ||
return acc + generateOperationNamespaceField(f); | ||
}, ''); | ||
result += "export namespace " + misc_1.toCamelCase(name) + " {\n" | ||
+ misc_1.withSpaces(content, 2) | ||
+ '}\n'; | ||
} | ||
return acc; | ||
}, { definition: '\n', requiredTypes: [] }), definition = _a.definition, requiredTypes = _a.requiredTypes; | ||
return misc_1.formatRequiredTypes(requiredTypes, schemaFileName) | ||
+ ("export interface " + name + " {" + definition + "}"); | ||
} | ||
return result; | ||
} | ||
exports.generateGQLOperationVariables = generateGQLOperationVariables; | ||
exports.generateOperationNamespaceField = generateOperationNamespaceField; | ||
/** | ||
* Generates operation root namespace | ||
* @param {OperationRootNamespace} nsp | ||
* @returns {string} | ||
*/ | ||
function generateOperationRootNamespace(nsp) { | ||
var name = nsp.name, args = nsp.args, fields = nsp.fields; | ||
// Namespace | ||
var content = generatePreparedObject(args); | ||
fields.forEach(function (f) { | ||
content += generateOperationNamespaceField(f); | ||
}); | ||
return "export namespace " + name + " {\n" | ||
+ misc_1.withSpaces(content, 2) | ||
+ '}\n'; | ||
} | ||
exports.generateOperationRootNamespace = generateOperationRootNamespace; | ||
/** | ||
* GQL operation => TS interfaces | ||
* @returns {string} | ||
* @param parsedType | ||
* @param operation | ||
* @param schemaFileName | ||
* @param defaultExport | ||
* @param wrapWithTag | ||
*/ | ||
function generateGQLOperation(parsedType, schemaFileName, defaultExport, wrapWithTag) { | ||
var originalName = parsedType.originalName, operationType = parsedType.operationType, operationDefinition = parsedType.operationDefinition, variables = parsedType.variables, requiredTypes = parsedType.requiredTypes, operationSignature = parsedType.operationSignature; | ||
var operationName = misc_1.getCompiledOperationName(originalName, operationType); | ||
var operationStringName = originalName + misc_1.toCamelCase(operationType); | ||
var variablesDefinition = variables.fields.length === 0 ? '' : (generateGQLOperationVariables(variables, schemaFileName, true) + '\n\n'); | ||
function generateOperation(operation, schemaFileName, wrapWithTag) { | ||
var selection = operation.selection, namespace = operation.namespace, name = operation.name, signature = operation.signature, importTypes = operation.importTypes; | ||
var operationConst = wrapWithTag | ||
? "const " + operationStringName + ": DocumentNode = gql(`" + operationSignature + "`);\n" | ||
: "const " + operationStringName + ": string = `" + operationSignature + "`;\n"; | ||
? "const " + name + ": DocumentNode = gql(`" + signature + "`);\n" | ||
: "const " + name + ": string = `" + signature + "`;\n"; | ||
// If graphql-tag required, import it | ||
@@ -159,16 +194,14 @@ var gqlTagImport = ''; | ||
gqlTagImport = 'import gql from \'graphql-tag\';\n' | ||
+ 'import { DocumentNode } from \'graphql\';'; | ||
+ 'import { DocumentNode } from \'graphql\';\n\n'; | ||
} | ||
return gqlTagImport | ||
// Required types import | ||
+ misc_1.formatRequiredTypes(requiredTypes, schemaFileName) | ||
+ misc_1.formatImportTypes(importTypes, schemaFileName) | ||
// Namespace | ||
+ generateOperationRootNamespace(namespace) | ||
// Operation result interface | ||
+ ("export interface " + operationName + " " + operationDefinition + "\n\n") | ||
// Operation variables interface | ||
+ variablesDefinition | ||
+ generatePreparedObject(selection) | ||
// Operation export | ||
+ (defaultExport | ||
? (operationConst + ("export default " + operationStringName + ";")) | ||
: "export " + operationConst); | ||
+ ("export " + operationConst); | ||
} | ||
exports.generateGQLOperation = generateGQLOperation; | ||
exports.generateOperation = generateOperation; |
@@ -1,4 +0,17 @@ | ||
import { CompiledTypeName, GQLScalarType, DisplayType, GraphQLNonWrappedType, DefinitionWithRequiredTypes } from '../types'; | ||
import { CompiledTypeName, GQLScalarType, DisplayType, GraphQLNonWrappedType, DefinitionWithImportTypes } from '../types'; | ||
import { GraphQLField, GraphQLFieldConfig, GraphQLInputType, GraphQLNamedType, GraphQLObjectType, GraphQLOutputType, GraphQLSchema, OperationTypeNode, TypeNode } from 'graphql'; | ||
/** | ||
* Returns string with "count" spaces count | ||
* @param {number} count | ||
* @returns {string} | ||
*/ | ||
export declare function getSpaces(count: number): string; | ||
/** | ||
* Returns text with spaces before | ||
* @param {string} text | ||
* @param spacesCount | ||
* @returns {string} | ||
*/ | ||
export declare function withSpaces(text: string, spacesCount: number): string; | ||
/** | ||
* Returns formatted TS description | ||
@@ -9,3 +22,3 @@ * @param {string} description | ||
*/ | ||
export declare function formatDescription(description: string | null, spacesCount?: number): string; | ||
export declare function formatDescription(description: string | undefined, spacesCount?: number): string; | ||
/** | ||
@@ -32,24 +45,16 @@ * States if value is GQL scalar type | ||
* @param {TypeNode} node | ||
* @param requiredTypes | ||
* @param importTypes | ||
* @param {boolean} nullable | ||
* @returns {string} | ||
*/ | ||
export declare function getTypeNodeDefinition(node: TypeNode, requiredTypes?: string[], nullable?: boolean): DefinitionWithRequiredTypes; | ||
export declare function getTypeNodeDefinition(node: TypeNode, importTypes?: string[], nullable?: boolean): DefinitionWithImportTypes; | ||
/** | ||
* Recursively gets type definition getting deeper into types tree | ||
* @param {GraphQLOutputType} type | ||
* @param requiredTypes | ||
* @param importTypes | ||
* @param {boolean} nullable | ||
* @returns {string} | ||
*/ | ||
export declare function getIOTypeDefinition(type: GraphQLOutputType | GraphQLInputType, requiredTypes?: string[], nullable?: boolean): DefinitionWithRequiredTypes; | ||
export declare function getIOTypeDefinition(type: GraphQLOutputType | GraphQLInputType, importTypes?: string[], nullable?: boolean): DefinitionWithImportTypes; | ||
/** | ||
* Returns met list and non-nullable wrappers | ||
* @param {GraphQLOutputType} type | ||
* @param definition | ||
* @param nullable | ||
* @returns {string} | ||
*/ | ||
export declare function getOutputTypeDefinitionWithWrappers(type: GraphQLOutputType, definition: string, nullable?: boolean): string; | ||
/** | ||
* Returns sorter depending on display type | ||
@@ -99,2 +104,2 @@ * @param {DisplayType} display | ||
*/ | ||
export declare function formatRequiredTypes(types: string[], schemaFileName: string): string; | ||
export declare function formatImportTypes(types: string[], schemaFileName: string): string; |
@@ -19,2 +19,25 @@ "use strict"; | ||
/** | ||
* Returns string with "count" spaces count | ||
* @param {number} count | ||
* @returns {string} | ||
*/ | ||
function getSpaces(count) { | ||
return new Array(count).fill(' ').join(''); | ||
} | ||
exports.getSpaces = getSpaces; | ||
/** | ||
* Returns text with spaces before | ||
* @param {string} text | ||
* @param spacesCount | ||
* @returns {string} | ||
*/ | ||
function withSpaces(text, spacesCount) { | ||
var spaces = getSpaces(spacesCount); | ||
return text | ||
.split('\n') | ||
.map(function (p) { return p.length === 0 ? p : spaces + p; }) | ||
.join('\n'); | ||
} | ||
exports.withSpaces = withSpaces; | ||
/** | ||
* Returns formatted TS description | ||
@@ -27,8 +50,6 @@ * @param {string} description | ||
if (spacesCount === void 0) { spacesCount = 0; } | ||
var before = new Array(spacesCount).fill(' ').join(''); | ||
return description | ||
? before + "/**\n" | ||
+ (before + " * " + description + "\n") | ||
+ (before + " */\n") | ||
: ''; | ||
if (!description) { | ||
return ''; | ||
} | ||
return withSpaces("/**\n * " + description + "\n */", spacesCount) + '\n'; | ||
} | ||
@@ -66,12 +87,12 @@ exports.formatDescription = formatDescription; | ||
* @param {TypeNode} node | ||
* @param requiredTypes | ||
* @param importTypes | ||
* @param {boolean} nullable | ||
* @returns {string} | ||
*/ | ||
function getTypeNodeDefinition(node, requiredTypes, nullable) { | ||
if (requiredTypes === void 0) { requiredTypes = []; } | ||
function getTypeNodeDefinition(node, importTypes, nullable) { | ||
if (importTypes === void 0) { importTypes = []; } | ||
if (nullable === void 0) { nullable = true; } | ||
switch (node.kind) { | ||
case 'NonNullType': | ||
return getTypeNodeDefinition(node.type, requiredTypes, false); | ||
return getTypeNodeDefinition(node.type, importTypes, false); | ||
case 'NamedType': | ||
@@ -83,11 +104,11 @@ case 'ListType': | ||
definition = transpileGQLTypeName(name); | ||
if (!isGQLScalarType(name) && !requiredTypes.includes(name)) { | ||
requiredTypes.push(name); | ||
if (!isGQLScalarType(name) && !importTypes.includes(name)) { | ||
importTypes.push(name); | ||
} | ||
} | ||
else { | ||
var _a = getTypeNodeDefinition(node.type, requiredTypes, true), _requiredTypes = _a.requiredTypes, _definition = _a.definition; | ||
_requiredTypes.forEach(function (t) { | ||
if (!isGQLScalarType(t) && !requiredTypes.includes(t)) { | ||
requiredTypes.push(t); | ||
var _a = getTypeNodeDefinition(node.type, importTypes, true), _importTypes = _a.importTypes, _definition = _a.definition; | ||
_importTypes.forEach(function (t) { | ||
if (!isGQLScalarType(t) && !importTypes.includes(t)) { | ||
importTypes.push(t); | ||
} | ||
@@ -100,3 +121,3 @@ }); | ||
} | ||
return { definition: definition, requiredTypes: requiredTypes }; | ||
return { definition: definition, importTypes: importTypes }; | ||
} | ||
@@ -108,18 +129,18 @@ } | ||
* @param {GraphQLOutputType} type | ||
* @param requiredTypes | ||
* @param importTypes | ||
* @param {boolean} nullable | ||
* @returns {string} | ||
*/ | ||
function getIOTypeDefinition(type, requiredTypes, nullable) { | ||
if (requiredTypes === void 0) { requiredTypes = []; } | ||
function getIOTypeDefinition(type, importTypes, nullable) { | ||
if (importTypes === void 0) { importTypes = []; } | ||
if (nullable === void 0) { nullable = true; } | ||
if (graphql_1.isNonNullType(type)) { | ||
return getIOTypeDefinition(type.ofType, requiredTypes, false); | ||
return getIOTypeDefinition(type.ofType, importTypes, false); | ||
} | ||
var definition = ''; | ||
if (graphql_1.isListType(type)) { | ||
var _a = getIOTypeDefinition(type.ofType, requiredTypes, true), _requiredTypes = _a.requiredTypes, _definition = _a.definition; | ||
_requiredTypes.forEach(function (t) { | ||
if (!isGQLScalarType(t) && !requiredTypes.includes(t)) { | ||
requiredTypes.push(t); | ||
var _a = getIOTypeDefinition(type.ofType, importTypes, true), _importTypes = _a.importTypes, _definition = _a.definition; | ||
_importTypes.forEach(function (t) { | ||
if (!isGQLScalarType(t) && !importTypes.includes(t)) { | ||
importTypes.push(t); | ||
} | ||
@@ -132,4 +153,4 @@ }); | ||
definition = transpileGQLTypeName(name); | ||
if (!isGQLScalarType(name) && !requiredTypes.includes(name)) { | ||
requiredTypes.push(name); | ||
if (!isGQLScalarType(name) && !importTypes.includes(name)) { | ||
importTypes.push(name); | ||
} | ||
@@ -140,25 +161,6 @@ } | ||
} | ||
return { definition: definition, requiredTypes: requiredTypes }; | ||
return { definition: definition, importTypes: importTypes }; | ||
} | ||
exports.getIOTypeDefinition = getIOTypeDefinition; | ||
/** | ||
* Returns met list and non-nullable wrappers | ||
* @param {GraphQLOutputType} type | ||
* @param definition | ||
* @param nullable | ||
* @returns {string} | ||
*/ | ||
function getOutputTypeDefinitionWithWrappers(type, definition, nullable) { | ||
if (nullable === void 0) { nullable = true; } | ||
if (graphql_1.isNonNullType(type)) { | ||
return getOutputTypeDefinitionWithWrappers(type.ofType, definition, false); | ||
} | ||
var def = definition; | ||
if (graphql_1.isListType(type)) { | ||
def = def + "[]"; | ||
} | ||
return nullable ? makeNullable(def) : def; | ||
} | ||
exports.getOutputTypeDefinitionWithWrappers = getOutputTypeDefinitionWithWrappers; | ||
/** | ||
* Returns sorter depending on display type | ||
@@ -280,3 +282,3 @@ * @param {DisplayType} display | ||
*/ | ||
function formatRequiredTypes(types, schemaFileName) { | ||
function formatImportTypes(types, schemaFileName) { | ||
var schemaName = fs_1.getFileName(schemaFileName); | ||
@@ -287,2 +289,2 @@ return types.length === 0 | ||
} | ||
exports.formatRequiredTypes = formatRequiredTypes; | ||
exports.formatImportTypes = formatImportTypes; |
@@ -1,63 +0,64 @@ | ||
import { ParsedGQLEnumType, ParsedGQLScalarType, ParsedGQLUnionType, ParsedGQLType, ParsedGQLOperationDefinitionNode, DefinitionWithRequiredTypes, ParsedGQLInputObjectType, ParsedGQLObjectType, ParsedGQLVariableDefinitions, ParsedGQLInterfaceType } from '../types'; | ||
import { GraphQLEnumType, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLNamedType, GraphQLObjectType, GraphQLScalarType, GraphQLSchema, GraphQLUnionType, OperationDefinitionNode, SelectionSetNode, VariableDefinitionNode } from 'graphql'; | ||
import { Enum, Scalar, Union, Entity, PreparedObject, Operation, PreparedObjectField, OperationNamespaceField, OperationRootNamespace, NamedGQLType } from '../types'; | ||
import { FieldNode, GraphQLEnumType, GraphQLInputObjectType, GraphQLInterfaceType, GraphQLNamedType, GraphQLObjectType, GraphQLScalarType, GraphQLSchema, GraphQLUnionType, OperationDefinitionNode, SelectionSetNode, VariableDefinitionNode } from 'graphql'; | ||
/** | ||
* Universal TypeDefinition parser | ||
* @returns {ParsedGQLType} | ||
* Universal GraphQLNamedType parser. Skips GraphQL internal types like Boolean, | ||
* Int, Float and other | ||
* @returns {NamedGQLType} | ||
* @param type | ||
*/ | ||
export declare function parseNamedType(type: GraphQLNamedType): ParsedGQLType | null; | ||
export declare function parseNamedType(type: GraphQLNamedType): NamedGQLType | null; | ||
/** | ||
* Parses GQL enum type | ||
* @returns {ParsedGQLEnumType} | ||
* Parses GraphQLEnumType | ||
* @returns {Enum} | ||
* @param type | ||
*/ | ||
export declare function parseEnumType(type: GraphQLEnumType): ParsedGQLEnumType; | ||
export declare function parseEnumType(type: GraphQLEnumType): Enum; | ||
/** | ||
* Parses GQL interface type | ||
* @param {GraphQLInterfaceType} type | ||
* @returns {ParsedGQLInterfaceType} | ||
*/ | ||
export declare function parseInterfaceType(type: GraphQLInterfaceType): ParsedGQLInterfaceType; | ||
/** | ||
* Parses GQL object type | ||
* @returns {ParsedGQLType} | ||
* Parses GraphQLInterfaceType and GraphQLObjectType | ||
* @returns {Entity} | ||
* @param type | ||
*/ | ||
export declare function parseObjectType(type: GraphQLObjectType): ParsedGQLObjectType; | ||
export declare function parseObjectOrInterfaceType(type: GraphQLInterfaceType | GraphQLObjectType): Entity; | ||
/** | ||
* Parses GQL input object type | ||
* @returns {ParsedGQLInputObjectType} | ||
* @param type | ||
* Parses GraphQLInputObjectType | ||
* @param {GraphQLInputObjectType} type | ||
* @returns {Entity} | ||
*/ | ||
export declare function parseInputObjectType(type: GraphQLInputObjectType): ParsedGQLInputObjectType; | ||
export declare function parseInputObjectType(type: GraphQLInputObjectType): Entity; | ||
/** | ||
* Parses GQL scalar type | ||
* @returns {ParsedGQLScalarType} | ||
* Parses GraphQLScalarType | ||
* @returns {Scalar} | ||
* @param type | ||
*/ | ||
export declare function parseScalarType(type: GraphQLScalarType): ParsedGQLScalarType; | ||
export declare function parseScalarType(type: GraphQLScalarType): Scalar; | ||
/** | ||
* Parses GQL union type | ||
* @returns {ParsedGQLUnionType} | ||
* Parses GraphQLUnionType | ||
* @returns {Union} | ||
* @param type | ||
*/ | ||
export declare function parseUnionType(type: GraphQLUnionType): ParsedGQLUnionType; | ||
export declare function parseUnionType(type: GraphQLUnionType): Union; | ||
/** | ||
* Parses GQL operation variables | ||
* @param {string} compiledName | ||
* @param {VariableDefinitionNode[]} nodes | ||
* @returns {ParsedGQLVariableDefinitions} | ||
* @returns {PreparedObject<true>} | ||
*/ | ||
export declare function parseOperationVariableDefinitions(compiledName: string, nodes: VariableDefinitionNode[]): ParsedGQLVariableDefinitions; | ||
export declare function parseOperationVariableDefinitions(nodes: VariableDefinitionNode[]): PreparedObject<true>; | ||
/** | ||
* Gets selection's set required types and definition | ||
* @param {GraphQLObjectType} rootNode | ||
* @param {GraphQLSchema} schema | ||
* @param {SelectionSetNode} selectionSet | ||
* @param {string} operationFieldPath | ||
* @param {number} spacesCount | ||
* @param nspName | ||
* @returns {string} | ||
*/ | ||
export declare function parseSelectionSet(rootNode: GraphQLObjectType, schema: GraphQLSchema, selectionSet: SelectionSetNode, operationFieldPath?: string, spacesCount?: number): DefinitionWithRequiredTypes; | ||
export declare function selectionSetToObjectFields(selectionSet: SelectionSetNode, nspName: string): PreparedObjectField[]; | ||
/** | ||
* Converts selection set to namespace fields | ||
* @param {SelectionSetNode} selectionSet | ||
* @param rootNode | ||
* @param path | ||
* @returns {OperationNamespaceField[]} | ||
*/ | ||
export declare function selectionSetToNamespaceFields(selectionSet: SelectionSetNode, rootNode: GraphQLObjectType, path?: string): OperationNamespaceField[]; | ||
export declare function selectionSetToRootNamespace(selectionSet: SelectionSetNode, compiledName: string, args: PreparedObject, rootNode: GraphQLObjectType): OperationRootNamespace; | ||
export declare function parseFieldNode(node: FieldNode, rootNode: GraphQLObjectType, prevPath: string): OperationNamespaceField; | ||
/** | ||
* Parses GQL operation | ||
@@ -67,4 +68,4 @@ * @param {OperationDefinitionNode} node | ||
* @param operationsString | ||
* @returns {ParsedGQLOperationDefinitionNode} | ||
* @returns {Operation} | ||
*/ | ||
export declare function parseOperationDefinitionNode(node: OperationDefinitionNode, schema: GraphQLSchema, operationsString: string): ParsedGQLOperationDefinitionNode; | ||
export declare function parseOperationDefinitionNode(node: OperationDefinitionNode, schema: GraphQLSchema, operationsString: string): Operation; |
@@ -13,4 +13,5 @@ "use strict"; | ||
/** | ||
* Universal TypeDefinition parser | ||
* @returns {ParsedGQLType} | ||
* Universal GraphQLNamedType parser. Skips GraphQL internal types like Boolean, | ||
* Int, Float and other | ||
* @returns {NamedGQLType} | ||
* @param type | ||
@@ -31,14 +32,11 @@ */ | ||
} | ||
else if (graphql_1.isObjectType(type)) { | ||
return parseObjectType(type); | ||
else if (graphql_1.isObjectType(type) || graphql_1.isInterfaceType(type)) { | ||
return parseObjectOrInterfaceType(type); | ||
} | ||
else if (graphql_1.isInputObjectType(type)) { | ||
return parseInputObjectType(type); | ||
} | ||
return parseInterfaceType(type); | ||
return parseInputObjectType(type); | ||
} | ||
exports.parseNamedType = parseNamedType; | ||
/** | ||
* Parses GQL enum type | ||
* @returns {ParsedGQLEnumType} | ||
* Parses GraphQLEnumType | ||
* @returns {Enum} | ||
* @param type | ||
@@ -48,71 +46,135 @@ */ | ||
var description = type.description, name = type.name; | ||
var values = type.getValues(); | ||
var parsedValues = values.map(function (v) { | ||
var description = v.description, name = v.name; | ||
return { description: description, value: name }; | ||
}, []); | ||
return { description: description, name: name, values: parsedValues }; | ||
return { | ||
__type: 'enum', | ||
name: name, | ||
description: description, | ||
values: type.getValues().map(function (_a) { | ||
var description = _a.description, name = _a.name; | ||
return ({ | ||
description: description, | ||
name: name, | ||
}); | ||
}), | ||
}; | ||
} | ||
exports.parseEnumType = parseEnumType; | ||
/** | ||
* Parses GQL interface type | ||
* @param {GraphQLInterfaceType} type | ||
* @returns {ParsedGQLInterfaceType} | ||
*/ | ||
function parseInterfaceType(type) { | ||
var description = type.description, name = type.name; | ||
var parsedFields = Object.values(type.getFields()) | ||
.map(function (f) { | ||
var type = f.type, description = f.description, name = f.name; | ||
var _a = misc_1.getIOTypeDefinition(type), definition = _a.definition, requiredTypes = _a.requiredTypes; | ||
return { definition: definition, description: description, name: name, requiredTypes: requiredTypes }; | ||
}, []); | ||
return { description: description, fields: parsedFields, name: name }; | ||
} | ||
exports.parseInterfaceType = parseInterfaceType; | ||
/** | ||
* Parses GQL object type | ||
* @returns {ParsedGQLType} | ||
* Parses GraphQLInterfaceType and GraphQLObjectType | ||
* @returns {Entity} | ||
* @param type | ||
*/ | ||
function parseObjectType(type) { | ||
function parseObjectOrInterfaceType(type) { | ||
var description = type.description, name = type.name; | ||
var parsedFields = Object.values(type.getFields()) | ||
.map(function (f) { | ||
var type = f.type, description = f.description, name = f.name; | ||
var _a = misc_1.getIOTypeDefinition(type), definition = _a.definition, requiredTypes = _a.requiredTypes; | ||
return { | ||
definition: definition, | ||
var fields = type.getFields(); | ||
var formattedName = misc_1.toCamelCase(name); | ||
return Object.values(fields).reduce(function (acc, f) { | ||
var fieldName = f.name, type = f.type, fieldDescription = f.description, args = f.args; | ||
var formattedFieldName = misc_1.toCamelCase(fieldName); | ||
// Fields | ||
acc.fields.fields.push({ | ||
name: fieldName, | ||
description: fieldDescription, | ||
type: formattedName + "." + formattedFieldName, | ||
}); | ||
// Arguments | ||
var preparedArguments = args.reduce(function (argAcc, arg) { | ||
var type = arg.type, name = arg.name, description = arg.description; | ||
var _a = misc_1.getIOTypeDefinition(type), definition = _a.definition, importTypes = _a.importTypes; | ||
argAcc.fields.push({ | ||
name: name, | ||
description: description, | ||
type: definition, | ||
}); | ||
importTypes.forEach(function (t) { | ||
if (!argAcc.importTypes.includes(t)) { | ||
argAcc.importTypes.push(t); | ||
} | ||
}); | ||
return argAcc; | ||
}, { | ||
name: 'Arguments', | ||
fields: [], | ||
importTypes: [], | ||
}); | ||
// Namespace | ||
var _a = misc_1.getIOTypeDefinition(type), definition = _a.definition, fImportTypes = _a.importTypes; | ||
// Add all collected required types | ||
__spreadArrays(fImportTypes, preparedArguments.importTypes).forEach(function (t) { | ||
if (!acc.importTypes.includes(t)) { | ||
acc.importTypes.push(t); | ||
} | ||
}); | ||
acc.namespace.fields.push({ | ||
name: formattedFieldName, | ||
description: fieldDescription, | ||
args: preparedArguments, | ||
type: definition, | ||
}); | ||
return acc; | ||
}, { | ||
__type: 'entity', | ||
fields: { | ||
name: formattedName, | ||
fields: [], | ||
}, | ||
namespace: { | ||
name: formattedName, | ||
description: description, | ||
name: name, | ||
requiredTypes: requiredTypes, | ||
}; | ||
}, []); | ||
return { description: description, fields: parsedFields, name: name }; | ||
fields: [], | ||
}, | ||
importTypes: [], | ||
}); | ||
} | ||
exports.parseObjectType = parseObjectType; | ||
exports.parseObjectOrInterfaceType = parseObjectOrInterfaceType; | ||
/** | ||
* Parses GQL input object type | ||
* @returns {ParsedGQLInputObjectType} | ||
* @param type | ||
* Parses GraphQLInputObjectType | ||
* @param {GraphQLInputObjectType} type | ||
* @returns {Entity} | ||
*/ | ||
function parseInputObjectType(type) { | ||
var description = type.description, name = type.name; | ||
var parsedFields = Object.values(type.getFields()) | ||
.map(function (f) { | ||
var type = f.type, description = f.description, name = f.name; | ||
var _a = misc_1.getIOTypeDefinition(type), definition = _a.definition, requiredTypes = _a.requiredTypes; | ||
return { | ||
definition: definition, | ||
var fields = type.getFields(); | ||
var formattedName = misc_1.toCamelCase(name); | ||
return Object.values(fields).reduce(function (acc, f) { | ||
var fieldName = f.name, type = f.type, fieldDescription = f.description; | ||
var formattedFieldName = misc_1.toCamelCase(fieldName); | ||
// Fields | ||
acc.fields.fields.push({ | ||
name: fieldName, | ||
description: fieldDescription, | ||
type: formattedName + "." + formattedFieldName, | ||
}); | ||
// Namespace | ||
var _a = misc_1.getIOTypeDefinition(type), definition = _a.definition, fImportTypes = _a.importTypes; | ||
// Add all collected required types | ||
fImportTypes.forEach(function (t) { | ||
if (!acc.importTypes.includes(t)) { | ||
acc.importTypes.push(t); | ||
} | ||
}); | ||
acc.namespace.fields.push({ | ||
name: formattedFieldName, | ||
description: fieldDescription, | ||
args: null, | ||
type: definition, | ||
}); | ||
return acc; | ||
}, { | ||
__type: 'entity', | ||
fields: { | ||
name: formattedName, | ||
fields: [], | ||
}, | ||
namespace: { | ||
name: formattedName, | ||
description: description, | ||
name: name, | ||
requiredTypes: requiredTypes, | ||
}; | ||
}, []); | ||
return { description: description, fields: parsedFields, name: name }; | ||
fields: [], | ||
}, | ||
importTypes: [], | ||
}); | ||
} | ||
exports.parseInputObjectType = parseInputObjectType; | ||
/** | ||
* Parses GQL scalar type | ||
* @returns {ParsedGQLScalarType} | ||
* Parses GraphQLScalarType | ||
* @returns {Scalar} | ||
* @param type | ||
@@ -122,21 +184,17 @@ */ | ||
var name = type.name, description = type.description; | ||
return { description: description, name: name }; | ||
return { __type: 'scalar', description: description, name: name }; | ||
} | ||
exports.parseScalarType = parseScalarType; | ||
/** | ||
* Parses GQL union type | ||
* @returns {ParsedGQLUnionType} | ||
* Parses GraphQLUnionType | ||
* @returns {Union} | ||
* @param type | ||
*/ | ||
function parseUnionType(type) { | ||
var name = type.name, description = type.description, getTypes = type.getTypes; | ||
var types = getTypes(); | ||
var requiredTypes = getTypes() | ||
.filter(function (t) { return !misc_1.isGQLScalarType(t.name); }) | ||
.map(function (t) { return t.name; }); | ||
var name = type.name, description = type.description; | ||
return { | ||
__type: 'union', | ||
name: name, | ||
description: description, | ||
requiredTypes: requiredTypes, | ||
types: types.map(function (t) { return misc_1.transpileGQLTypeName(t.name); }), | ||
types: type.getTypes().map(function (t) { return misc_1.transpileGQLTypeName(t.name); }), | ||
}; | ||
@@ -147,19 +205,24 @@ } | ||
* Parses GQL operation variables | ||
* @param {string} compiledName | ||
* @param {VariableDefinitionNode[]} nodes | ||
* @returns {ParsedGQLVariableDefinitions} | ||
* @returns {PreparedObject<true>} | ||
*/ | ||
function parseOperationVariableDefinitions(compiledName, nodes) { | ||
return { | ||
name: compiledName + "Variables", | ||
fields: nodes.map(function (n) { | ||
var variable = n.variable, type = n.type; | ||
var _a = misc_1.getTypeNodeDefinition(type), definition = _a.definition, requiredTypes = _a.requiredTypes; | ||
return { | ||
name: variable.name.value, | ||
definition: definition, | ||
requiredTypes: requiredTypes, | ||
}; | ||
}, {}), | ||
}; | ||
function parseOperationVariableDefinitions(nodes) { | ||
return nodes.reduce(function (acc, n) { | ||
var variable = n.variable, type = n.type; | ||
var _a = misc_1.getTypeNodeDefinition(type), definition = _a.definition, importTypes = _a.importTypes; | ||
acc.fields.push({ | ||
name: variable.name.value, | ||
type: definition, | ||
}); | ||
importTypes.forEach(function (t) { | ||
if (!acc.importTypes.includes(t)) { | ||
acc.importTypes.push(t); | ||
} | ||
}); | ||
return acc; | ||
}, { | ||
name: 'Arguments', | ||
fields: [], | ||
importTypes: [], | ||
}); | ||
} | ||
@@ -169,59 +232,88 @@ exports.parseOperationVariableDefinitions = parseOperationVariableDefinitions; | ||
* Gets selection's set required types and definition | ||
* @param {GraphQLObjectType} rootNode | ||
* @param {GraphQLSchema} schema | ||
* @param {SelectionSetNode} selectionSet | ||
* @param {string} operationFieldPath | ||
* @param {number} spacesCount | ||
* @param nspName | ||
* @returns {string} | ||
*/ | ||
function parseSelectionSet(rootNode, schema, selectionSet, operationFieldPath, spacesCount) { | ||
if (operationFieldPath === void 0) { operationFieldPath = ''; } | ||
if (spacesCount === void 0) { spacesCount = 0; } | ||
var bracketSpaces = new Array(spacesCount).fill(' ').join(''); | ||
var lineSpaces = bracketSpaces + new Array(2).fill(' ').join(''); | ||
return selectionSet | ||
.selections | ||
.reduce(function (acc, s, idx, arr) { | ||
// TODO: Fragments and inline fragments support | ||
function selectionSetToObjectFields(selectionSet, nspName) { | ||
return selectionSet.selections.reduce(function (acc, s) { | ||
// TODO: Fragments support | ||
if (s.kind === 'Field') { | ||
var name = s.name.value; | ||
var path = operationFieldPath.length === 0 | ||
? name | ||
: operationFieldPath + "." + name; | ||
var field = misc_1.getIn(rootNode, path); | ||
var description = field.description; | ||
var currentRequiredTypes = []; | ||
if (description) { | ||
acc.definition += misc_1.formatDescription(description, spacesCount + 2); | ||
} | ||
// Definition line start | ||
acc.definition += "" + lineSpaces + name + ": "; | ||
// Definition line content | ||
if (s.selectionSet) { | ||
var _a = parseSelectionSet(rootNode, schema, s.selectionSet, path, spacesCount + 2), definition = _a.definition, requiredTypes = _a.requiredTypes; | ||
currentRequiredTypes = requiredTypes; | ||
acc.definition += misc_1.getOutputTypeDefinitionWithWrappers(field.type, definition); | ||
} | ||
else { | ||
var _b = misc_1.getIOTypeDefinition(field.type), definition = _b.definition, requiredTypes = _b.requiredTypes; | ||
currentRequiredTypes = requiredTypes; | ||
acc.definition += definition; | ||
} | ||
// Definition line end | ||
acc.definition += ';\n'; | ||
// Add all required types if they were not added before | ||
currentRequiredTypes.forEach(function (t) { | ||
if (!acc.requiredTypes.includes(t)) { | ||
acc.requiredTypes.push(t); | ||
} | ||
acc.push({ | ||
name: s.name.value, | ||
type: nspName + "." + misc_1.toCamelCase(s.name.value), | ||
}); | ||
} | ||
// If it was last selection, close definition | ||
if (idx === arr.length - 1) { | ||
acc.definition += bracketSpaces + "}"; | ||
return acc; | ||
}, []); | ||
} | ||
exports.selectionSetToObjectFields = selectionSetToObjectFields; | ||
/** | ||
* Converts selection set to namespace fields | ||
* @param {SelectionSetNode} selectionSet | ||
* @param rootNode | ||
* @param path | ||
* @returns {OperationNamespaceField[]} | ||
*/ | ||
function selectionSetToNamespaceFields(selectionSet, rootNode, path) { | ||
if (path === void 0) { path = ''; } | ||
return selectionSet.selections.reduce(function (acc, s) { | ||
// TODO: Fragments support | ||
if (s.kind === 'Field') { | ||
acc.push(parseFieldNode(s, rootNode, path)); | ||
} | ||
return acc; | ||
}, { requiredTypes: [], definition: '{\n' }); | ||
}, []); | ||
} | ||
exports.parseSelectionSet = parseSelectionSet; | ||
exports.selectionSetToNamespaceFields = selectionSetToNamespaceFields; | ||
function selectionSetToRootNamespace(selectionSet, compiledName, args, rootNode) { | ||
var nspFields = selectionSetToNamespaceFields(selectionSet, rootNode); | ||
var importTypes = nspFields.reduce(function (acc, f) { | ||
if (f.type.importTypes) { | ||
f.type.importTypes.forEach(function (t) { | ||
if (!acc.includes(t)) { | ||
acc.push(t); | ||
} | ||
}); | ||
} | ||
return acc; | ||
}, []); | ||
return { | ||
name: compiledName, | ||
fields: nspFields, | ||
args: args, | ||
importTypes: importTypes, | ||
}; | ||
} | ||
exports.selectionSetToRootNamespace = selectionSetToRootNamespace; | ||
function parseFieldNode(node, rootNode, prevPath) { | ||
var name = node.name.value; | ||
var compiledName = misc_1.toCamelCase(name); | ||
var path = prevPath.length > 0 ? prevPath + "." + name : name; | ||
if (node.selectionSet) { | ||
return { | ||
name: compiledName, | ||
type: { | ||
name: compiledName, | ||
fields: node.selectionSet.selections.reduce(function (acc, s) { | ||
// TODO: Fragments support | ||
if (s.kind === 'Field') { | ||
var selectionCompiledName = misc_1.toCamelCase(s.name.value); | ||
acc.push({ | ||
name: s.name.value, | ||
type: compiledName + "." + selectionCompiledName, | ||
}); | ||
} | ||
return acc; | ||
}, []), | ||
}, | ||
fields: selectionSetToNamespaceFields(node.selectionSet, rootNode, path), | ||
}; | ||
} | ||
var type = misc_1.getIn(rootNode, path).type; | ||
return { | ||
name: compiledName, | ||
type: misc_1.getIOTypeDefinition(type), | ||
}; | ||
} | ||
exports.parseFieldNode = parseFieldNode; | ||
/** | ||
@@ -232,26 +324,34 @@ * Parses GQL operation | ||
* @param operationsString | ||
* @returns {ParsedGQLOperationDefinitionNode} | ||
* @returns {Operation} | ||
*/ | ||
function parseOperationDefinitionNode(node, schema, operationsString) { | ||
var _a; | ||
var name = node.name, selectionSet = node.selectionSet, operation = node.operation, variableDefinitions = node.variableDefinitions, loc = node.loc; | ||
var originalName = name.value; | ||
var compiledName = misc_1.getCompiledOperationName(name.value, operation); | ||
var rootNode = misc_1.getOperationRootNode(schema, operation); | ||
var _b = parseSelectionSet(rootNode, schema, selectionSet), definition = _b.definition, requiredTypes = _b.requiredTypes; | ||
var variables = parseOperationVariableDefinitions(misc_1.getCompiledOperationName(originalName, operation), __spreadArrays(variableDefinitions)); | ||
// Extract variables required types | ||
var allRequiredTypes = (_a = variables.fields | ||
.map(function (f) { return f.requiredTypes; }) | ||
.flat()).concat.apply(_a, requiredTypes).filter(function (t, idx, arr) { return arr.indexOf(t, idx + 1) === -1; }); | ||
// Drop already extracted required types | ||
variables.fields.forEach(function (f) { return f.requiredTypes = []; }); | ||
var importTypes = []; | ||
var addImportTypes = function (types) { return types.forEach(function (t) { | ||
if (!importTypes.includes(t)) { | ||
importTypes.push(t); | ||
} | ||
}); }; | ||
// Selection | ||
var selection = { | ||
name: compiledName, | ||
fields: selectionSetToObjectFields(selectionSet, compiledName), | ||
}; | ||
// Arguments | ||
var args = parseOperationVariableDefinitions(__spreadArrays(variableDefinitions)); | ||
addImportTypes(args.importTypes); | ||
// Namespace | ||
var namespace = selectionSetToRootNamespace(selectionSet, compiledName, args, rootNode); | ||
addImportTypes(namespace.importTypes); | ||
return { | ||
originalName: originalName, | ||
operationSignature: operationsString.slice(loc.start, loc.end), | ||
operationType: operation, | ||
operationDefinition: definition, | ||
requiredTypes: allRequiredTypes, | ||
variables: variables, | ||
__type: 'operation', | ||
name: name.value + misc_1.toCamelCase(operation), | ||
signature: operationsString.slice(loc.start, loc.end), | ||
selection: selection, | ||
namespace: namespace, | ||
importTypes: importTypes, | ||
}; | ||
} | ||
exports.parseOperationDefinitionNode = parseOperationDefinitionNode; |
{ | ||
"name": "gql-types-generator", | ||
"version": "1.3.1", | ||
"version": "1.4.0", | ||
"main": "dist/index.js", | ||
@@ -5,0 +5,0 @@ "types": "dist/index.d.ts", |
@@ -31,3 +31,3 @@ gql-types-generator | ||
## Command line interface | ||
### Command line interface | ||
After installation of package is done, `gql-types-generator` command | ||
@@ -58,18 +58,29 @@ becomes available. | ||
### Compilation result | ||
As a result, command creates a directory on passed `--output-directory` path, | ||
generates `d.ts` definition file and compiled `js` code: | ||
#### Schema | ||
Command creates a directory on passed `--output-directory` path, generates | ||
`{schemaFile}.d.ts` definition file and compiled `{schemaFile}.js` code. | ||
- `d.ts` contains all schema types and by default exports constant `schema: string` | ||
which is a text representation of schema | ||
- `js` exports by default text representation of schema | ||
`{schemaFile}.d.ts` contains all schema types and by default exports | ||
constant `schema` which is a text or `graphql`'s `DocumentNode` representation | ||
of schema. | ||
If `--operations` passed, command is searching for operations and creates a | ||
pair of `d.ts` and `js` files for each found operation. Name of each created | ||
file depends on original operation name and its type. So, if operation was | ||
`query getUsers { ... }`, created files will be `getUsersQuery.d.ts` and | ||
`getUsersQuery.js`. | ||
Each type definition consists of interface and namespace with the | ||
same name. All interface fields refers to namespace fields. So, if you want | ||
to get some `Query` field type you could use `Query['someField']` | ||
or `Query.SomeField`. They return the same thing. It is recommended | ||
to use `Query.*`-like syntax for better experience. | ||
If `--operations-file` passed, all files will be placed into a single file | ||
with passed name. | ||
If `--scalars` passed, compiled type of scalar will be taken from this map. | ||
If scalar not found, it will be `any`. Must be stringified | ||
`Record<string, string | number>`. | ||
#### Operations | ||
To compile operations, it is required to use `--operations` argument. | ||
Library creates single file with name `--operations-file` if it is passed | ||
or 2 separate files `d.ts` and `js` for each command with name | ||
`{operationName}{operationType}` in directory on passed `--output-directory`. | ||
So, if operation was `query getUsers { ... }`, created files will be | ||
`getUsersQuery.d.ts` and `getUsersQuery.js`.] | ||
If `--operations-wrap` passed, wraps each operation string with `graphql-tag` | ||
@@ -79,13 +90,12 @@ package making each operation not string, but `graphql`s `Document Node`. | ||
If `--scalars` passed, compiled type of scalar will be taken from this map. | ||
If scalar not found, it will be `any`. Must be stringified | ||
`Record<string, string | number>`. | ||
- `d.ts` exports selection and namespace with `Arguments` if they exist. | ||
Additionally namespace contains subselections represented as other | ||
namespaces | ||
- `js` exports representation of operation | ||
- `d.ts` by default exports string which is a text representation of operation. | ||
Additionally file contains types connected with operation. They can be: | ||
- Operation return type (for example, `GetUsersQuery`) | ||
- Operation variables type (for example, `GetUsersQueryVariables`) | ||
- `js` exports by default text representation of operation | ||
### Compiled types example | ||
You can find library test right [here](src/test). Just use command in | ||
`command` file and look what happens. | ||
## Programmatic control | ||
### Programmatic control | ||
Library provides such functions as `compile`, `compileSchema` and | ||
@@ -92,0 +102,0 @@ `compileOperations` to generate types. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83922
1894
223