Socket
Socket
Sign inDemoInstall

apollo-codegen-typescript-legacy

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-codegen-typescript-legacy - npm Package Compare versions

Comparing version 0.27.0 to 0.27.3

LICENSE

8

lib/codeGeneration.d.ts

@@ -1,5 +0,5 @@

import { LegacyCompilerContext, LegacyInlineFragment, LegacyFragment, LegacyOperation } from 'apollo-codegen-core/lib/compiler/legacyIR';
import { GraphQLType } from 'graphql';
import CodeGenerator from 'apollo-codegen-core/lib/utilities/CodeGenerator';
import { Property } from './language';
import { LegacyCompilerContext, LegacyInlineFragment, LegacyFragment, LegacyOperation } from "apollo-codegen-core/lib/compiler/legacyIR";
import { GraphQLType } from "graphql";
import CodeGenerator from "apollo-codegen-core/lib/utilities/CodeGenerator";
import { Property } from "./language";
export declare function generateSource(context: LegacyCompilerContext): string;

@@ -6,0 +6,0 @@ export declare function typeDeclarationForGraphQLType(generator: CodeGenerator, type: GraphQLType): void;

@@ -10,4 +10,4 @@ "use strict";

const generator = new CodeGenerator_1.default(context);
generator.printOnNewline('/* tslint:disable */');
generator.printOnNewline('// This file was automatically generated and should not be edited.');
generator.printOnNewline("/* tslint:disable */");
generator.printOnNewline("// This file was automatically generated and should not be edited.");
context.typesUsed.forEach(type => typeDeclarationForGraphQLType(generator, type));

