Join our webinar on Wednesday, June 26, at 1pm EDTHow Chia Mitigates Risk in the Crypto Industry.Register
Socket
Socket
Sign inDemoInstall

@graphql-codegen/visitor-plugin-common

Package Overview
Dependencies
6
Maintainers
5
Versions
5775
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.18.1-alpha.59 to 0.18.1-alpha.60

35

dist/commonjs/base-documents-visitor.d.ts

@@ -1,2 +0,2 @@

import { ScalarsMap, NamingConvention, ConvertFn, ConvertOptions } from './types';
import { ScalarsMap, ConvertOptions } from './types';
import { DeclarationBlockConfig } from './utils';

@@ -6,23 +6,28 @@ import { GraphQLSchema, FragmentDefinitionNode, OperationDefinitionNode, ASTNode } from 'graphql';

import { OperationVariablesToObject } from './variables-to-object';
import { BaseVisitorConvertOptions } from './base-visitor';
export interface ParsedDocumentsConfig {
scalars: ScalarsMap;
convert: ConvertFn<ConvertOptions>;
typesPrefix: string;
import { RawConfig, ParsedConfig, BaseVisitor, BaseVisitorConvertOptions } from './base-visitor';
export interface ParsedDocumentsConfig extends ParsedConfig {
addTypename: boolean;
}
export interface RawDocumentsConfig {
scalars?: ScalarsMap;
namingConvention?: NamingConvention;
typesPrefix?: string;
export interface RawDocumentsConfig extends RawConfig {
/**
* @name skipTypename
* @type boolean
* @default false
* @description Automatically adds `__typename` field to the generated types, even when they are not specified
* in the selection set.
*
* @example
* ```yml
* config:
* skipTypename: true
* ```
*/
skipTypename?: boolean;
}
export declare class BaseDocumentsVisitor<TRawConfig extends RawDocumentsConfig = RawDocumentsConfig, TPluginConfig extends ParsedDocumentsConfig = ParsedDocumentsConfig> {
export declare class BaseDocumentsVisitor<TRawConfig extends RawDocumentsConfig = RawDocumentsConfig, TPluginConfig extends ParsedDocumentsConfig = ParsedDocumentsConfig> extends BaseVisitor<TRawConfig, TPluginConfig> {
protected _schema: GraphQLSchema;
protected _parsedConfig: TPluginConfig;
protected _declarationBlockConfig: DeclarationBlockConfig;
protected _unnamedCounter: number;
protected _variablesTransfomer: OperationVariablesToObject;
protected _selectionSetToObject: SelectionSetToObject;
constructor(rawConfig: TRawConfig, additionalConfig: TPluginConfig, _schema: GraphQLSchema, defaultScalars?: ScalarsMap);
constructor(rawConfig: TRawConfig, additionalConfig: TPluginConfig, _schema: GraphQLSchema, scalars?: ScalarsMap);
setSelectionSetHandler(handler: SelectionSetToObject): void;

@@ -32,5 +37,3 @@ setDeclarationBlockConfig(config: DeclarationBlockConfig): void;

convertName(node: ASTNode | string, options?: ConvertOptions & BaseVisitorConvertOptions): string;
readonly config: TPluginConfig;
readonly schema: GraphQLSchema;
readonly scalars: ScalarsMap;
readonly addTypename: boolean;

@@ -37,0 +40,0 @@ private handleAnonymouseOperation;

@@ -7,3 +7,3 @@ "use strict";

const variables_to_object_1 = require("./variables-to-object");
const naming_1 = require("./naming");
const base_visitor_1 = require("./base-visitor");
function getRootType(operation, schema) {

@@ -19,8 +19,7 @@ switch (operation) {

}
class BaseDocumentsVisitor {
constructor(rawConfig, additionalConfig, _schema, defaultScalars = scalars_1.DEFAULT_SCALARS) {
class BaseDocumentsVisitor extends base_visitor_1.BaseVisitor {
constructor(rawConfig, additionalConfig, _schema, scalars = scalars_1.DEFAULT_SCALARS) {
super(rawConfig, Object.assign({ addTypename: !rawConfig.skipTypename }, (additionalConfig || {})), scalars);
this._schema = _schema;
this._declarationBlockConfig = {};
this._unnamedCounter = 1;
this._parsedConfig = Object.assign({ addTypename: !rawConfig.skipTypename, scalars: Object.assign({}, (defaultScalars || scalars_1.DEFAULT_SCALARS), (rawConfig.scalars || {})), convert: naming_1.convertFactory(rawConfig), typesPrefix: rawConfig.typesPrefix || '' }, (additionalConfig || {}));
autoBind(this);

@@ -42,11 +41,5 @@ this._variablesTransfomer = new variables_to_object_1.OperationVariablesToObject(this.scalars, this.convertName);

}
get config() {
return this._parsedConfig;
}
get schema() {
return this._schema;
}
get scalars() {
return this.config.scalars;
}
get addTypename() {

@@ -59,3 +52,3 @@ return this._parsedConfig.addTypename;

return this.convertName(node, {
useTypesPrefix: false
useTypesPrefix: false,
});

@@ -66,3 +59,3 @@ }

suffix: '_',
useTypesPrefix: false
useTypesPrefix: false,
});

@@ -78,3 +71,3 @@ }

useTypesPrefix: true,
suffix: 'Fragment'
suffix: 'Fragment',
}))

@@ -92,3 +85,3 @@ .withContent(selectionSet.string).string;

.withName(this.convertName(name, {
suffix: utils_1.toPascalCase(node.operation)
suffix: utils_1.toPascalCase(node.operation),
}))

@@ -100,3 +93,3 @@ .withContent(selectionSet.string).string;

.withName(this.convertName(name, {
suffix: utils_1.toPascalCase(node.operation) + 'Variables'
suffix: utils_1.toPascalCase(node.operation) + 'Variables',
}))

@@ -103,0 +96,0 @@ .withBlock(visitedOperationVariables).string;

@@ -1,3 +0,3 @@

import { BaseVisitor } from './base-visitor';
import { ScalarsMap, NamingConvention, ConvertFn } from './types';
import { ParsedConfig, RawConfig, BaseVisitor } from './base-visitor';
import { ScalarsMap } from './types';
import { DeclarationBlockConfig } from './utils';

@@ -8,6 +8,3 @@ import { NameNode, ListTypeNode, NamedTypeNode, FieldDefinitionNode, ObjectTypeDefinitionNode, GraphQLSchema, NonNullTypeNode, UnionTypeDefinitionNode, ScalarTypeDefinitionNode, InterfaceTypeDefinitionNode } from 'graphql';

import { ParsedMapper } from './mappers';
export interface ParsedResolversConfig {
scalars: ScalarsMap;
convert: ConvertFn;
typesPrefix: string;
export interface ParsedResolversConfig extends ParsedConfig {
contextType: ParsedMapper;

@@ -18,10 +15,43 @@ mappers: {

}
export interface RawResolversConfig {
export interface RawResolversConfig extends RawConfig {
/**
* @name contextType
* @type string
* @description Use this configuration to set a custom type for your `context`, and it will
* effect all the resolvers, without the need to override it using generics each time.
* If you wish to use an external type and import it from another file, you can use `add` plugin
* and add the required `import` statement, or you can use a `module#type` syntax.
*
* @example Custom Context Type
* ```yml
* plugins
* config:
* contextType: MyContext
* ```
* @example Custom Context Type
* ```yml
* plugins
* config:
* contextType: ./my-types#MyContext
* ```
*/
contextType?: string;
/**
* @name mappers
* @type Object
* @description Replaces a GraphQL type usage with a custom type, allowing you to return custom object from
* your resolvers.
* You can use a `module#type` syntax.
*
* @example Custom Context Type
* ```yml
* plugins
* config:
* mappers:
* User: ./my-models#UserDbObject
* ```
*/
mappers?: {
[typeName: string]: string;
};
scalars?: ScalarsMap;
namingConvention?: NamingConvention;
typesPrefix?: string;
}

@@ -28,0 +58,0 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig = RawResolversConfig, TPluginConfig extends ParsedResolversConfig = ParsedResolversConfig> extends BaseVisitor<TRawConfig, TPluginConfig> {

@@ -104,5 +104,3 @@ "use strict";

const realType = utils_1.getBaseTypeNode(original.type).name.value;
const mappedType = this.config.mappers[realType]
? this._variablesTransfomer.wrapAstTypeWithModifiers(this.config.mappers[realType].type, original.type)
: node.type;
const mappedType = this.config.mappers[realType] ? this._variablesTransfomer.wrapAstTypeWithModifiers(this.config.mappers[realType].type, original.type) : node.type;
const subscriptionType = this._schema.getSubscriptionType();

@@ -112,6 +110,6 @@ const isSubscriptionType = subscriptionType && subscriptionType.name === parentName;

? `, ${this.convertName(parentName, {
useTypesPrefix: true
useTypesPrefix: true,
}) +
this.convertName(node.name, {
useTypesPrefix: false
useTypesPrefix: false,
}) +

@@ -124,3 +122,3 @@ 'Args'}`

const name = this.convertName(node, {
suffix: 'Resolvers'
suffix: 'Resolvers',
});

@@ -136,3 +134,3 @@ let type = null;

.export()
.asKind('interface')
.asKind('type')
.withName(name, `<Context = ${this.config.contextType.type}, ParentType = ${type}>`)

@@ -145,3 +143,3 @@ .withBlock(node.fields.map((f) => f(node.name)).join('\n'));

const name = this.convertName(node, {
suffix: 'Resolvers'
suffix: 'Resolvers',
});

@@ -156,3 +154,3 @@ const originalNode = parent[key];

.export()
.asKind('interface')
.asKind('type')
.withName(name, `<Context = ${this.config.contextType.type}, ParentType = ${node.name}>`)

@@ -165,7 +163,9 @@ .withBlock(utils_1.indent(`__resolveType: TypeResolveFn<${possibleTypes}>`)).string;

this._collectedResolvers[node.name] = 'GraphQLScalarType';
return new utils_1.DeclarationBlock(this._declarationBlockConfig)
return new utils_1.DeclarationBlock(Object.assign({}, this._declarationBlockConfig, { blockTransformer(block) {
return block;
} }))
.export()
.asKind('interface')
.withName(this.convertName(node, {
suffix: 'ScalarConfig'
suffix: 'ScalarConfig',
}), ` extends GraphQLScalarTypeConfig<${baseName}, any>`)

@@ -176,10 +176,10 @@ .withBlock(utils_1.indent(`name: '${node.name}'`)).string;

const directiveName = this.convertName(node, {
suffix: 'DirectiveResolver'
suffix: 'DirectiveResolver',
});
const hasArguments = node.arguments && node.arguments.length > 0;
const directiveArgs = hasArguments
? this._variablesTransfomer.transform(node.arguments)
: '';
const directiveArgs = hasArguments ? this._variablesTransfomer.transform(node.arguments) : '';
this._collectedDirectiveResolvers[node.name] = directiveName + '<any, any, Context>';
return new utils_1.DeclarationBlock(this._declarationBlockConfig)
return new utils_1.DeclarationBlock(Object.assign({}, this._declarationBlockConfig, { blockTransformer(block) {
return block;
} }))
.export()

@@ -192,3 +192,3 @@ .asKind('type')

const name = this.convertName(node, {
suffix: 'Resolvers'
suffix: 'Resolvers',
});

@@ -208,8 +208,5 @@ const allTypesMap = this._schema.getTypeMap();

.export()
.asKind('interface')
.asKind('type')
.withName(name, `<Context = ${this.config.contextType.type}, ParentType = ${node.name}>`)
.withBlock([
utils_1.indent(`__resolveType: TypeResolveFn<${implementingTypes.map(name => `'${name}'`).join(' | ')}>,`),
...(node.fields || []).map((f) => f(node.name))
].join('\n')).string;
.withBlock([utils_1.indent(`__resolveType: TypeResolveFn<${implementingTypes.map(name => `'${name}'`).join(' | ')}>,`), ...(node.fields || []).map((f) => f(node.name))].join('\n')).string;
}

@@ -216,0 +213,0 @@ SchemaDefinition() {

@@ -10,2 +10,15 @@ import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor';

export interface RawTypesConfig extends RawConfig {
/**
* @name enumValues
* @type EnumValuesMap
* @description Overrides the default value of enum values declared in your GraphQL schema.
*
* @example
* ```yml
* config:
* enumValues:
* MyEnum:
* A: 'foo'
* ```
*/
enumValues?: EnumValuesMap;

@@ -12,0 +25,0 @@ }

@@ -53,5 +53,3 @@ "use strict";

const originalNode = parent[key];
const possibleTypes = originalNode.types
.map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value) : this.convertName(t)))
.join(' | ');
const possibleTypes = originalNode.types.map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value) : this.convertName(t))).join(' | ');
return new utils_1.DeclarationBlock(this._declarationBlockConfig)

