UNPKG

apollo-codegen

Version:

Generate API code or type annotations based on a GraphQL schema and query documents

68 lines (57 loc) 2.29 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.possibleTypesForType = possibleTypesForType; exports.typeNameFromGraphQLType = typeNameFromGraphQLType; exports.fieldTypeEnum = fieldTypeEnum; var _printing = require('../utilities/printing'); var _changeCase = require('change-case'); var _graphql = require('graphql'); var builtInScalarMap = { [_graphql.GraphQLString.name]: 'String', [_graphql.GraphQLInt.name]: 'Int', [_graphql.GraphQLFloat.name]: 'Double', [_graphql.GraphQLBoolean.name]: 'Bool', [_graphql.GraphQLID.name]: 'GraphQLID' }; function possibleTypesForType(context, type) { if ((0, _graphql.isAbstractType)(type)) { return context.schema.getPossibleTypes(type); } else { return [type]; } } function typeNameFromGraphQLType(context, type, bareTypeName, isOptional) { if (type instanceof _graphql.GraphQLNonNull) { return typeNameFromGraphQLType(context, type.ofType, bareTypeName, isOptional || false); } else if (isOptional === undefined) { isOptional = true; } var typeName = void 0; if (type instanceof _graphql.GraphQLList) { typeName = '[' + typeNameFromGraphQLType(context, type.ofType, bareTypeName) + ']'; } else if (type instanceof _graphql.GraphQLScalarType) { typeName = typeNameForScalarType(context, type); } else { typeName = bareTypeName || type.name; } return isOptional ? typeName + '?' : typeName; } function typeNameForScalarType(context, type) { return builtInScalarMap[type.name] || (context.passthroughCustomScalars ? context.customScalarsPrefix + type.name : _graphql.GraphQLString); } function fieldTypeEnum(context, type, structName) { if (type instanceof _graphql.GraphQLNonNull) { return `.nonNull(${fieldTypeEnum(context, type.ofType, structName)})`; } else if (type instanceof _graphql.GraphQLList) { return `.list(${fieldTypeEnum(context, type.ofType, structName)})`; } else if (type instanceof _graphql.GraphQLScalarType) { return `.scalar(${typeNameForScalarType(context, type)}.self)`; } else if (type instanceof _graphql.GraphQLEnumType) { return `.scalar(${type.name}.self)`; } else if ((0, _graphql.isCompositeType)(type)) { return `.object(${structName}.self)`; } } //# sourceMappingURL=types.js.map