How to use the @graphql-codegen/visitor-plugin-common.DeclarationBlock function in @graphql-codegen/visitor-plugin-common

To help you get started, we’ve selected a few @graphql-codegen/visitor-plugin-common examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dotansimha / graphql-code-generator / packages / plugins / typescript / typescript / src / visitor.ts View on Github external
EnumTypeDefinition(node: EnumTypeDefinitionNode): string {
    const enumName = (node.name as any) as string;

    // In case of mapped external enum string
    if (this.config.enumValues[enumName] && this.config.enumValues[enumName].sourceFile) {
      return `export { ${this.config.enumValues[enumName].typeIdentifier} };\n`;
    }

    const enumTypeName = this.convertName(node, { useTypesPrefix: this.config.enumPrefix });

    if (this.config.enumsAsTypes) {
      return new DeclarationBlock(this._declarationBlockConfig)
        .export()
        .asKind('type')
        .withComment((node.description as any) as string)
        .withName(enumTypeName)
        .withContent(
          '\n' +
            node.values
              .map(enumOption => {
                let enumValue: string = (enumOption.name as any) as string;
                const comment = transformComment((enumOption.description as any) as string, 1);

                if (this.config.enumValues[enumName] && this.config.enumValues[enumName].mappedValues && this.config.enumValues[enumName].mappedValues[enumValue]) {
                  enumValue = this.config.enumValues[enumName].mappedValues[enumValue];
                }

                return comment + indent(wrapWithSingleQuotes(enumValue));
github dotansimha / graphql-code-generator / packages / plugins / typescript / mongodb / src / visitor.ts View on Github external
ObjectTypeDefinition(node: ObjectTypeDefinitionNode): string {
    const entityDirective = this._getDirectiveFromAstNode(node, Directives.ENTITY);

    if (entityDirective === null) {
      return null;
    }

    const implementingInterfaces = this._buildInterfaces(node.interfaces);
    const fields = this._buildFieldsTree(node.fields);
    const additionalFields = this._getDirectiveArgValue(entityDirective, 'additionalFields');
    this._addAdditionalFields(fields, additionalFields);

    return new DeclarationBlock(this._declarationBlockConfig)
      .export()
      .asKind('type')
      .withName(this.convertName(node, { suffix: this.config.dbTypeSuffix }))
      .withContent(implementingInterfaces.length ? implementingInterfaces.join(' & ') + ' & ' : '')
      .withBlock(fields.string).string;
  }
}
github dotansimha / graphql-code-generator / packages / plugins / typescript / compatibility / src / visitor.ts View on Github external
FragmentDefinition(node: FragmentDefinitionNode): string {
    const baseName = node.name.value;
    const results = [];
    const convertedName = this.convertName(baseName);
    const selectionSetTypes = this.buildFragmentBlock(node);
    const fragmentBlock = this.printTypes(selectionSetTypes);

    if (!this.config.noNamespaces) {
      results.push(
        new DeclarationBlock(this._declarationBlockConfig)
          .export()
          .asKind('namespace')
          .withName(convertedName)
          .withBlock(fragmentBlock).string
      );
    } else {
      results.push(fragmentBlock);
    }

    return results.join('\n');
  }
github dotansimha / graphql-code-generator / packages / plugins / flow / flow / src / visitor.ts View on Github external
node.values
          .map(enumOption => {
            const comment = transformComment((enumOption.description as any) as string, 1);
            const optionName = this.convertName(enumOption, { transformUnderscore: true, useTypesPrefix: false });
            let enumValue: string = (enumOption.name as any) as string;

            if (this.config.enumValues[typeName] && this.config.enumValues[typeName].mappedValues && this.config.enumValues[typeName].mappedValues[enumValue]) {
              enumValue = this.config.enumValues[typeName].mappedValues[enumValue];
            }

            return comment + indent(`${optionName}: ${wrapWithSingleQuotes(enumValue)}`);
          })
          .join(', \n')
      ).string;

    const enumType = new DeclarationBlock(this._declarationBlockConfig)
      .export()
      .asKind('type')
      .withName(this.convertName(node, { useTypesPrefix: this.config.enumPrefix }))
      .withComment((node.description as any) as string)
      .withContent(`$Values`).string;

    return [enumValues, enumType].join('\n\n');
  }
}
github dotansimha / graphql-code-generator / packages / plugins / typescript / mongodb / src / visitor.ts View on Github external
}

        return null;
      })
      .filter(a => a);

    if (possibleTypes.length === 0) {
      return null;
    }

    const additionalFields = this._getDirectiveArgValue(unionDirective, 'additionalFields');
    const fields = new FieldsTree();
    fields.addField(discriminatorField, this.scalars.String);
    this._addAdditionalFields(fields, additionalFields);

    return new DeclarationBlock(this._declarationBlockConfig)
      .export()
      .asKind('type')
      .withName(this.convertName(node, { suffix: this.config.dbTypeSuffix }))
      .withContent(`(${possibleTypes.join(' | ')}) & `)
      .withBlock(fields.string).string;
  }