@@ -65,5 +63,3 @@ .export()

const originalNode = parent[key];
const interfaces = originalNode.interfaces && node.interfaces.length > 0
? originalNode.interfaces.map(i => this.convertName(i)).join(' & ') + ' & '
: '';
const interfaces = originalNode.interfaces && node.interfaces.length > 0 ? originalNode.interfaces.map(i => this.convertName(i)).join(' & ') + ' & ' : '';
const typeDefinition = new utils_1.DeclarationBlock(this._declarationBlockConfig)

@@ -80,3 +76,3 @@ .export()

this.convertName(field, {
useTypesPrefix: false
useTypesPrefix: false,
}) +

@@ -111,5 +107,3 @@ 'Args';

buildEnumValuesBlock(values) {
return values
.map(enumOption => utils_1.indent(`${this.convertName(enumOption)}${this._declarationBlockConfig.enumNameValueSeparator} ${utils_1.wrapWithSingleQuotes(this.config.enumValues[enumOption.name] || enumOption.name)}`))
.join(',\n');
return values.map(enumOption => utils_1.indent(`${this.convertName(enumOption)}${this._declarationBlockConfig.enumNameValueSeparator} ${utils_1.wrapWithSingleQuotes(this.config.enumValues[enumOption.name] || enumOption.name)}`)).join(',\n');
}