@@ -38,3 +38,3 @@ Object.values(context.operations).forEach(operation => {

generator.printOnNewline(`export enum ${name} {`);
graphql_2.sortEnumValues(values).forEach((value) => {
graphql_2.sortEnumValues(values).forEach(value => {
printDocComment(generator, value.description || undefined, 1);

@@ -49,3 +49,3 @@ generator.printOnNewline(` ${value.value} = "${value.value}",`);

language_1.interfaceDeclaration(generator, {
interfaceName,
interfaceName
}, () => {

@@ -58,9 +58,9 @@ const properties = propertiesFromFields(generator.context, Object.values(type.getFields()));

switch (operationType) {
case 'query':
case "query":
return `${operationName}Query`;
break;
case 'mutation':
case "mutation":
return `${operationName}Mutation`;
break;
case 'subscription':
case "subscription":
return `${operationName}Subscription`;

@@ -76,5 +76,8 @@ break;

}
const interfaceName = `${interfaceNameFromOperation({ operationName, operationType })}Variables`;
const interfaceName = `${interfaceNameFromOperation({
operationName,
operationType
})}Variables`;
language_1.interfaceDeclaration(generator, {
interfaceName,
interfaceName
}, () => {

@@ -97,3 +100,6 @@ const properties = propertiesFromFields(generator.context, variables);

if (type instanceof graphql_1.GraphQLUnionType) {
return type.getTypes().map(type => getObjectTypeName(type)).join(" | ");
return type
.getTypes()
.map(type => getObjectTypeName(type))
.join(" | ");
}

@@ -103,20 +109,24 @@ return `"${type.name}"`;

function updateTypeNameField(rootField) {
const fields = rootField.fields && rootField.fields.map(field => {
if (field.fieldName === '__typename') {
const objectTypeName = getObjectTypeName(rootField.type);
return Object.assign({}, field, { typeName: objectTypeName, type: { name: objectTypeName } });
}
if (field.fields) {
return updateTypeNameField(field);
}
return field;
});
const fields = rootField.fields &&
rootField.fields.map(field => {
if (field.fieldName === "__typename") {
const objectTypeName = getObjectTypeName(rootField.type);
return Object.assign({}, field, { typeName: objectTypeName, type: { name: objectTypeName } });
}
if (field.fields) {
return updateTypeNameField(field);
}
return field;
});
return Object.assign({}, rootField, { fields });
}
function interfaceDeclarationForOperation(generator, { operationName, operationType, fields }) {
const interfaceName = interfaceNameFromOperation({ operationName, operationType });
const interfaceName = interfaceNameFromOperation({
operationName,
operationType
});
fields = fields.map(field => updateTypeNameField(field));
const properties = propertiesFromFields(generator.context, fields);
language_1.interfaceDeclaration(generator, {
interfaceName,
interfaceName
}, () => {

@@ -135,4 +145,3 @@ propertyDeclarations(generator, properties);

if (graphql_1.isAbstractType(typeCondition)) {
const propertySets = fragment.possibleTypes
.map(type => {
const propertySets = fragment.possibleTypes.map(type => {
const inlineFragment = inlineFragments.find(inlineFragment => {

@@ -143,4 +152,6 @@ return inlineFragment.typeCondition.toString() == type.toString();

const fields = inlineFragment.fields.map(field => {
if (field.fieldName === '__typename') {
return Object.assign({}, field, { typeName: `"${inlineFragment.typeCondition}"`, type: { name: `"${inlineFragment.typeCondition}"` } });
if (field.fieldName === "__typename") {
return Object.assign({}, field, { typeName: `"${inlineFragment.typeCondition}"`, type: {
name: `"${inlineFragment.typeCondition}"`
} });
}

@@ -155,3 +166,3 @@ else {

const fragmentFields = fields.map(field => {
if (field.fieldName === '__typename') {
if (field.fieldName === "__typename") {
return Object.assign({}, field, { typeName: `"${type}"`, type: { name: `"${type}"` } });

@@ -170,3 +181,3 @@ }

const fragmentFields = fields.map(field => {
if (field.fieldName === '__typename') {
if (field.fieldName === "__typename") {
return Object.assign({}, field, { typeName: `"${fragment.typeCondition}"`, type: { name: `"${fragment.typeCondition}"` } });

@@ -206,11 +217,16 @@ }

}
else if (fieldType instanceof graphql_1.GraphQLNonNull && fieldType.ofType instanceof graphql_1.GraphQLList) {
else if (fieldType instanceof graphql_1.GraphQLNonNull &&
fieldType.ofType instanceof graphql_1.GraphQLList) {
isArray = true;
isArrayElementNullable = !(fieldType.ofType.ofType instanceof graphql_1.GraphQLNonNull);
}
return Object.assign({}, property, { typeName, fields: field.fields, isComposite: true, fragmentSpreads, inlineFragments, fieldType,
isArray, isNullable, isArrayElementNullable });
return Object.assign({}, property, { typeName, fields: field.fields, isComposite: true, fragmentSpreads,
inlineFragments,
fieldType,
isArray,
isNullable,
isArrayElementNullable });
}
else {
if (field.fieldName === '__typename') {
if (field.fieldName === "__typename") {
const typeName = types_1.typeNameFromGraphQLType(context, fieldType, null, false);

@@ -221,3 +237,4 @@ return Object.assign({}, property, { typeName, isComposite: false, fieldType, isNullable: false });

const typeName = types_1.typeNameFromGraphQLType(context, fieldType, null, isNullable);
return Object.assign({}, property, { typeName, isComposite: false, fieldType, isNullable });
return Object.assign({}, property, { typeName, isComposite: false, fieldType,
isNullable });
}

@@ -232,11 +249,13 @@ }

if (graphql_1.isAbstractType(graphql_1.getNamedType(property.type || property.fieldType))) {
const propertySets = getPossibleTypeNames(generator, property)
.map(type => {
const inlineFragment = property.inlineFragments && property.inlineFragments.find(inlineFragment => {
return inlineFragment.typeCondition.toString() == type;
});
const propertySets = getPossibleTypeNames(generator, property).map(type => {
const inlineFragment = property.inlineFragments &&
property.inlineFragments.find(inlineFragment => {
return inlineFragment.typeCondition.toString() == type;
});
if (inlineFragment) {
const fields = inlineFragment.fields.map(field => {
if (field.fieldName === '__typename') {
return Object.assign({}, field, { typeName: `"${inlineFragment.typeCondition}"`, type: { name: `"${inlineFragment.typeCondition}"` } });
if (field.fieldName === "__typename") {
return Object.assign({}, field, { typeName: `"${inlineFragment.typeCondition}"`, type: {
name: `"${inlineFragment.typeCondition}"`
} });
}

@@ -251,3 +270,3 @@ else {

const fields = property.fields.map(field => {
if (field.fieldName === '__typename') {
if (field.fieldName === "__typename") {
return Object.assign({}, field, { typeName: `"${type}"`, type: { name: `"${type}"` } });

@@ -265,8 +284,8 @@ }

else {
if (property.fields && property.fields.length > 0
|| property.inlineFragments && property.inlineFragments.length > 0
|| property.fragmentSpreads && property.fragmentSpreads.length > 0) {
if ((property.fields && property.fields.length > 0) ||
(property.inlineFragments && property.inlineFragments.length > 0) ||
(property.fragmentSpreads && property.fragmentSpreads.length > 0)) {
language_1.propertyDeclaration(generator, property, () => {
const fields = property.fields.map(field => {
if (field.fieldName === '__typename') {
if (field.fieldName === "__typename") {
const objectTypeName = getObjectTypeName(property.fieldType || property.type);

@@ -292,4 +311,7 @@ return Object.assign({}, field, { typeName: objectTypeName, type: { name: objectTypeName } });

const type = graphql_1.getNamedType(property.fieldType || property.type);
if (type instanceof graphql_1.GraphQLUnionType || type instanceof graphql_1.GraphQLInterfaceType) {
return generator.context.schema.getPossibleTypes(type).map(type => type.name);
if (type instanceof graphql_1.GraphQLUnionType ||
type instanceof graphql_1.GraphQLInterfaceType) {
return generator.context.schema
.getPossibleTypes(type)
.map(type => type.name);
}

@@ -302,6 +324,5 @@ return [];

}
const leadingSpace = ' '.repeat(2 * depth);
const leadingSpace = " ".repeat(2 * depth);
generator.printOnNewline(`${leadingSpace}/**`);
description.split('\n')
.forEach(line => {
description.split("\n").forEach(line => {
generator.printOnNewline(`${leadingSpace} * ${line.trim()}`);

@@ -308,0 +329,0 @@ });

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

export { generateSource } from './codeGeneration';
export { generateSource } from "./codeGeneration";

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

import { LegacyInlineFragment } from 'apollo-codegen-core/lib/compiler/legacyIR';
import { LegacyInlineFragment } from "apollo-codegen-core/lib/compiler/legacyIR";
import CodeGenerator from "apollo-codegen-core/lib/utilities/CodeGenerator";

@@ -3,0 +3,0 @@ import { GraphQLType } from "graphql";

@@ -16,9 +16,9 @@ "use strict";

if (noBrackets) {
generator.withinBlock(closure, '', '');
generator.withinBlock(closure, "", "");
}
else {
generator.withinBlock(closure, '{', '}');
generator.withinBlock(closure, "{", "}");
}
generator.popScope();
generator.print(';');
generator.print(";");
}

@@ -32,7 +32,7 @@ exports.interfaceDeclaration = interfaceDeclaration;

if (isNullable && isInput) {
generator.print('?');
generator.print("?");
}
generator.print(': ');
generator.print(": ");
if (isArray) {
generator.print(' Array<');
generator.print(" Array<");
}

@@ -44,8 +44,8 @@ generator.pushScope({ typeName: name });

if (isArrayElementNullable) {
generator.print(' | null');
generator.print(" | null");
}
generator.print(' >');
generator.print(" >");
}
if (isNullable) {
generator.print(' | null');
generator.print(" | null");
}

@@ -56,11 +56,12 @@ }

if (isInput && isNullable) {
generator.print('?');
generator.print("?");
}
generator.print(`: ${typeName || type && types_1.typeNameFromGraphQLType(generator.context, type)}`);
generator.print(`: ${typeName ||
(type && types_1.typeNameFromGraphQLType(generator.context, type))}`);
}
generator.print(',');
generator.print(",");
}
exports.propertyDeclaration = propertyDeclaration;
function propertySetsDeclaration(generator, property, propertySets, standalone = false) {
const { description, fieldName, propertyName, isNullable, isArray, isArrayElementNullable, } = property;
const { description, fieldName, propertyName, isNullable, isArray, isArrayElementNullable } = property;
const name = fieldName || propertyName;

@@ -72,3 +73,3 @@ codeGeneration_1.printDocComment(generator, description);

if (isArray) {
generator.print(' Array<');
generator.print(" Array<");
}

@@ -82,18 +83,18 @@ generator.pushScope({ typeName: name });

if (index !== propertySets.length - 1) {
generator.print(' |');
generator.print(" |");
}
});
}, '(', ')');
}, "(", ")");
generator.popScope();
if (isArray) {
if (isArrayElementNullable) {
generator.print(' | null');
generator.print(" | null");
}
generator.print(' >');
generator.print(" >");
}
if (isNullable) {
generator.print(' | null');
generator.print(" | null");
}
if (!standalone) {
generator.print(',');
generator.print(",");
}

@@ -100,0 +101,0 @@ }

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

import { LegacyCompilerContext } from 'apollo-codegen-core/lib/compiler/legacyIR';
import { GraphQLType } from 'graphql';
import { LegacyCompilerContext } from "apollo-codegen-core/lib/compiler/legacyIR";
import { GraphQLType } from "graphql";
export declare function typeNameFromGraphQLType(context: LegacyCompilerContext, type: GraphQLType, bareTypeName?: string | null, nullable?: boolean): string;

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

const builtInScalarMap = {
[graphql_1.GraphQLString.name]: 'string',
[graphql_1.GraphQLInt.name]: 'number',
[graphql_1.GraphQLFloat.name]: 'number',
[graphql_1.GraphQLBoolean.name]: 'boolean',
[graphql_1.GraphQLID.name]: 'string',
[graphql_1.GraphQLString.name]: "string",
[graphql_1.GraphQLInt.name]: "number",
[graphql_1.GraphQLFloat.name]: "number",
[graphql_1.GraphQLBoolean.name]: "boolean",
[graphql_1.GraphQLID.name]: "string"
};

@@ -21,3 +21,7 @@ function typeNameFromGraphQLType(context, type, bareTypeName, nullable = true) {

else if (type instanceof graphql_1.GraphQLScalarType) {
typeName = builtInScalarMap[type.name] || (context.options.passthroughCustomScalars ? context.options.customScalarsPrefix + type.name : builtInScalarMap[graphql_1.GraphQLString.name]);
typeName =
builtInScalarMap[type.name] ||
(context.options.passthroughCustomScalars
? context.options.customScalarsPrefix + type.name
: builtInScalarMap[graphql_1.GraphQLString.name]);
}

@@ -27,5 +31,5 @@ else {

}
return nullable ? typeName + ' | null' : typeName;
return nullable ? typeName + " | null" : typeName;
}
exports.typeNameFromGraphQLType = typeNameFromGraphQLType;
//# sourceMappingURL=types.js.map
{
"name": "apollo-codegen-typescript-legacy",
"description": "Legacy TypeScript generator module for Apollo Codegen",
"version": "0.27.0",
"main": "./lib/index.js",
"version": "0.27.3",
"author": "Apollo GraphQL <opensource@apollographql.com>",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/apollographql/apollo-cli.git"
},
"homepage": "https://github.com/apollographql/apollo-cli",
"bugs": "https://github.com/apollographql/apollo-cli/issues",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"clean": "rm -rf lib",
"prebuild": "npm run clean",
"build": "tsc -p .",
"watch": "tsc -w -p .",
"test": "./node_modules/.bin/jest",
"prepack": "npm run build"
"build": "tsc",
"prepare": "npm run build"
},
"repository": {
"type": "git",
"url": "apollographql/apollo-cli"
},
"author": "Martijn Walraven <martijn@martijnwalraven.com>",
"license": "MIT",
"engines": {
"node": ">=6.0",
"npm": ">=5.0"
"node": ">=8",
"npm": ">=6"
},
"devDependencies": {
"@types/graphql": "^0.13.1",
"@types/inflected": "^1.1.29",
"graphql": "^0.13.1",
"jest": "^22.0.3",
"jest-matcher-utils": "^22.0.3",
"lerna": "^2.11.0",
"ts-jest": "^22.0.0",
"typescript": "^2.6.2"
},
"dependencies": {
"apollo-codegen-core": "^0.27.0"
"apollo-codegen-core": "0.27.3"
},

@@ -64,3 +55,4 @@ "jest": {

}
}
},
"gitHead": "e8d31998beae239ea92e140c670f45412ca0ffd4"
}

@@ -1,14 +0,18 @@

import { parse } from 'graphql';
import { parse } from "graphql";
import { generateSource } from '../codeGeneration';
import { generateSource } from "../codeGeneration";
import { loadSchema } from 'apollo-codegen-core/lib/loading';
const starWarsSchema = loadSchema(require.resolve('../../../common-test/fixtures/starwars/schema.json'));
const miscSchema = loadSchema(require.resolve('../../../common-test/fixtures/misc/schema.json'));
import { loadSchema } from "apollo-codegen-core/lib/loading";
const starWarsSchema = loadSchema(
require.resolve("../../../common-test/fixtures/starwars/schema.json")
);
const miscSchema = loadSchema(
require.resolve("../../../common-test/fixtures/misc/schema.json")
);
import CodeGenerator from 'apollo-codegen-core/lib/utilities/CodeGenerator';
import CodeGenerator from "apollo-codegen-core/lib/utilities/CodeGenerator";
import { compileToLegacyIR } from 'apollo-codegen-core/lib/compiler/legacyIR';
import { compileToLegacyIR } from "apollo-codegen-core/lib/compiler/legacyIR";
describe('TypeScript code generation', function() {
describe("TypeScript code generation", function() {
let generator;

@@ -24,7 +28,7 @@ let compileFromSource;

typesUsed: {}
}
};
generator = new CodeGenerator(context);
compileFromSource = (source) => {
compileFromSource = source => {
const document = parse(source);

@@ -39,3 +43,3 @@ const context = compileToLegacyIR(schema, document, {

addFragment = (fragment) => {
addFragment = fragment => {
generator.context.fragments[fragment.fragmentName] = fragment;

@@ -47,3 +51,3 @@ };

describe('#generateSource()', function() {
describe("#generateSource()", function() {
test(`should generate simple query operations`, function() {

@@ -189,3 +193,3 @@ const { compileFromSource } = setup(starWarsSchema);

test('should handle single line comments', () => {
test("should handle single line comments", () => {
const { compileFromSource } = setup(miscSchema);

@@ -204,3 +208,3 @@ const context = compileFromSource(`

test('should handle multi-line comments', () => {
test("should handle multi-line comments", () => {
const { compileFromSource } = setup(miscSchema);

@@ -219,3 +223,3 @@ const context = compileFromSource(`

test('should handle comments in enums', () => {
test("should handle comments in enums", () => {
const { compileFromSource } = setup(miscSchema);

@@ -234,3 +238,3 @@ const context = compileFromSource(`

test('should handle interfaces at root', () => {
test("should handle interfaces at root", () => {
const { compileFromSource } = setup(miscSchema);

@@ -255,3 +259,3 @@ const context = compileFromSource(`

test('should handle unions at root', () => {
test("should handle unions at root", () => {
const { compileFromSource } = setup(miscSchema);

@@ -275,3 +279,3 @@ const context = compileFromSource(`

test('should have __typename value matching fragment type on generic type', () => {
test("should have __typename value matching fragment type on generic type", () => {
const { compileFromSource } = setup(starWarsSchema);

@@ -295,3 +299,3 @@ const context = compileFromSource(`

test('should have __typename value matching fragment type on specific type', () => {
test("should have __typename value matching fragment type on specific type", () => {
const { compileFromSource } = setup(starWarsSchema);

@@ -315,3 +319,3 @@ const context = compileFromSource(`

test('should have __typename value in nested property', () => {
test("should have __typename value in nested property", () => {
const { compileFromSource } = setup(starWarsSchema);

@@ -318,0 +322,0 @@ const context = compileFromSource(`

@@ -7,3 +7,3 @@ import {

LegacyOperation
} from 'apollo-codegen-core/lib/compiler/legacyIR';
} from "apollo-codegen-core/lib/compiler/legacyIR";

@@ -23,9 +23,7 @@ import {

GraphQLObjectType
} from 'graphql'
} from "graphql";
import {
sortEnumValues
} from 'apollo-codegen-core/lib/utilities/graphql';
import { sortEnumValues } from "apollo-codegen-core/lib/utilities/graphql";
import CodeGenerator from 'apollo-codegen-core/lib/utilities/CodeGenerator';
import CodeGenerator from "apollo-codegen-core/lib/utilities/CodeGenerator";

@@ -37,7 +35,5 @@ import {

Property
} from './language';
} from "./language";
import {
typeNameFromGraphQLType,
} from './types';
import { typeNameFromGraphQLType } from "./types";

@@ -47,4 +43,6 @@ export function generateSource(context: LegacyCompilerContext) {

generator.printOnNewline('/* tslint:disable */');
generator.printOnNewline('// This file was automatically generated and should not be edited.');
generator.printOnNewline("/* tslint:disable */");
generator.printOnNewline(
"// This file was automatically generated and should not be edited."
);

@@ -67,3 +65,6 @@ context.typesUsed.forEach(type =>

export function typeDeclarationForGraphQLType(generator: CodeGenerator, type: GraphQLType) {
export function typeDeclarationForGraphQLType(
generator: CodeGenerator,
type: GraphQLType
) {
if (type instanceof GraphQLEnumType) {

@@ -76,3 +77,6 @@ enumerationDeclaration(generator, type);

function enumerationDeclaration(generator: CodeGenerator, type: GraphQLEnumType) {
function enumerationDeclaration(
generator: CodeGenerator,
type: GraphQLEnumType
) {
const { name, description } = type;

@@ -84,5 +88,5 @@ const values = type.getValues();

generator.printOnNewline(`export enum ${name} {`);
sortEnumValues(values).forEach((value) => {
sortEnumValues(values).forEach(value => {
printDocComment(generator, value.description || undefined, 1);
generator.printOnNewline(` ${value.value} = "${value.value}",`)
generator.printOnNewline(` ${value.value} = "${value.value}",`);
});

@@ -98,19 +102,32 @@ generator.printOnNewline(`}`);

const interfaceName = type.name;
interfaceDeclaration(generator, {
interfaceName,
}, () => {
const properties = propertiesFromFields(generator.context, Object.values(type.getFields()));
propertyDeclarations(generator, properties, true);
});
interfaceDeclaration(
generator,
{
interfaceName
},
() => {
const properties = propertiesFromFields(
generator.context,
Object.values(type.getFields())
);
propertyDeclarations(generator, properties, true);
}
);
}
function interfaceNameFromOperation({ operationName, operationType }: { operationName: string, operationType: string }) {
function interfaceNameFromOperation({
operationName,
operationType
}: {
operationName: string;
operationType: string;
}) {
switch (operationType) {
case 'query':
case "query":
return `${operationName}Query`;
break;
case 'mutation':
case "mutation":
return `${operationName}Mutation`;
break;
case 'subscription':
case "subscription":
return `${operationName}Subscription`;

@@ -125,7 +142,3 @@ break;

generator: CodeGenerator,
{
operationName,
operationType,
variables
}: LegacyOperation
{ operationName, operationType, variables }: LegacyOperation
) {

@@ -135,10 +148,17 @@ if (!variables || variables.length < 1) {

}
const interfaceName = `${interfaceNameFromOperation({ operationName, operationType })}Variables`;
const interfaceName = `${interfaceNameFromOperation({
operationName,
operationType
})}Variables`;
interfaceDeclaration(generator, {
interfaceName,
}, () => {
const properties = propertiesFromFields(generator.context, variables);
propertyDeclarations(generator, properties, true);
});
interfaceDeclaration(
generator,
{
interfaceName
},
() => {
const properties = propertiesFromFields(generator.context, variables);
propertyDeclarations(generator, properties, true);
}
);
}

@@ -157,3 +177,6 @@

if (type instanceof GraphQLUnionType) {
return type.getTypes().map(type => getObjectTypeName(type)).join(" | ");
return type
.getTypes()
.map(type => getObjectTypeName(type))
.join(" | ");
}

@@ -164,21 +187,23 @@ return `"${type.name}"`;

function updateTypeNameField(rootField: LegacyField): LegacyField {
const fields = rootField.fields && rootField.fields.map(field => {
if (field.fieldName === '__typename') {
const objectTypeName = getObjectTypeName(rootField.type);
return {
...field,
typeName: objectTypeName,
type: { name: objectTypeName },
};
}
const fields =
rootField.fields &&
rootField.fields.map(field => {
if (field.fieldName === "__typename") {
const objectTypeName = getObjectTypeName(rootField.type);
return {
...field,
typeName: objectTypeName,
type: { name: objectTypeName }
};
}
if (field.fields) {
return updateTypeNameField(field);
}
if (field.fields) {
return updateTypeNameField(field);
}
return field;
});
return field;
});
return {
...rootField,
fields,
fields
} as LegacyField;

@@ -189,16 +214,19 @@ }

generator: CodeGenerator,
{
{ operationName, operationType, fields }: LegacyOperation
) {
const interfaceName = interfaceNameFromOperation({
operationName,
operationType,
fields
}: LegacyOperation
) {
const interfaceName = interfaceNameFromOperation({ operationName, operationType });
operationType
});
fields = fields.map(field => updateTypeNameField(field));
const properties = propertiesFromFields(generator.context, fields);
interfaceDeclaration(generator, {
interfaceName,
}, () => {
propertyDeclarations(generator, properties);
});
interfaceDeclaration(
generator,
{
interfaceName
},
() => {
propertyDeclarations(generator, properties);
}
);
}

@@ -210,18 +238,15 @@

) {
const {
fragmentName,
typeCondition,
fields,
inlineFragments
} = fragment;
const { fragmentName, typeCondition, fields, inlineFragments } = fragment;
const interfaceName = `${fragmentName}Fragment`;
interfaceDeclaration(generator, {
interfaceName,
noBrackets: isAbstractType(typeCondition)
}, () => {
if (isAbstractType(typeCondition)) {
const propertySets = fragment.possibleTypes
.map(type => {
interfaceDeclaration(
generator,
{
interfaceName,
noBrackets: isAbstractType(typeCondition)
},
() => {
if (isAbstractType(typeCondition)) {
const propertySets = fragment.possibleTypes.map(type => {
// NOTE: inlineFragment currently consists of the merged fields

@@ -231,3 +256,3 @@ // from both inline fragments and fragment spreads.

const inlineFragment = inlineFragments.find(inlineFragment => {
return inlineFragment.typeCondition.toString() == type.toString()
return inlineFragment.typeCondition.toString() == type.toString();
});

@@ -237,8 +262,10 @@

const fields = inlineFragment.fields.map(field => {
if (field.fieldName === '__typename') {
if (field.fieldName === "__typename") {
return {
...field,
typeName: `"${inlineFragment.typeCondition}"`,
type: { name: `"${inlineFragment.typeCondition}"` } as GraphQLType
}
type: {
name: `"${inlineFragment.typeCondition}"`
} as GraphQLType
};
} else {

@@ -252,3 +279,3 @@ return field;

const fragmentFields = fields.map(field => {
if (field.fieldName === '__typename') {
if (field.fieldName === "__typename") {
return {

@@ -258,3 +285,3 @@ ...field,

type: { name: `"${type}"` } as GraphQLType
}
};
} else {

@@ -269,47 +296,66 @@ return field;

propertySetsDeclaration(generator, fragment, propertySets, true);
} else {
const fragmentFields = fields.map(field => {
if (field.fieldName === '__typename') {
return {
...field,
typeName: `"${fragment.typeCondition}"`,
type: { name: `"${fragment.typeCondition}"` } as GraphQLType
propertySetsDeclaration(generator, fragment, propertySets, true);
} else {
const fragmentFields = fields.map(field => {
if (field.fieldName === "__typename") {
return {
...field,
typeName: `"${fragment.typeCondition}"`,
type: { name: `"${fragment.typeCondition}"` } as GraphQLType
};
} else {
return field;
}
} else {
return field;
}
});
});
const properties = propertiesFromFields(generator.context, fragmentFields)
propertyDeclarations(generator, properties);
const properties = propertiesFromFields(
generator.context,
fragmentFields
);
propertyDeclarations(generator, properties);
}
}
});
);
}
export function propertiesFromFields(context: LegacyCompilerContext, fields: {
name?: string,
type: GraphQLType,
responseName?: string,
description?: string | undefined | null,
fragmentSpreads?: any,
inlineFragments?: LegacyInlineFragment[],
fieldName?: string
}[]) {
return fields.map(field => propertyFromField(
context, { ...field, description: field.description || undefined }
));
export function propertiesFromFields(
context: LegacyCompilerContext,
fields: {
name?: string;
type: GraphQLType;
responseName?: string;
description?: string | undefined | null;
fragmentSpreads?: any;
inlineFragments?: LegacyInlineFragment[];
fieldName?: string;
}[]
) {
return fields.map(field =>
propertyFromField(context, {
...field,
description: field.description || undefined
})
);
}
export function propertyFromField(context: LegacyCompilerContext, field: {
name?: string,
type: GraphQLType,
fields?: any[],
responseName?: string,
description?: string,
fragmentSpreads?: any,
inlineFragments?: LegacyInlineFragment[],
fieldName?: string
}): Property {
let { name: fieldName, type: fieldType, description, fragmentSpreads, inlineFragments } = field;
export function propertyFromField(
context: LegacyCompilerContext,
field: {
name?: string;
type: GraphQLType;
fields?: any[];
responseName?: string;
description?: string;
fragmentSpreads?: any;
inlineFragments?: LegacyInlineFragment[];
fieldName?: string;
}
): Property {
let {
name: fieldName,
type: fieldType,
description,
fragmentSpreads,
inlineFragments
} = field;
fieldName = fieldName || field.responseName;

@@ -335,5 +381,10 @@

isArrayElementNullable = !(fieldType.ofType instanceof GraphQLNonNull);
} else if (fieldType instanceof GraphQLNonNull && fieldType.ofType instanceof GraphQLList) {
isArray = true
isArrayElementNullable = !(fieldType.ofType.ofType instanceof GraphQLNonNull);
} else if (
fieldType instanceof GraphQLNonNull &&
fieldType.ofType instanceof GraphQLList
) {
isArray = true;
isArrayElementNullable = !(
fieldType.ofType.ofType instanceof GraphQLNonNull
);
}

@@ -346,12 +397,33 @@

isComposite: true,
fragmentSpreads, inlineFragments, fieldType,
isArray, isNullable, isArrayElementNullable,
fragmentSpreads,
inlineFragments,
fieldType,
isArray,
isNullable,
isArrayElementNullable
};
} else {
if (field.fieldName === '__typename') {
if (field.fieldName === "__typename") {
const typeName = typeNameFromGraphQLType(context, fieldType, null, false);
return { ...property, typeName, isComposite: false, fieldType, isNullable: false };
return {
...property,
typeName,
isComposite: false,
fieldType,
isNullable: false
};
} else {
const typeName = typeNameFromGraphQLType(context, fieldType, null, isNullable);
return { ...property, typeName, isComposite: false, fieldType, isNullable };
const typeName = typeNameFromGraphQLType(
context,
fieldType,
null,
isNullable
);
return {
...property,
typeName,
isComposite: false,
fieldType,
isNullable
};
}

@@ -361,21 +433,28 @@ }

export function propertyDeclarations(generator: CodeGenerator, properties: Property[], isInput = false) {
export function propertyDeclarations(
generator: CodeGenerator,
properties: Property[],
isInput = false
) {
if (!properties) return;
properties.forEach(property => {
if (isAbstractType(getNamedType(property.type || property.fieldType!))) {
const propertySets = getPossibleTypeNames(generator, property)
.map(type => {
const inlineFragment = property.inlineFragments && property.inlineFragments.find(inlineFragment => {
return inlineFragment.typeCondition.toString() == type
});
const propertySets = getPossibleTypeNames(generator, property).map(
type => {
const inlineFragment =
property.inlineFragments &&
property.inlineFragments.find(inlineFragment => {
return inlineFragment.typeCondition.toString() == type;
});
if (inlineFragment) {
const fields = inlineFragment.fields.map(field => {
if (field.fieldName === '__typename') {
if (field.fieldName === "__typename") {
return {
...field,
typeName: `"${inlineFragment.typeCondition}"`,
type: { name: `"${inlineFragment.typeCondition}"` } as GraphQLType
}
type: {
name: `"${inlineFragment.typeCondition}"`
} as GraphQLType
};
} else {

@@ -389,3 +468,3 @@ return field;

const fields = property.fields!.map(field => {
if (field.fieldName === '__typename') {
if (field.fieldName === "__typename") {
return {

@@ -395,3 +474,3 @@ ...field,

type: { name: `"${type}"` } as GraphQLType
}
};
} else {

@@ -404,14 +483,18 @@ return field;

}
});
}
);
propertySetsDeclaration(generator, property, propertySets);
} else {
if (property.fields && property.fields.length > 0
|| property.inlineFragments && property.inlineFragments.length > 0
|| property.fragmentSpreads && property.fragmentSpreads.length > 0
if (
(property.fields && property.fields.length > 0) ||
(property.inlineFragments && property.inlineFragments.length > 0) ||
(property.fragmentSpreads && property.fragmentSpreads.length > 0)
) {
propertyDeclaration(generator, property, () => {
const fields = property.fields!.map(field => {
if (field.fieldName === '__typename') {
const objectTypeName = getObjectTypeName(property.fieldType || property.type!);
if (field.fieldName === "__typename") {
const objectTypeName = getObjectTypeName(
property.fieldType || property.type!
);
return {

@@ -421,3 +504,3 @@ ...field,

type: { name: objectTypeName }
}
};
} else {

@@ -442,7 +525,15 @@ return field;

*/
function getPossibleTypeNames(generator: CodeGenerator<LegacyCompilerContext>, property: Property) {
function getPossibleTypeNames(
generator: CodeGenerator<LegacyCompilerContext>,
property: Property
) {
const type = getNamedType(property.fieldType || property.type!);
if (type instanceof GraphQLUnionType || type instanceof GraphQLInterfaceType) {
return generator.context.schema.getPossibleTypes(type).map(type => type.name);
if (
type instanceof GraphQLUnionType ||
type instanceof GraphQLInterfaceType
) {
return generator.context.schema
.getPossibleTypes(type)
.map(type => type.name);
}

@@ -453,3 +544,7 @@

export function printDocComment(generator: CodeGenerator, description?: string, depth: number = 0): void {
export function printDocComment(
generator: CodeGenerator,
description?: string,
depth: number = 0
): void {
if (!description) {

@@ -459,10 +554,9 @@ return;

const leadingSpace = ' '.repeat(2 * depth);
const leadingSpace = " ".repeat(2 * depth);
generator.printOnNewline(`${leadingSpace}/**`);
description.split('\n')
.forEach(line => {
generator.printOnNewline(`${leadingSpace} * ${line.trim()}`);
});
description.split("\n").forEach(line => {
generator.printOnNewline(`${leadingSpace} * ${line.trim()}`);
});
generator.printOnNewline(`${leadingSpace} */`);
}

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

export { generateSource } from './codeGeneration';
export { generateSource } from "./codeGeneration";

@@ -1,5 +0,5 @@

import { LegacyInlineFragment } from 'apollo-codegen-core/lib/compiler/legacyIR';
import { LegacyInlineFragment } from "apollo-codegen-core/lib/compiler/legacyIR";
import { propertyDeclarations, printDocComment } from './codeGeneration';
import { typeNameFromGraphQLType } from './types';
import { propertyDeclarations, printDocComment } from "./codeGeneration";
import { typeNameFromGraphQLType } from "./types";

@@ -10,23 +10,26 @@ import CodeGenerator from "apollo-codegen-core/lib/utilities/CodeGenerator";

export interface Property {
fieldName?: string,
fieldType?: GraphQLType,
propertyName?: string,
type?: GraphQLType,
description?: string,
typeName?: string,
isComposite?: boolean,
isNullable?: boolean,
fields?: any[],
inlineFragments?: LegacyInlineFragment[],
fragmentSpreads?: any,
isInput?: boolean,
isArray?: boolean,
isArrayElementNullable?: boolean | null,
fieldName?: string;
fieldType?: GraphQLType;
propertyName?: string;
type?: GraphQLType;
description?: string;
typeName?: string;
isComposite?: boolean;
isNullable?: boolean;
fields?: any[];
inlineFragments?: LegacyInlineFragment[];
fragmentSpreads?: any;
isInput?: boolean;
isArray?: boolean;
isArrayElementNullable?: boolean | null;
}
export function interfaceDeclaration(generator: CodeGenerator, {
interfaceName,
noBrackets
}: { interfaceName: string, noBrackets?: boolean },
closure: () => void) {
export function interfaceDeclaration(
generator: CodeGenerator,
{
interfaceName,
noBrackets
}: { interfaceName: string; noBrackets?: boolean },
closure: () => void
) {
generator.printNewlineIfNeeded();

@@ -42,21 +45,25 @@ generator.printNewline();

if (noBrackets) {
generator.withinBlock(closure, '', '');
generator.withinBlock(closure, "", "");
} else {
generator.withinBlock(closure, '{', '}');
generator.withinBlock(closure, "{", "}");
}
generator.popScope();
generator.print(';');
generator.print(";");
}
export function propertyDeclaration(generator: CodeGenerator, {
fieldName,
type,
propertyName,
typeName,
description,
isInput,
isArray,
isNullable,
isArrayElementNullable
}: Property, closure?: () => void) {
export function propertyDeclaration(
generator: CodeGenerator,
{
fieldName,
type,
propertyName,
typeName,
description,
isInput,
isArray,
isNullable,
isArrayElementNullable
}: Property,
closure?: () => void
) {
const name = fieldName || propertyName;

@@ -70,8 +77,8 @@

if (isNullable && isInput) {
generator.print('?');
generator.print("?");
}
generator.print(': ');
generator.print(": ");
if (isArray) {
generator.print(' Array<');
generator.print(" Array<");
}

@@ -86,25 +93,36 @@ generator.pushScope({ typeName: name });

if (isArrayElementNullable) {
generator.print(' | null');
generator.print(" | null");
}
generator.print(' >');
generator.print(" >");
}
if (isNullable) {
generator.print(' | null');
generator.print(" | null");
}
} else {
generator.printOnNewline(name);
if (isInput && isNullable) {
generator.print('?')
generator.print("?");
}
generator.print(`: ${typeName || type && typeNameFromGraphQLType(generator.context, type)}`);
generator.print(
`: ${typeName ||
(type && typeNameFromGraphQLType(generator.context, type))}`
);
}
generator.print(',');
generator.print(",");
}
export function propertySetsDeclaration(generator: CodeGenerator, property: Property, propertySets: Property[][], standalone = false) {
export function propertySetsDeclaration(
generator: CodeGenerator,
property: Property,
propertySets: Property[][],
standalone = false
) {
const {
description, fieldName, propertyName,
isNullable, isArray, isArrayElementNullable,
description,
fieldName,
propertyName,
isNullable,
isArray,
isArrayElementNullable
} = property;

@@ -120,3 +138,3 @@ const name = fieldName || propertyName;

if (isArray) {
generator.print(' Array<');
generator.print(" Array<");
}

@@ -126,12 +144,16 @@

generator.withinBlock(() => {
propertySets.forEach((propertySet, index, propertySets) => {
generator.withinBlock(() => {
propertyDeclarations(generator, propertySet);
generator.withinBlock(
() => {
propertySets.forEach((propertySet, index, propertySets) => {
generator.withinBlock(() => {
propertyDeclarations(generator, propertySet);
});
if (index !== propertySets.length - 1) {
generator.print(" |");
}
});
if (index !== propertySets.length - 1) {
generator.print(' |');
}
})
}, '(', ')');
},
"(",
")"
);

@@ -142,14 +164,14 @@ generator.popScope();

if (isArrayElementNullable) {
generator.print(' | null');
generator.print(" | null");
}
generator.print(' >');
generator.print(" >");
}
if (isNullable) {
generator.print(' | null');
generator.print(" | null");
}
if (!standalone) {
generator.print(',');
generator.print(",");
}
}

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

import { LegacyCompilerContext } from 'apollo-codegen-core/lib/compiler/legacyIR';
import { LegacyCompilerContext } from "apollo-codegen-core/lib/compiler/legacyIR";

@@ -13,15 +13,20 @@ import {

GraphQLType
} from 'graphql';
} from "graphql";
const builtInScalarMap = {
[GraphQLString.name]: 'string',
[GraphQLInt.name]: 'number',
[GraphQLFloat.name]: 'number',
[GraphQLBoolean.name]: 'boolean',
[GraphQLID.name]: 'string',
}
[GraphQLString.name]: "string",
[GraphQLInt.name]: "number",
[GraphQLFloat.name]: "number",
[GraphQLBoolean.name]: "boolean",
[GraphQLID.name]: "string"
};
export function typeNameFromGraphQLType(context: LegacyCompilerContext, type: GraphQLType, bareTypeName?: string | null, nullable = true): string {
export function typeNameFromGraphQLType(
context: LegacyCompilerContext,
type: GraphQLType,
bareTypeName?: string | null,
nullable = true
): string {
if (type instanceof GraphQLNonNull) {
return typeNameFromGraphQLType(context, type.ofType, bareTypeName, false)
return typeNameFromGraphQLType(context, type.ofType, bareTypeName, false);
}

@@ -31,5 +36,14 @@

if (type instanceof GraphQLList) {
typeName = `Array< ${typeNameFromGraphQLType(context, type.ofType, bareTypeName, true)} >`;
typeName = `Array< ${typeNameFromGraphQLType(
context,
type.ofType,
bareTypeName,
true
)} >`;
} else if (type instanceof GraphQLScalarType) {
typeName = builtInScalarMap[type.name] || (context.options.passthroughCustomScalars ? context.options.customScalarsPrefix + type.name : builtInScalarMap[GraphQLString.name]);
typeName =
builtInScalarMap[type.name] ||
(context.options.passthroughCustomScalars
? context.options.customScalarsPrefix + type.name
: builtInScalarMap[GraphQLString.name]);
} else {

@@ -39,3 +53,3 @@ typeName = bareTypeName || type.name;

return nullable ? typeName + ' | null' : typeName;
return nullable ? typeName + " | null" : typeName;
}

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc