graphql-codegen-typescript
Advanced tools
Comparing version 0.19.0-alpha.6f432573 to 0.19.0-alpha.72ec7038
@@ -10,3 +10,3 @@ "use strict"; | ||
exports.plugin = (schema, documents, config) => { | ||
const visitor = new visitor_1.TsVisitor(config); | ||
const visitor = new visitor_1.TsVisitor(schema, config); | ||
const printedSchema = graphql_1.printSchema(schema); | ||
@@ -17,3 +17,4 @@ const astNode = graphql_1.parse(printedSchema); | ||
const introspectionDefinitions = includeIntrospectionDefinitions(schema, documents, config); | ||
return [header, ...visitorResult.definitions, ...introspectionDefinitions].join('\n'); | ||
const scalars = visitor.scalarsDefinition; | ||
return [header, scalars, ...visitorResult.definitions, ...introspectionDefinitions].join('\n'); | ||
}; | ||
@@ -36,3 +37,3 @@ function includeIntrospectionDefinitions(schema, documents, config) { | ||
}); | ||
const visitor = new introspection_visitor_1.TsIntrospectionVisitor(config, typesToInclude); | ||
const visitor = new introspection_visitor_1.TsIntrospectionVisitor(schema, config, typesToInclude); | ||
const result = graphql_1.visit(graphql_1.parse(graphql_1.printIntrospectionSchema(schema)), { leave: visitor }); | ||
@@ -39,0 +40,0 @@ // recursively go through each `usedTypes` and their children and collect all used types |
@@ -1,2 +0,2 @@ | ||
import { GraphQLNamedType, EnumTypeDefinitionNode, ObjectTypeDefinitionNode } from 'graphql'; | ||
import { GraphQLSchema, GraphQLNamedType, EnumTypeDefinitionNode, ObjectTypeDefinitionNode } from 'graphql'; | ||
import { TsVisitor } from './visitor'; | ||
@@ -6,3 +6,3 @@ import { TypeScriptPluginConfig } from './index'; | ||
private typesToInclude; | ||
constructor(pluginConfig: TypeScriptPluginConfig, typesToInclude: GraphQLNamedType[]); | ||
constructor(schema: GraphQLSchema, pluginConfig: TypeScriptPluginConfig, typesToInclude: GraphQLNamedType[]); | ||
DirectiveDefinition(): any; | ||
@@ -9,0 +9,0 @@ ObjectTypeDefinition(node: ObjectTypeDefinitionNode, key: string | number, parent: any): string; |
@@ -6,4 +6,4 @@ "use strict"; | ||
class TsIntrospectionVisitor extends visitor_1.TsVisitor { | ||
constructor(pluginConfig = {}, typesToInclude) { | ||
super(pluginConfig); | ||
constructor(schema, pluginConfig = {}, typesToInclude) { | ||
super(schema, pluginConfig); | ||
this.typesToInclude = []; | ||
@@ -10,0 +10,0 @@ this.typesToInclude = typesToInclude; |
import { BaseTypesVisitor, ParsedTypesConfig } from 'graphql-codegen-visitor-plugin-common'; | ||
import { TypeScriptPluginConfig } from './index'; | ||
import { FieldDefinitionNode, NamedTypeNode, ListTypeNode, NonNullTypeNode, EnumTypeDefinitionNode } from 'graphql'; | ||
import { FieldDefinitionNode, NamedTypeNode, ListTypeNode, NonNullTypeNode, EnumTypeDefinitionNode, InputValueDefinitionNode, GraphQLSchema } from 'graphql'; | ||
export interface TypeScriptPluginParsedConfig extends ParsedTypesConfig { | ||
@@ -12,3 +12,3 @@ avoidOptionals: boolean; | ||
export declare class TsVisitor<TRawConfig extends TypeScriptPluginConfig = TypeScriptPluginConfig, TParsedConfig extends TypeScriptPluginParsedConfig = TypeScriptPluginParsedConfig> extends BaseTypesVisitor<TRawConfig, TParsedConfig> { | ||
constructor(pluginConfig: TRawConfig, additionalConfig?: Partial<TParsedConfig>); | ||
constructor(schema: GraphQLSchema, pluginConfig: TRawConfig, additionalConfig?: Partial<TParsedConfig>); | ||
private clearOptional; | ||
@@ -20,3 +20,4 @@ NamedType(node: NamedTypeNode): string; | ||
FieldDefinition(node: FieldDefinitionNode, key?: number | string, parent?: any): string; | ||
InputValueDefinition(node: InputValueDefinitionNode, key?: number | string, parent?: any): string; | ||
EnumTypeDefinition(node: EnumTypeDefinitionNode): string; | ||
} |
@@ -8,4 +8,4 @@ "use strict"; | ||
class TsVisitor extends graphql_codegen_visitor_plugin_common_1.BaseTypesVisitor { | ||
constructor(pluginConfig, additionalConfig = {}) { | ||
super(pluginConfig, Object.assign({ avoidOptionals: pluginConfig.avoidOptionals || false, maybeValue: pluginConfig.maybeValue || 'T | null', constEnums: pluginConfig.constEnums || false, enumsAsTypes: pluginConfig.enumsAsTypes || false, immutableTypes: pluginConfig.immutableTypes || false }, (additionalConfig || {})), null); | ||
constructor(schema, pluginConfig, additionalConfig = {}) { | ||
super(schema, pluginConfig, Object.assign({ avoidOptionals: pluginConfig.avoidOptionals || false, maybeValue: pluginConfig.maybeValue || 'T | null', constEnums: pluginConfig.constEnums || false, enumsAsTypes: pluginConfig.enumsAsTypes || false, immutableTypes: pluginConfig.immutableTypes || false }, (additionalConfig || {}))); | ||
autoBind(this); | ||
@@ -19,3 +19,3 @@ this.setArgumentsTransformer(new typescript_variables_to_object_1.TypeScriptOperationVariablesToObject(this.scalars, this.convertName, this.config.avoidOptionals, this.config.immutableTypes)); | ||
if (str.startsWith('Maybe')) { | ||
return str.replace(/Maybe<(.*?)>/, '$1'); | ||
return str.replace(/Maybe<(.*?)>$/, '$1'); | ||
} | ||
@@ -43,2 +43,7 @@ return str; | ||
} | ||
InputValueDefinition(node, key, parent) { | ||
const originalFieldNode = parent[key]; | ||
const addOptionalSign = !this.config.avoidOptionals && originalFieldNode.type.kind !== graphql_1.Kind.NON_NULL_TYPE; | ||
return graphql_codegen_visitor_plugin_common_1.indent(`${node.name}${addOptionalSign ? '?' : ''}: ${node.type},`); | ||
} | ||
EnumTypeDefinition(node) { | ||
@@ -45,0 +50,0 @@ if (this.config.enumsAsTypes) { |
@@ -7,3 +7,3 @@ import { parse, printSchema, visit, TypeInfo, visitWithTypeInfo, getNamedType, isIntrospectionType, printIntrospectionSchema, isObjectType } from 'graphql'; | ||
export const plugin = (schema, documents, config) => { | ||
const visitor = new TsVisitor(config); | ||
const visitor = new TsVisitor(schema, config); | ||
const printedSchema = printSchema(schema); | ||
@@ -14,3 +14,4 @@ const astNode = parse(printedSchema); | ||
const introspectionDefinitions = includeIntrospectionDefinitions(schema, documents, config); | ||
return [header, ...visitorResult.definitions, ...introspectionDefinitions].join('\n'); | ||
const scalars = visitor.scalarsDefinition; | ||
return [header, scalars, ...visitorResult.definitions, ...introspectionDefinitions].join('\n'); | ||
}; | ||
@@ -33,3 +34,3 @@ function includeIntrospectionDefinitions(schema, documents, config) { | ||
}); | ||
const visitor = new TsIntrospectionVisitor(config, typesToInclude); | ||
const visitor = new TsIntrospectionVisitor(schema, config, typesToInclude); | ||
const result = visit(parse(printIntrospectionSchema(schema)), { leave: visitor }); | ||
@@ -36,0 +37,0 @@ // recursively go through each `usedTypes` and their children and collect all used types |
@@ -1,2 +0,2 @@ | ||
import { GraphQLNamedType, EnumTypeDefinitionNode, ObjectTypeDefinitionNode } from 'graphql'; | ||
import { GraphQLSchema, GraphQLNamedType, EnumTypeDefinitionNode, ObjectTypeDefinitionNode } from 'graphql'; | ||
import { TsVisitor } from './visitor'; | ||
@@ -6,3 +6,3 @@ import { TypeScriptPluginConfig } from './index'; | ||
private typesToInclude; | ||
constructor(pluginConfig: TypeScriptPluginConfig, typesToInclude: GraphQLNamedType[]); | ||
constructor(schema: GraphQLSchema, pluginConfig: TypeScriptPluginConfig, typesToInclude: GraphQLNamedType[]); | ||
DirectiveDefinition(): any; | ||
@@ -9,0 +9,0 @@ ObjectTypeDefinition(node: ObjectTypeDefinitionNode, key: string | number, parent: any): string; |
import { TsVisitor } from './visitor'; | ||
import * as autoBind from 'auto-bind'; | ||
export class TsIntrospectionVisitor extends TsVisitor { | ||
constructor(pluginConfig = {}, typesToInclude) { | ||
super(pluginConfig); | ||
constructor(schema, pluginConfig = {}, typesToInclude) { | ||
super(schema, pluginConfig); | ||
this.typesToInclude = []; | ||
@@ -7,0 +7,0 @@ this.typesToInclude = typesToInclude; |
import { BaseTypesVisitor, ParsedTypesConfig } from 'graphql-codegen-visitor-plugin-common'; | ||
import { TypeScriptPluginConfig } from './index'; | ||
import { FieldDefinitionNode, NamedTypeNode, ListTypeNode, NonNullTypeNode, EnumTypeDefinitionNode } from 'graphql'; | ||
import { FieldDefinitionNode, NamedTypeNode, ListTypeNode, NonNullTypeNode, EnumTypeDefinitionNode, InputValueDefinitionNode, GraphQLSchema } from 'graphql'; | ||
export interface TypeScriptPluginParsedConfig extends ParsedTypesConfig { | ||
@@ -12,3 +12,3 @@ avoidOptionals: boolean; | ||
export declare class TsVisitor<TRawConfig extends TypeScriptPluginConfig = TypeScriptPluginConfig, TParsedConfig extends TypeScriptPluginParsedConfig = TypeScriptPluginParsedConfig> extends BaseTypesVisitor<TRawConfig, TParsedConfig> { | ||
constructor(pluginConfig: TRawConfig, additionalConfig?: Partial<TParsedConfig>); | ||
constructor(schema: GraphQLSchema, pluginConfig: TRawConfig, additionalConfig?: Partial<TParsedConfig>); | ||
private clearOptional; | ||
@@ -20,3 +20,4 @@ NamedType(node: NamedTypeNode): string; | ||
FieldDefinition(node: FieldDefinitionNode, key?: number | string, parent?: any): string; | ||
InputValueDefinition(node: InputValueDefinitionNode, key?: number | string, parent?: any): string; | ||
EnumTypeDefinition(node: EnumTypeDefinitionNode): string; | ||
} |
@@ -6,4 +6,4 @@ import { DeclarationBlock, indent, BaseTypesVisitor } from 'graphql-codegen-visitor-plugin-common'; | ||
export class TsVisitor extends BaseTypesVisitor { | ||
constructor(pluginConfig, additionalConfig = {}) { | ||
super(pluginConfig, Object.assign({ avoidOptionals: pluginConfig.avoidOptionals || false, maybeValue: pluginConfig.maybeValue || 'T | null', constEnums: pluginConfig.constEnums || false, enumsAsTypes: pluginConfig.enumsAsTypes || false, immutableTypes: pluginConfig.immutableTypes || false }, (additionalConfig || {})), null); | ||
constructor(schema, pluginConfig, additionalConfig = {}) { | ||
super(schema, pluginConfig, Object.assign({ avoidOptionals: pluginConfig.avoidOptionals || false, maybeValue: pluginConfig.maybeValue || 'T | null', constEnums: pluginConfig.constEnums || false, enumsAsTypes: pluginConfig.enumsAsTypes || false, immutableTypes: pluginConfig.immutableTypes || false }, (additionalConfig || {}))); | ||
autoBind(this); | ||
@@ -17,3 +17,3 @@ this.setArgumentsTransformer(new TypeScriptOperationVariablesToObject(this.scalars, this.convertName, this.config.avoidOptionals, this.config.immutableTypes)); | ||
if (str.startsWith('Maybe')) { | ||
return str.replace(/Maybe<(.*?)>/, '$1'); | ||
return str.replace(/Maybe<(.*?)>$/, '$1'); | ||
} | ||
@@ -41,2 +41,7 @@ return str; | ||
} | ||
InputValueDefinition(node, key, parent) { | ||
const originalFieldNode = parent[key]; | ||
const addOptionalSign = !this.config.avoidOptionals && originalFieldNode.type.kind !== Kind.NON_NULL_TYPE; | ||
return indent(`${node.name}${addOptionalSign ? '?' : ''}: ${node.type},`); | ||
} | ||
EnumTypeDefinition(node) { | ||
@@ -43,0 +48,0 @@ if (this.config.enumsAsTypes) { |
{ | ||
"name": "graphql-codegen-typescript", | ||
"version": "0.19.0-alpha.6f432573", | ||
"version": "0.19.0-alpha.72ec7038", | ||
"description": "GraphQL Code Generator plugin for generating TypeScript types", | ||
@@ -9,7 +9,7 @@ "repository": "git@github.com:dotansimha/graphql-code-generator.git", | ||
"build": "tsc -m esnext --outDir dist/esnext && tsc -m commonjs --outDir dist/commonjs", | ||
"test": "jest" | ||
"test": "jest --config ../../../jest.config.js" | ||
}, | ||
"dependencies": { | ||
"graphql-codegen-plugin-helpers": "0.19.0-alpha.6f432573", | ||
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.6f432573", | ||
"graphql-codegen-plugin-helpers": "0.19.0-alpha.72ec7038", | ||
"graphql-codegen-visitor-plugin-common": "0.19.0-alpha.72ec7038", | ||
"tslib": "1.9.3" | ||
@@ -19,7 +19,8 @@ }, | ||
"graphql": "14.1.1", | ||
"graphql-codegen-testing": "0.19.0-alpha.6f432573", | ||
"jest": "24.1.0", | ||
"graphql-codegen-testing": "0.19.0-alpha.72ec7038", | ||
"jest": "24.5.0", | ||
"ts-jest": "24.0.0", | ||
"typescript": "3.3.3333" | ||
}, | ||
"sideEffects": false, | ||
"main": "dist/commonjs/index.js", | ||
@@ -30,22 +31,3 @@ "module": "dist/esnext/index.js", | ||
"definition": "dist/esnext/index.d.ts" | ||
}, | ||
"jest": { | ||
"globals": { | ||
"ts-jest": { | ||
"enableTsDiagnostics": false | ||
} | ||
}, | ||
"transform": { | ||
"^.+\\.tsx?$": "ts-jest" | ||
}, | ||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", | ||
"moduleFileExtensions": [ | ||
"ts", | ||
"tsx", | ||
"js", | ||
"jsx", | ||
"json", | ||
"node" | ||
] | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
39631
504
0
+ Addedgraphql-codegen-plugin-helpers@0.19.0-alpha.72ec7038(transitive)
+ Addedgraphql-codegen-visitor-plugin-common@0.19.0-alpha.72ec7038(transitive)
- Removedgraphql-codegen-plugin-helpers@0.19.0-alpha.6f432573(transitive)
- Removedgraphql-codegen-visitor-plugin-common@0.19.0-alpha.6f432573(transitive)
- Removedts-log@2.1.4(transitive)
Updatedgraphql-codegen-visitor-plugin-common@0.19.0-alpha.72ec7038