@@ -116,0 +110,0 @@ DirectiveDefinition(node) {

@@ -13,4 +13,57 @@ import { ScalarsMap, NamingConvention, ConvertFn, ConvertOptions } from './types';

export interface RawConfig {
/**
* @name scalars
* @type ScalarsMap
* @description Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.
*
* @example
* ```yml
* config:
* scalars:
* DateTime: Date
* JSON: { [key: string]: any }
* ```
*/
scalars?: ScalarsMap;
/**
* @name namingConvention
* @type NamingConvention
* @default change-case#pascalCase
* @description Allow you to ovderride the naming convention of the output.
* You can either override all namings, or specify an object with specific custom naming convention per output.
* The format of the converter must be a valid `module#method`.
* Allowed values for specific output are: `typeNames`, `enumValues`.
* You can also use "keep" to keep all GraphQL names as-is.
*
* @example Override All Names
* ```yml
* config:
* namingConvention: change-case#lowerCase
* ```
* @example Upper-case enum values
* ```yml
* config:
* namingConvention:
* typeNames: change-case#pascalCase
* enumValues: change-case#upperCase
* ```
* @example Keep
* ```yml
* config:
* namingConvention: keep
* ```
*/
namingConvention?: NamingConvention;
/**
* @name typesPrefix
* @type string
* @default ""
* @description Prefixes all the generated types.
*
* @example Add "I" Prefix
* ```yml
* config:
* typesPrefix: I
* ```
*/
typesPrefix?: string;

@@ -17,0 +70,0 @@ }

@@ -8,3 +8,34 @@ import { BaseVisitor, ParsedConfig, RawConfig } from './index';

export interface ClientSideBasePluginConfig extends ParsedConfig {
/**
* @name noGraphQLTag
* @type boolean
* @default false
* @description Instead of adding gql tag with the GraphQL operation, it uses the percompiled JSON representation (DocumentNode)
* of the operation.
*
* @example
* ```yml
* config:
* noGraphQLTag: true
* ```
*/
noGraphQLTag: boolean;
/**
* @name gqlImport
* @type string
* @default gql#graphql-tag
* @description Customize from which module will `gql` be imported from.
* This is useful if you want to use modules other than `graphql-tag`, e.g. `graphql.macro`.
*
* @example graphql.macro
* ```yml
* config:
* gqlImport: graphql.macro#gql
* ```
* @example Gatsby
* ```yml
* config:
* gqlImport: gatsby#graphql
* ```
*/
gqlImport: string;

@@ -11,0 +42,0 @@ }

@@ -12,2 +12,3 @@ import { NameNode, TypeNode, NamedTypeNode, GraphQLObjectType, GraphQLNonNull, GraphQLList, GraphQLOutputType, GraphQLNamedType, GraphQLSchema } from 'graphql';

blockWrapper?: string;
blockTransformer?: (block: string) => string;
enumNameValueSeparator?: string;

@@ -14,0 +15,0 @@ }

