gql-types-generator
Advanced tools
Comparing version 1.1.1 to 1.2.0
@@ -53,7 +53,8 @@ #!/usr/bin/env node | ||
program | ||
.option('--sort <sort>', 'how to display compiled types. Valid values are "as-is" and ' + | ||
.option('--remove-description', 'states if we should remove description') | ||
.option('-s --sort <sort>', 'how to display compiled types. Valid values are "as-is" and ' + | ||
'"default". By default, generator compiles scalars first, then enums, ' + | ||
'interfaces, inputs, unions and then types. "as-is" places types as they ' + | ||
'are placed in schema', parsePlacement) | ||
.requiredOption('--output-path <path>', 'path to file where typings will be saved') | ||
.requiredOption('-o --output-path <path>', 'path to file where typings will be saved') | ||
.arguments('<schema-globs>') | ||
@@ -67,3 +68,3 @@ .action(function (globs) { return __awaiter(void 0, void 0, void 0, function () { | ||
_b = { | ||
outputPath: fs_2.withCwd(program.outputPath) | ||
removeDescription: 'removeDescription' in program | ||
}; | ||
@@ -73,2 +74,3 @@ return [4 /*yield*/, fs_1.withCwdAndGlob(globs)]; | ||
_b.sort = program.sort, | ||
_b.outputPath = fs_2.withCwd(program.outputPath), | ||
_b)])]; | ||
@@ -75,0 +77,0 @@ case 2: |
@@ -43,2 +43,3 @@ "use strict"; | ||
var generation_1 = require("./utils/generation"); | ||
var misc_1 = require("./utils/misc"); | ||
var weights = { | ||
@@ -59,7 +60,8 @@ ScalarTypeDefinition: 0, | ||
return __awaiter(this, void 0, void 0, function () { | ||
var outputPath, sort, schemaString, schema, typeMap, compiledTypes; | ||
var outputPath, sort, removeDescription, includeDescription, schemaString, schema, typeMap, compiledTypes; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
outputPath = options.outputPath, sort = options.sort; | ||
outputPath = options.outputPath, sort = options.sort, removeDescription = options.removeDescription; | ||
includeDescription = !removeDescription; | ||
schemaString = ''; | ||
@@ -98,3 +100,3 @@ if (!('source' in options)) return [3 /*break*/, 2]; | ||
// We parse only types used in schema. We can meet some scalar types | ||
// in typeMap. Scalar types dont have astNode | ||
// in typeMap. Internal types dont have astNode | ||
if (astNode !== undefined) { | ||
@@ -104,12 +106,12 @@ switch (astNode.kind) { | ||
case 'InputObjectTypeDefinition': | ||
acc.push(generation_1.generateGQLInterface(utils_1.parseInterfaceDefinitionNode(astNode))); | ||
acc.push(generation_1.generateGQLInterface(utils_1.parseInterfaceDefinitionNode(astNode, includeDescription))); | ||
break; | ||
case 'ScalarTypeDefinition': | ||
acc.push(generation_1.generateGQLScalar(utils_1.parseScalarTypeDefinitionNode(astNode))); | ||
acc.push(generation_1.generateGQLScalar(utils_1.parseScalarTypeDefinitionNode(astNode, includeDescription))); | ||
break; | ||
case 'UnionTypeDefinition': | ||
acc.push(generation_1.generateGQLUnion(utils_1.parseUnionTypeDefinitionNode(astNode))); | ||
acc.push(generation_1.generateGQLUnion(utils_1.parseUnionTypeDefinitionNode(astNode, includeDescription))); | ||
break; | ||
case 'EnumTypeDefinition': | ||
acc.push(generation_1.generateGQLEnum(utils_1.parseEnumDefinitionNode(astNode))); | ||
acc.push(generation_1.generateGQLEnum(utils_1.parseEnumDefinitionNode(astNode, includeDescription))); | ||
break; | ||
@@ -121,2 +123,4 @@ } | ||
.join('\n\n'); | ||
// Add warning that these types are compiled and should not be edited | ||
compiledTypes = misc_1.wrapWithWarning(compiledTypes); | ||
fs_1.writeFile(outputPath, compiledTypes); | ||
@@ -123,0 +127,0 @@ return [2 /*return*/]; |
@@ -26,2 +26,3 @@ /** | ||
outputPath: string; | ||
removeDescription?: boolean; | ||
sort?: 'as-is' | 'default'; | ||
@@ -28,0 +29,0 @@ } |
@@ -35,1 +35,8 @@ import { CompiledTypeName, GQLInternalTypeName } from '../types'; | ||
export declare function getTypeDefinition(node: TypeNode, nullable?: boolean): string; | ||
/** | ||
* Wraps schema with warning that types should be edited due to they are | ||
* compiled | ||
* @param {string} types | ||
* @returns {string} | ||
*/ | ||
export declare function wrapWithWarning(types: string): string; |
@@ -86,1 +86,16 @@ "use strict"; | ||
exports.getTypeDefinition = getTypeDefinition; | ||
/** | ||
* Wraps schema with warning that types should be edited due to they are | ||
* compiled | ||
* @param {string} types | ||
* @returns {string} | ||
*/ | ||
function wrapWithWarning(types) { | ||
var line = '// ' + new Array(20).fill('=').join('') + '\n'; | ||
return line | ||
+ '// THESE TYPES ARE COMPILED VIA GQL-TYPES-GENERATOR AND SHOULD NOT BE\n' + | ||
'// DIRECTLY EDITED\n' | ||
+ line | ||
+ types; | ||
} | ||
exports.wrapWithWarning = wrapWithWarning; |
@@ -6,22 +6,26 @@ import { ParsedGQLEnumType, ParsedGQLScalarType, ParsedGQLTypeOrInterface, ParsedGQLUnionType } from '../types'; | ||
* @param {EnumTypeDefinitionNode} node | ||
* @param includeDescription | ||
* @returns {ParsedGQLEnumType} | ||
*/ | ||
export declare function parseEnumDefinitionNode(node: EnumTypeDefinitionNode): ParsedGQLEnumType; | ||
export declare function parseEnumDefinitionNode(node: EnumTypeDefinitionNode, includeDescription: boolean): ParsedGQLEnumType; | ||
/** | ||
* Parses GQL types which have fields | ||
* @param {ObjectTypeDefinitionNode | InputObjectTypeDefinitionNode} node | ||
* @param includeDescription | ||
* @returns {ParsedGQLType} | ||
*/ | ||
export declare function parseInterfaceDefinitionNode(node: ObjectTypeDefinitionNode | InputObjectTypeDefinitionNode): ParsedGQLTypeOrInterface; | ||
export declare function parseInterfaceDefinitionNode(node: ObjectTypeDefinitionNode | InputObjectTypeDefinitionNode, includeDescription: boolean): ParsedGQLTypeOrInterface; | ||
/** | ||
* Parses GQL scalar type | ||
* @param {ScalarTypeDefinitionNode} node | ||
* @param includeDescription | ||
* @returns {ParsedGQLScalarType} | ||
*/ | ||
export declare function parseScalarTypeDefinitionNode(node: ScalarTypeDefinitionNode): ParsedGQLScalarType; | ||
export declare function parseScalarTypeDefinitionNode(node: ScalarTypeDefinitionNode, includeDescription: boolean): ParsedGQLScalarType; | ||
/** | ||
* Parses GQL union type | ||
* @param {UnionTypeDefinitionNode} node | ||
* @param includeDescription | ||
* @returns {ParsedGQLUnionType} | ||
*/ | ||
export declare function parseUnionTypeDefinitionNode(node: UnionTypeDefinitionNode): ParsedGQLUnionType; | ||
export declare function parseUnionTypeDefinitionNode(node: UnionTypeDefinitionNode, includeDescription: boolean): ParsedGQLUnionType; |
@@ -14,5 +14,6 @@ "use strict"; | ||
* @param {EnumTypeDefinitionNode} node | ||
* @param includeDescription | ||
* @returns {ParsedGQLEnumType} | ||
*/ | ||
function parseEnumDefinitionNode(node) { | ||
function parseEnumDefinitionNode(node, includeDescription) { | ||
var values = node.values, description = node.description, name = node.name; | ||
@@ -22,3 +23,3 @@ var parsedValues = values.reduce(function (vAcc, v) { | ||
vAcc.push({ | ||
description: description ? description.value : null, | ||
description: description && includeDescription ? description.value : null, | ||
value: name.value, | ||
@@ -29,3 +30,3 @@ }); | ||
return { | ||
description: description ? description.value : null, | ||
description: description && includeDescription ? description.value : null, | ||
name: name.value, | ||
@@ -40,5 +41,6 @@ type: 'enum', | ||
* @param {ObjectTypeDefinitionNode | InputObjectTypeDefinitionNode} node | ||
* @param includeDescription | ||
* @returns {ParsedGQLType} | ||
*/ | ||
function parseInterfaceDefinitionNode(node) { | ||
function parseInterfaceDefinitionNode(node, includeDescription) { | ||
var fields = node.fields, description = node.description, name = node.name; | ||
@@ -49,3 +51,4 @@ var parsedFields = __spreadArrays(fields).reduce(function (fAcc, f) { | ||
definition: misc_1.getTypeDefinition(type), | ||
description: description ? description.value : null, | ||
description: description && includeDescription | ||
? description.value : null, | ||
name: name.value, | ||
@@ -56,3 +59,3 @@ }); | ||
return { | ||
description: description ? description.value : null, | ||
description: description && includeDescription ? description.value : null, | ||
type: 'interface', | ||
@@ -67,8 +70,9 @@ fields: parsedFields, | ||
* @param {ScalarTypeDefinitionNode} node | ||
* @param includeDescription | ||
* @returns {ParsedGQLScalarType} | ||
*/ | ||
function parseScalarTypeDefinitionNode(node) { | ||
function parseScalarTypeDefinitionNode(node, includeDescription) { | ||
var name = node.name, description = node.description; | ||
return { | ||
description: description ? description.value : null, | ||
description: description && includeDescription ? description.value : null, | ||
type: 'type', | ||
@@ -82,5 +86,6 @@ name: name.value, | ||
* @param {UnionTypeDefinitionNode} node | ||
* @param includeDescription | ||
* @returns {ParsedGQLUnionType} | ||
*/ | ||
function parseUnionTypeDefinitionNode(node) { | ||
function parseUnionTypeDefinitionNode(node, includeDescription) { | ||
var name = node.name, description = node.description, types = node.types; | ||
@@ -90,3 +95,3 @@ return { | ||
name: name.value, | ||
description: description ? description.value : null, | ||
description: description && includeDescription ? description.value : null, | ||
definition: types.map(function (t) { return misc_1.transpileGQLTypeName(t.name.value); }).join(' | '), | ||
@@ -93,0 +98,0 @@ }; |
{ | ||
"name": "gql-types-generator", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"main": "index.js", | ||
@@ -5,0 +5,0 @@ "types": "index.d.ts", |
@@ -38,11 +38,27 @@ gql-types-generator | ||
Options: | ||
--sort <sort> how to display compiled types. Valid values are "as-is" and "default". By default, generator compiles scalars first, then enums, interfaces, inputs, unions and then types. "as-is" places types as they are placed in schema | ||
--output-path <path> path to file where typings will be saved | ||
-h, --help display help for command | ||
--remove-description states if we should remove description | ||
-s --sort <sort> how to display compiled types. Valid values are "as-is" and "default". By default, generator compiles scalars first, then enums, interfaces, inputs, unions and then types. "as-is" places types as they are placed in schema | ||
-o --output-path <path> path to file where typings will be saved | ||
-h, --help display help for command | ||
``` | ||
### Programmatic control | ||
If needed, you can use `compile` function to generate types. | ||
#### Options | ||
[Current list of options](https://github.com/wolframdeus/gql-types-generator/blob/master/src/types.ts#L29) | ||
| Name | Type | Description | | ||
| --- | --- | --- | | ||
| `removeDescription (optional)` | `boolean` | Removes GraphQL descriptions | | ||
| `schema` | `string` | String representation of schema. Can be used instead of `source` option | | ||
| `source` | `string` | Glob to search schema partials. Can be used instead of `schema` option | | ||
| `sort (optional)` | `'as-is'` or `'default'` | States in what order display compiled types. By default, generator compiles scalars first, then enums, interfaces, inputs, unions and then types. `as-is` places types as they are placed in schema | | ||
| `outputPath` | `string` | Full path to file where compiler should save types | | ||
#### Examples | ||
When schema is separated between 2 directories: | ||
```typescript | ||
@@ -58,17 +74,17 @@ import {compile} from 'gql-types-generator'; | ||
outputPath: path.resolve(__dirname, 'types.d.ts'), | ||
sort: 'as-is', | ||
}); | ||
``` | ||
// OR | ||
When all the schema partials are in the only 1 directory: | ||
```typescript | ||
compile({ | ||
source: path.resolve(__dirname, 'schema-artifacts/*.graphql'), | ||
outputPath: path.resolve(__dirname, 'types.d.ts'), | ||
sort: 'default', | ||
}); | ||
``` | ||
// OR | ||
When you already have schema as text: | ||
compile({ | ||
// You can pass gql schema directly | ||
```typescript | ||
compile({ | ||
schema: 'type Query { ... }', | ||
@@ -78,1 +94,10 @@ outputPath: path.resolve(__dirname, 'types.d.ts'), | ||
``` | ||
When you want to sort schema types as they are placed in original GQL schema: | ||
```typescript | ||
compile({ | ||
source: path.resolve(__dirname, 'schema-artifacts/*.graphql'), | ||
outputPath: path.resolve(__dirname, 'types.d.ts'), | ||
sort: 'as-is' | ||
}); | ||
``` |
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
36836
808
101