github dotansimha / graphql-code-generator / packages / plugins / typescript / type-graphql / src / visitor.ts View on Github external
getArgumentsObjectDeclarationBlock(node: InterfaceTypeDefinitionNode | ObjectTypeDefinitionNode, name: string, field: FieldDefinitionNode): DeclarationBlock {
    return new DeclarationBlock(this._declarationBlockConfig)
      .export()
      .asKind(this._parsedConfig.declarationKind.arguments)
      .withName(this.convertName(name))
      .withComment(node.description)
      .withBlock(field.arguments.map(argument => this.InputValueDefinition(argument)).join('\n'));
  }
github dotansimha / graphql-code-generator / packages / plugins / flow / resolvers / src / visitor.ts View on Github external
ScalarTypeDefinition(node: ScalarTypeDefinitionNode): string {
    const nameAsString = (node.name as any) as string;
    const baseName = this.getTypeToUse(nameAsString);
    this._collectedResolvers[node.name as any] = 'GraphQLScalarType';

    return new DeclarationBlock({
      ...this._declarationBlockConfig,
      blockTransformer(block) {
        return block;
      },
    })
      .export()
      .asKind('type')
      .withName(
        this.convertName(node, {
          suffix: 'ScalarConfig',
        })
      )
      .withBlock([indent(`...GraphQLScalarTypeConfig<${baseName}, any>`), indent(`name: '${node.name}'`)].join(', \n')).string;
  }
}
github dotansimha / graphql-code-generator / packages / plugins / typescript / mongodb / src / visitor.ts View on Github external
InterfaceTypeDefinition(node: InterfaceTypeDefinitionNode): string {
    const abstractEntityDirective = this._getDirectiveFromAstNode(node, Directives.ABSTRACT_ENTITY);

    if (abstractEntityDirective === null) {
      return null;
    }

    const discriminatorField = this._getDirectiveArgValue(abstractEntityDirective, 'discriminatorField');
    const additionalFields = this._getDirectiveArgValue(abstractEntityDirective, 'additionalFields');
    const fields = this._buildFieldsTree(node.fields);
    fields.addField(discriminatorField, this.scalars.String);
    this._addAdditionalFields(fields, additionalFields);

    return new DeclarationBlock(this._declarationBlockConfig)
      .export()
      .asKind('type')
      .withName(this.convertName(node, { suffix: this.config.dbInterfaceSuffix }))
      .withBlock(fields.string).string;
  }
github dotansimha / graphql-code-generator / packages / plugins / typescript / compatibility / src / visitor.ts View on Github external
};
      }

      if (hooks) {
        selectionSetTypes['use' + prefix] = {
          export: 'const',
          name: 'use' + this.convertName(baseName, { suffix: toPascalCase(node.operation) }),
        };
      }
    }

    const operationsBlock = this.printTypes(selectionSetTypes);

    if (!this.config.noNamespaces) {
      results.push(
        new DeclarationBlock(this._declarationBlockConfig)
          .export()
          .asKind('namespace')
          .withName(convertedName)
          .withBlock(operationsBlock).string
      );
    } else {
      results.push(operationsBlock);
    }

    return results.join('\n');
  }
}