@@ -61,3 +61,3 @@ "use strict";

this._nameGenerics = null;
this._config = Object.assign({ blockWrapper: '', enumNameValueSeparator: ':' }, this._config);
this._config = Object.assign({ blockWrapper: '', blockTransformer: block => block, enumNameValueSeparator: ':' }, this._config);
}

@@ -109,11 +109,10 @@ export(exp = true) {

}
const before = '{' + this._config.blockWrapper;
const after = this._config.blockWrapper + '}';
const block = [before, this._block, after].join('\n');
if (this._methodName) {
result += `${this._methodName}({${this._config.blockWrapper}
${this._block}
${this._config.blockWrapper}})`;
result += `${this._methodName}(${this._config.blockTransformer(block)})`;
}
else {
result += `{${this._config.blockWrapper}
${this._block}
${this._config.blockWrapper}}`;
result += this._config.blockTransformer(block);
}

@@ -120,0 +119,0 @@ }

@@ -1,2 +0,2 @@

import { ScalarsMap, NamingConvention, ConvertFn, ConvertOptions } from './types';
import { ScalarsMap, ConvertOptions } from './types';
import { DeclarationBlockConfig } from './utils';

@@ -6,23 +6,28 @@ import { GraphQLSchema, FragmentDefinitionNode, OperationDefinitionNode, ASTNode } from 'graphql';

import { OperationVariablesToObject } from './variables-to-object';
import { BaseVisitorConvertOptions } from './base-visitor';
export interface ParsedDocumentsConfig {
scalars: ScalarsMap;
convert: ConvertFn<ConvertOptions>;
typesPrefix: string;
import { RawConfig, ParsedConfig, BaseVisitor, BaseVisitorConvertOptions } from './base-visitor';
export interface ParsedDocumentsConfig extends ParsedConfig {
addTypename: boolean;
}
export interface RawDocumentsConfig {
scalars?: ScalarsMap;
namingConvention?: NamingConvention;
typesPrefix?: string;
export interface RawDocumentsConfig extends RawConfig {
/**
* @name skipTypename
* @type boolean
* @default false
* @description Automatically adds `__typename` field to the generated types, even when they are not specified
* in the selection set.
*
* @example
* ```yml
* config:
* skipTypename: true
* ```
*/
skipTypename?: boolean;
}
export declare class BaseDocumentsVisitor<TRawConfig extends RawDocumentsConfig = RawDocumentsConfig, TPluginConfig extends ParsedDocumentsConfig = ParsedDocumentsConfig> {
export declare class BaseDocumentsVisitor<TRawConfig extends RawDocumentsConfig = RawDocumentsConfig, TPluginConfig extends ParsedDocumentsConfig = ParsedDocumentsConfig> extends BaseVisitor<TRawConfig, TPluginConfig> {
protected _schema: GraphQLSchema;
protected _parsedConfig: TPluginConfig;
protected _declarationBlockConfig: DeclarationBlockConfig;
protected _unnamedCounter: number;
protected _variablesTransfomer: OperationVariablesToObject;
protected _selectionSetToObject: SelectionSetToObject;
constructor(rawConfig: TRawConfig, additionalConfig: TPluginConfig, _schema: GraphQLSchema, defaultScalars?: ScalarsMap);
constructor(rawConfig: TRawConfig, additionalConfig: TPluginConfig, _schema: GraphQLSchema, scalars?: ScalarsMap);
setSelectionSetHandler(handler: SelectionSetToObject): void;

@@ -32,5 +37,3 @@ setDeclarationBlockConfig(config: DeclarationBlockConfig): void;

convertName(node: ASTNode | string, options?: ConvertOptions & BaseVisitorConvertOptions): string;
readonly config: TPluginConfig;
readonly schema: GraphQLSchema;
readonly scalars: ScalarsMap;
readonly addTypename: boolean;

@@ -37,0 +40,0 @@ private handleAnonymouseOperation;

@@ -5,3 +5,3 @@ import * as autoBind from 'auto-bind';

import { OperationVariablesToObject } from './variables-to-object';
import { convertFactory } from './naming';
import { BaseVisitor } from './base-visitor';
function getRootType(operation, schema) {

@@ -17,8 +17,7 @@ switch (operation) {

}
export class BaseDocumentsVisitor {
constructor(rawConfig, additionalConfig, _schema, defaultScalars = DEFAULT_SCALARS) {
export class BaseDocumentsVisitor extends BaseVisitor {
constructor(rawConfig, additionalConfig, _schema, scalars = DEFAULT_SCALARS) {
super(rawConfig, Object.assign({ addTypename: !rawConfig.skipTypename }, (additionalConfig || {})), scalars);
this._schema = _schema;
this._declarationBlockConfig = {};
this._unnamedCounter = 1;
this._parsedConfig = Object.assign({ addTypename: !rawConfig.skipTypename, scalars: Object.assign({}, (defaultScalars || DEFAULT_SCALARS), (rawConfig.scalars || {})), convert: convertFactory(rawConfig), typesPrefix: rawConfig.typesPrefix || '' }, (additionalConfig || {}));
autoBind(this);

@@ -40,11 +39,5 @@ this._variablesTransfomer = new OperationVariablesToObject(this.scalars, this.convertName);

}
get config() {
return this._parsedConfig;
}
get schema() {
return this._schema;
}
get scalars() {
return this.config.scalars;
}
get addTypename() {

@@ -57,3 +50,3 @@ return this._parsedConfig.addTypename;

return this.convertName(node, {
useTypesPrefix: false
useTypesPrefix: false,
});

@@ -64,3 +57,3 @@ }

suffix: '_',
useTypesPrefix: false
useTypesPrefix: false,
});

@@ -76,3 +69,3 @@ }

useTypesPrefix: true,
suffix: 'Fragment'
suffix: 'Fragment',
}))

@@ -90,3 +83,3 @@ .withContent(selectionSet.string).string;

.withName(this.convertName(name, {
suffix: toPascalCase(node.operation)
suffix: toPascalCase(node.operation),
}))

@@ -98,3 +91,3 @@ .withContent(selectionSet.string).string;

.withName(this.convertName(name, {
suffix: toPascalCase(node.operation) + 'Variables'
suffix: toPascalCase(node.operation) + 'Variables',
}))

@@ -101,0 +94,0 @@ .withBlock(visitedOperationVariables).string;

@@ -1,3 +0,3 @@

import { BaseVisitor } from './base-visitor';
import { ScalarsMap, NamingConvention, ConvertFn } from './types';
import { ParsedConfig, RawConfig, BaseVisitor } from './base-visitor';
import { ScalarsMap } from './types';
import { DeclarationBlockConfig } from './utils';

@@ -8,6 +8,3 @@ import { NameNode, ListTypeNode, NamedTypeNode, FieldDefinitionNode, ObjectTypeDefinitionNode, GraphQLSchema, NonNullTypeNode, UnionTypeDefinitionNode, ScalarTypeDefinitionNode, InterfaceTypeDefinitionNode } from 'graphql';

import { ParsedMapper } from './mappers';
export interface ParsedResolversConfig {
scalars: ScalarsMap;
convert: ConvertFn;
typesPrefix: string;
export interface ParsedResolversConfig extends ParsedConfig {
contextType: ParsedMapper;

@@ -18,10 +15,43 @@ mappers: {

}
export interface RawResolversConfig {
export interface RawResolversConfig extends RawConfig {
/**
* @name contextType
* @type string
* @description Use this configuration to set a custom type for your `context`, and it will
* effect all the resolvers, without the need to override it using generics each time.
* If you wish to use an external type and import it from another file, you can use `add` plugin
* and add the required `import` statement, or you can use a `module#type` syntax.
*
* @example Custom Context Type
* ```yml
* plugins
* config:
* contextType: MyContext
* ```
* @example Custom Context Type
* ```yml
* plugins
* config:
* contextType: ./my-types#MyContext
* ```
*/
contextType?: string;
/**
* @name mappers
* @type Object
* @description Replaces a GraphQL type usage with a custom type, allowing you to return custom object from
* your resolvers.
* You can use a `module#type` syntax.
*
* @example Custom Context Type
* ```yml
* plugins
* config:
* mappers:
* User: ./my-models#UserDbObject
* ```
*/
mappers?: {
[typeName: string]: string;
};
scalars?: ScalarsMap;
namingConvention?: NamingConvention;
typesPrefix?: string;
}

@@ -28,0 +58,0 @@ export declare class BaseResolversVisitor<TRawConfig extends RawResolversConfig = RawResolversConfig, TPluginConfig extends ParsedResolversConfig = ParsedResolversConfig> extends BaseVisitor<TRawConfig, TPluginConfig> {

@@ -102,5 +102,3 @@ import { BaseVisitor } from './base-visitor';

const realType = getBaseTypeNode(original.type).name.value;
const mappedType = this.config.mappers[realType]
? this._variablesTransfomer.wrapAstTypeWithModifiers(this.config.mappers[realType].type, original.type)
: node.type;
const mappedType = this.config.mappers[realType] ? this._variablesTransfomer.wrapAstTypeWithModifiers(this.config.mappers[realType].type, original.type) : node.type;
const subscriptionType = this._schema.getSubscriptionType();

@@ -110,6 +108,6 @@ const isSubscriptionType = subscriptionType && subscriptionType.name === parentName;

? `, ${this.convertName(parentName, {
useTypesPrefix: true
useTypesPrefix: true,
}) +
this.convertName(node.name, {
useTypesPrefix: false
useTypesPrefix: false,
}) +

@@ -122,3 +120,3 @@ 'Args'}`

const name = this.convertName(node, {
suffix: 'Resolvers'
suffix: 'Resolvers',
});

@@ -134,3 +132,3 @@ let type = null;

.export()
.asKind('interface')
.asKind('type')
.withName(name, `<Context = ${this.config.contextType.type}, ParentType = ${type}>`)

@@ -143,3 +141,3 @@ .withBlock(node.fields.map((f) => f(node.name)).join('\n'));

const name = this.convertName(node, {
suffix: 'Resolvers'
suffix: 'Resolvers',
});

@@ -154,3 +152,3 @@ const originalNode = parent[key];

.export()
.asKind('interface')
.asKind('type')
.withName(name, `<Context = ${this.config.contextType.type}, ParentType = ${node.name}>`)

@@ -163,7 +161,9 @@ .withBlock(indent(`__resolveType: TypeResolveFn<${possibleTypes}>`)).string;

this._collectedResolvers[node.name] = 'GraphQLScalarType';
return new DeclarationBlock(this._declarationBlockConfig)
return new DeclarationBlock(Object.assign({}, this._declarationBlockConfig, { blockTransformer(block) {
return block;
} }))
.export()
.asKind('interface')
.withName(this.convertName(node, {
suffix: 'ScalarConfig'
suffix: 'ScalarConfig',
}), ` extends GraphQLScalarTypeConfig<${baseName}, any>`)

@@ -174,10 +174,10 @@ .withBlock(indent(`name: '${node.name}'`)).string;

const directiveName = this.convertName(node, {
suffix: 'DirectiveResolver'
suffix: 'DirectiveResolver',
});
const hasArguments = node.arguments && node.arguments.length > 0;
const directiveArgs = hasArguments
? this._variablesTransfomer.transform(node.arguments)
: '';
const directiveArgs = hasArguments ? this._variablesTransfomer.transform(node.arguments) : '';
this._collectedDirectiveResolvers[node.name] = directiveName + '<any, any, Context>';
return new DeclarationBlock(this._declarationBlockConfig)
return new DeclarationBlock(Object.assign({}, this._declarationBlockConfig, { blockTransformer(block) {
return block;
} }))
.export()

@@ -190,3 +190,3 @@ .asKind('type')

const name = this.convertName(node, {
suffix: 'Resolvers'
suffix: 'Resolvers',
});

@@ -206,8 +206,5 @@ const allTypesMap = this._schema.getTypeMap();

.export()
.asKind('interface')
.asKind('type')
.withName(name, `<Context = ${this.config.contextType.type}, ParentType = ${node.name}>`)
.withBlock([
indent(`__resolveType: TypeResolveFn<${implementingTypes.map(name => `'${name}'`).join(' | ')}>,`),
...(node.fields || []).map((f) => f(node.name))
].join('\n')).string;
.withBlock([indent(`__resolveType: TypeResolveFn<${implementingTypes.map(name => `'${name}'`).join(' | ')}>,`), ...(node.fields || []).map((f) => f(node.name))].join('\n')).string;
}

@@ -214,0 +211,0 @@ SchemaDefinition() {

@@ -10,2 +10,15 @@ import { BaseVisitor, ParsedConfig, RawConfig } from './base-visitor';

export interface RawTypesConfig extends RawConfig {
/**
* @name enumValues
* @type EnumValuesMap
* @description Overrides the default value of enum values declared in your GraphQL schema.
*
* @example
* ```yml
* config:
* enumValues:
* MyEnum:
* A: 'foo'
* ```
*/
enumValues?: EnumValuesMap;

@@ -12,0 +25,0 @@ }

@@ -51,5 +51,3 @@ import { BaseVisitor } from './base-visitor';

const originalNode = parent[key];
const possibleTypes = originalNode.types
.map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value) : this.convertName(t)))
.join(' | ');
const possibleTypes = originalNode.types.map(t => (this.scalars[t.name.value] ? this._getScalar(t.name.value) : this.convertName(t))).join(' | ');
return new DeclarationBlock(this._declarationBlockConfig)

@@ -63,5 +61,3 @@ .export()

const originalNode = parent[key];
const interfaces = originalNode.interfaces && node.interfaces.length > 0
? originalNode.interfaces.map(i => this.convertName(i)).join(' & ') + ' & '
: '';
const interfaces = originalNode.interfaces && node.interfaces.length > 0 ? originalNode.interfaces.map(i => this.convertName(i)).join(' & ') + ' & ' : '';
const typeDefinition = new DeclarationBlock(this._declarationBlockConfig)

@@ -78,3 +74,3 @@ .export()

this.convertName(field, {
useTypesPrefix: false
useTypesPrefix: false,
}) +

@@ -109,5 +105,3 @@ 'Args';

buildEnumValuesBlock(values) {
return values
.map(enumOption => indent(`${this.convertName(enumOption)}${this._declarationBlockConfig.enumNameValueSeparator} ${wrapWithSingleQuotes(this.config.enumValues[enumOption.name] || enumOption.name)}`))
.join(',\n');
return values.map(enumOption => indent(`${this.convertName(enumOption)}${this._declarationBlockConfig.enumNameValueSeparator} ${wrapWithSingleQuotes(this.config.enumValues[enumOption.name] || enumOption.name)}`)).join(',\n');
}

@@ -114,0 +108,0 @@ DirectiveDefinition(node) {

@@ -13,4 +13,57 @@ import { ScalarsMap, NamingConvention, ConvertFn, ConvertOptions } from './types';

export interface RawConfig {
/**
* @name scalars
* @type ScalarsMap
* @description Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type.
*
* @example
* ```yml
* config:
* scalars:
* DateTime: Date
* JSON: { [key: string]: any }
* ```
*/
scalars?: ScalarsMap;
/**
* @name namingConvention
* @type NamingConvention
* @default change-case#pascalCase
* @description Allow you to ovderride the naming convention of the output.
* You can either override all namings, or specify an object with specific custom naming convention per output.
* The format of the converter must be a valid `module#method`.
* Allowed values for specific output are: `typeNames`, `enumValues`.
* You can also use "keep" to keep all GraphQL names as-is.
*
* @example Override All Names
* ```yml
* config:
* namingConvention: change-case#lowerCase
* ```
* @example Upper-case enum values
* ```yml
* config:
* namingConvention:
* typeNames: change-case#pascalCase
* enumValues: change-case#upperCase
* ```
* @example Keep
* ```yml
* config:
* namingConvention: keep
* ```
*/
namingConvention?: NamingConvention;
/**
* @name typesPrefix
* @type string
* @default ""
* @description Prefixes all the generated types.
*
* @example Add "I" Prefix
* ```yml
* config:
* typesPrefix: I
* ```
*/
typesPrefix?: string;

@@ -17,0 +70,0 @@ }

@@ -8,3 +8,34 @@ import { BaseVisitor, ParsedConfig, RawConfig } from './index';

export interface ClientSideBasePluginConfig extends ParsedConfig {
/**
* @name noGraphQLTag
* @type boolean
* @default false
* @description Instead of adding gql tag with the GraphQL operation, it uses the percompiled JSON representation (DocumentNode)
* of the operation.
*
* @example
* ```yml
* config:
* noGraphQLTag: true
* ```
*/
noGraphQLTag: boolean;
/**
* @name gqlImport
* @type string
* @default gql#graphql-tag
* @description Customize from which module will `gql` be imported from.
* This is useful if you want to use modules other than `graphql-tag`, e.g. `graphql.macro`.
*
* @example graphql.macro
* ```yml
* config:
* gqlImport: graphql.macro#gql
* ```
* @example Gatsby
* ```yml
* config:
* gqlImport: gatsby#graphql
* ```
*/
gqlImport: string;

@@ -11,0 +42,0 @@ }

@@ -12,2 +12,3 @@ import { NameNode, TypeNode, NamedTypeNode, GraphQLObjectType, GraphQLNonNull, GraphQLList, GraphQLOutputType, GraphQLNamedType, GraphQLSchema } from 'graphql';

blockWrapper?: string;
blockTransformer?: (block: string) => string;
enumNameValueSeparator?: string;

@@ -14,0 +15,0 @@ }

@@ -53,3 +53,3 @@ import { pascalCase } from 'change-case';

this._nameGenerics = null;
this._config = Object.assign({ blockWrapper: '', enumNameValueSeparator: ':' }, this._config);
this._config = Object.assign({ blockWrapper: '', blockTransformer: block => block, enumNameValueSeparator: ':' }, this._config);
}

@@ -101,11 +101,10 @@ export(exp = true) {

}
const before = '{' + this._config.blockWrapper;
const after = this._config.blockWrapper + '}';
const block = [before, this._block, after].join('\n');
if (this._methodName) {
result += `${this._methodName}({${this._config.blockWrapper}
${this._block}
${this._config.blockWrapper}})`;
result += `${this._methodName}(${this._config.blockTransformer(block)})`;
}
else {
result += `{${this._config.blockWrapper}
${this._block}
${this._config.blockWrapper}}`;
result += this._config.blockTransformer(block);
}

@@ -112,0 +111,0 @@ }

{
"name": "@graphql-codegen/visitor-plugin-common",
"version": "0.18.1-alpha.59+c8b59c87",
"version": "0.18.1-alpha.60+9266437a",
"license": "MIT",

@@ -9,3 +9,3 @@ "scripts": {

"dependencies": {
"@graphql-codegen/plugin-helpers": "0.18.1-alpha.59+c8b59c87",
"@graphql-codegen/plugin-helpers": "0.18.1-alpha.60+9266437a",
"auto-bind": "2.0.0",

@@ -20,3 +20,3 @@ "dependency-graph": "0.8.0",

"devDependencies": {
"@graphql-codegen/testing": "0.18.1-alpha.59+c8b59c87",
"@graphql-codegen/testing": "0.18.1-alpha.60+9266437a",
"@types/graphql": "14.0.7",

@@ -36,3 +36,3 @@ "graphql": "14.1.1",

},
"gitHead": "c8b59c8737fdc9e68dd8dbb3b8640ef4d64b7aaf"
"gitHead": "9266437a65c88485c4d649f4d81cc7a99c154288"
}

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

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc