Socket
Socket
Sign inDemoInstall

apollo-codegen

Package Overview
Dependencies
Maintainers
1
Versions
102
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

apollo-codegen - npm Package Compare versions

Comparing version 0.16.2 to 0.16.3

253

lib/compilation.js

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

const graphql_2 = require("./utilities/graphql");
const printing_1 = require("./utilities/printing");
const lodash_1 = require("lodash");
const sjcl = require("sjcl");
const crypto_1 = require("crypto");
function compileToIR(schema, document, options = { mergeInFieldsFromFragmentSpreads: true }) {

@@ -15,14 +13,11 @@ if (options.addTypename) {

const operations = Object.create(null);
compiler.operations.forEach(operation => {
operations[operation.name.value] = compiler.compileOperation(operation);
compiler.compiledOperations.forEach(operation => {
operations[operation.operationName] = operation;
});
const fragments = Object.create(null);
compiler.fragments.forEach(fragment => {
fragments[fragment.name.value] = compiler.compileFragment(fragment);
});
Object.values(operations).forEach(operation => {
augmentCompiledOperationWithFragments(operation, fragments);
});
for (const [fragmentName, compiledFragment] of compiler.compiledFragmentMap.entries()) {
fragments[fragmentName] = compiledFragment;
}
const typesUsed = compiler.typesUsed;
return { schema, operations, fragments, typesUsed };
return { schema, operations, fragments, typesUsed, options };
}

@@ -35,15 +30,19 @@ exports.compileToIR = compileToIR;

this.typesUsedSet = new Set();
this.fragmentMap = Object.create(null);
this.operations = [];
this.fragmentMap = new Map();
const operations = [];
for (const definition of document.definitions) {
switch (definition.kind) {
case graphql_1.Kind.OPERATION_DEFINITION:
this.operations.push(definition);
operations.push(definition);
break;
case graphql_1.Kind.FRAGMENT_DEFINITION:
this.fragmentMap[definition.name.value] = definition;
this.fragmentMap.set(definition.name.value, definition);
break;
}
}
this.compiledFragmentMap = Object.create(null);
this.compiledFragmentMap = new Map();
this.compiledOperations = operations.map(this.compileOperation, this);
for (const fragmentName of this.fragmentMap.keys()) {
this.compiledFragmentNamed(fragmentName);
}
}

@@ -67,13 +66,10 @@ addTypeUsed(type) {

}
fragmentNamed(fragmentName) {
return this.fragmentMap[fragmentName];
}
get fragments() {
return Object.values(this.fragmentMap);
}
compileOperation(operationDefinition) {
if (!operationDefinition.name) {
throw new Error('Operations should be named');
}
const filePath = graphql_2.filePathForNode(operationDefinition);
const operationName = operationDefinition.name.value;
const operationType = operationDefinition.operation;
const variables = operationDefinition.variableDefinitions.map(node => {
const variables = (operationDefinition.variableDefinitions || []).map(node => {
const name = node.variable.name.value;

@@ -88,21 +84,63 @@ const type = graphql_1.typeFromAST(this.schema, node.type);

const groupedFieldSet = this.collectFields(rootType, operationDefinition.selectionSet, undefined, groupedVisitedFragmentSet);
const fragmentsReferencedSet = Object.create(null);
const fragmentsReferencedSet = new Set();
const { fields } = this.resolveFields(rootType, groupedFieldSet, groupedVisitedFragmentSet, fragmentsReferencedSet);
const fragmentsReferenced = Object.keys(fragmentsReferencedSet);
return { filePath, operationName, operationType, rootType, variables, source, fields, fragmentsReferenced };
const fragmentsReferenced = Array.from(fragmentsReferencedSet.keys());
const sourceWithFragments = [
source,
...fragmentsReferenced.map(fragmentName => {
return this.compiledFragmentNamed(fragmentName).source;
})
].join('\n');
const hash = crypto_1.createHash('sha256');
hash.update(sourceWithFragments);
const operationId = hash.digest('hex');
return {
filePath,
operationName,
operationType,
rootType,
variables,
source,
fields,
fragmentsReferenced,
sourceWithFragments,
operationId
};
}
compileFragment(fragmentDefinition) {
const filePath = graphql_2.filePathForNode(fragmentDefinition);
const fragmentName = fragmentDefinition.name.value;
const source = graphql_1.print(fragmentDefinition);
const typeCondition = graphql_1.typeFromAST(this.schema, fragmentDefinition.typeCondition);
fragmentNamed(fragmentName) {
const fragment = this.fragmentMap.get(fragmentName);
if (!fragment) {
throw new graphql_1.GraphQLError(`Cannot find fragment "${fragmentName}"`);
}
return fragment;
}
compiledFragmentNamed(fragmentName) {
let compiledFragment = this.compiledFragmentMap.get(fragmentName);
if (compiledFragment)
return compiledFragment;
const fragment = this.fragmentNamed(fragmentName);
const filePath = graphql_2.filePathForNode(fragment);
const source = graphql_1.print(fragment);
const typeCondition = graphql_1.typeFromAST(this.schema, fragment.typeCondition);
const possibleTypes = this.possibleTypesForType(typeCondition);
const groupedVisitedFragmentSet = new Map();
const groupedFieldSet = this.collectFields(typeCondition, fragmentDefinition.selectionSet, undefined, groupedVisitedFragmentSet);
const fragmentsReferencedSet = Object.create(null);
const groupedFieldSet = this.collectFields(typeCondition, fragment.selectionSet, undefined, groupedVisitedFragmentSet);
const fragmentsReferencedSet = new Set();
const { fields, fragmentSpreads, inlineFragments } = this.resolveFields(typeCondition, groupedFieldSet, groupedVisitedFragmentSet, fragmentsReferencedSet);
const fragmentsReferenced = Object.keys(fragmentsReferencedSet);
return { filePath, fragmentName, source, typeCondition, possibleTypes, fields, fragmentSpreads, inlineFragments, fragmentsReferenced };
const fragmentsReferenced = Array.from(fragmentsReferencedSet.keys());
compiledFragment = {
filePath,
fragmentName,
source,
typeCondition,
possibleTypes,
fields,
fragmentSpreads,
inlineFragments,
fragmentsReferenced
};
this.compiledFragmentMap.set(fragmentName, compiledFragment);
return compiledFragment;
}
collectFields(parentType, selectionSet, groupedFieldSet = Object.create(null), groupedVisitedFragmentSet = new Map()) {
collectFields(parentType, selectionSet, groupedFieldSet = new Map(), groupedVisitedFragmentSet = new Map()) {
if (!graphql_1.isCompositeType(parentType)) {

@@ -118,17 +156,22 @@ throw new Error(`parentType should be a composite type, but is "${String(parentType)}"`);

if (!field) {
throw new graphql_1.GraphQLError(`Cannot query field "${fieldName}" on type "${String(parentType)}"`, [selection]);
throw new graphql_1.GraphQLError(`Cannot query field "${fieldName}" on type "${String(parentType)}"`, [
selection
]);
}
if (groupedFieldSet) {
if (!groupedFieldSet[responseName]) {
groupedFieldSet[responseName] = [];
let fieldSet = groupedFieldSet.get(responseName);
if (!fieldSet) {
fieldSet = [];
groupedFieldSet.set(responseName, fieldSet);
}
fieldSet.push([
parentType,
{
responseName,
fieldName,
args: selection.arguments ? argumentsFromAST(selection.arguments) : undefined,
type: field.type,
directives: selection.directives,
selectionSet: selection.selectionSet
}
groupedFieldSet[responseName].push([parentType, {
responseName,
fieldName,
args: argumentsFromAST(selection.arguments),
type: field.type,
directives: selection.directives,
selectionSet: selection.selectionSet
}]);
}
]);
break;

@@ -138,5 +181,5 @@ }

const typeCondition = selection.typeCondition;
const inlineFragmentType = typeCondition ?
graphql_1.typeFromAST(this.schema, typeCondition) :
parentType;
const inlineFragmentType = typeCondition
? graphql_1.typeFromAST(this.schema, typeCondition)
: parentType;
if (!graphql_1.doTypesOverlap(this.schema, inlineFragmentType, parentType))

@@ -151,20 +194,16 @@ continue;

const fragment = this.fragmentNamed(fragmentName);
if (!fragment)
throw new graphql_1.GraphQLError(`Cannot find fragment "${fragmentName}"`);
const typeCondition = fragment.typeCondition;
const fragmentType = graphql_1.typeFromAST(this.schema, typeCondition);
if (groupedVisitedFragmentSet) {
let visitedFragmentSet = groupedVisitedFragmentSet.get(parentType);
if (!visitedFragmentSet) {
visitedFragmentSet = {};
groupedVisitedFragmentSet.set(parentType, visitedFragmentSet);
}
if (visitedFragmentSet[fragmentName])
continue;
visitedFragmentSet[fragmentName] = true;
let visitedFragmentSet = groupedVisitedFragmentSet.get(parentType);
if (!visitedFragmentSet) {
visitedFragmentSet = new Set();
groupedVisitedFragmentSet.set(parentType, visitedFragmentSet);
}
if (visitedFragmentSet.has(fragmentName))
continue;
visitedFragmentSet.add(fragmentName);
if (!graphql_1.doTypesOverlap(this.schema, fragmentType, parentType))
continue;
const effectiveType = parentType instanceof graphql_1.GraphQLObjectType ? parentType : fragmentType;
this.collectFields(effectiveType, fragment.selectionSet, this.options.mergeInFieldsFromFragmentSpreads ? groupedFieldSet : null, groupedVisitedFragmentSet);
this.collectFields(effectiveType, fragment.selectionSet, this.options.mergeInFieldsFromFragmentSpreads ? groupedFieldSet : undefined, groupedVisitedFragmentSet);
break;

@@ -185,3 +224,3 @@ }

mergeSelectionSets(parentType, fieldSet, groupedVisitedFragmentSet) {
const groupedFieldSet = Object.create(null);
const groupedFieldSet = new Map();
for (const [, field] of fieldSet) {

@@ -197,4 +236,4 @@ const selectionSet = field.selectionSet;

const fields = [];
for (let [responseName, fieldSet] of Object.entries(groupedFieldSet)) {
fieldSet = fieldSet.filter(([typeCondition,]) => graphql_1.isTypeSubTypeOf(this.schema, parentType, typeCondition));
for (let [responseName, fieldSet] of groupedFieldSet.entries()) {
fieldSet = fieldSet.filter(([typeCondition]) => graphql_1.isTypeSubTypeOf(this.schema, parentType, typeCondition));
if (fieldSet.length < 1)

@@ -211,6 +250,7 @@ continue;

const isConditional = fieldSet.some(([, field]) => {
return field.directives && field.directives.some(directive => {
const directiveName = directive.name.value;
return directiveName == 'skip' || directiveName == 'include';
});
return (!!field.directives &&
field.directives.some(directive => {
const directiveName = directive.name.value;
return directiveName == 'skip' || directiveName == 'include';
}));
});

@@ -227,6 +267,4 @@ if (isConditional) {

}
Object.assign(field, {
isDeprecated: fieldDef.isDeprecated,
deprecationReason: fieldDef.deprecationReason,
});
field.isDeprecated = fieldDef.isDeprecated;
field.deprecationReason = fieldDef.deprecationReason;
}

@@ -240,3 +278,3 @@ }

const { fields, fragmentSpreads, inlineFragments } = this.resolveFields(bareType, subSelectionGroupedFieldSet, subSelectionGroupedVisitedFragmentSet, fragmentsReferencedSet);
Object.assign(field, { fields, fragmentSpreads, inlineFragments });
field = Object.assign({}, field, { fields, fragmentSpreads, inlineFragments });
}

@@ -248,10 +286,11 @@ fields.push(field);

if (fragmentsReferencedSet) {
Object.assign(fragmentsReferencedSet, ...groupedVisitedFragmentSet.values());
for (const visitedFragmentSet of groupedVisitedFragmentSet.values()) {
for (const visitedFragment of visitedFragmentSet) {
fragmentsReferencedSet.add(visitedFragment);
}
}
for (let fragmentName of fragmentSpreads) {
const fragment = this.fragmentNamed(fragmentName);
if (!fragment)
throw new graphql_1.GraphQLError(`Cannot find fragment "${fragmentName}"`);
const { fragmentsReferenced: fragmentsReferencedFromFragment } = this.compileFragment(fragment);
for (let fragmentReferenced of fragmentsReferencedFromFragment) {
fragmentsReferencedSet[fragmentReferenced] = true;
const compiledFragment = this.compiledFragmentNamed(fragmentName);
for (let fragmentReferenced of compiledFragment.fragmentsReferenced) {
fragmentsReferencedSet.add(fragmentReferenced);
}

@@ -273,5 +312,6 @@ }

const possibleTypes = new Set();
for (const fieldSet of Object.values(groupedFieldSet)) {
for (const [typeCondition,] of fieldSet) {
if (this.schema.isPossibleType(parentType, typeCondition)) {
for (const fieldSet of groupedFieldSet.values()) {
for (const [typeCondition] of fieldSet) {
if (typeCondition instanceof graphql_1.GraphQLObjectType &&
this.schema.isPossibleType(parentType, typeCondition)) {
possibleTypes.add(typeCondition);

@@ -283,3 +323,4 @@ }

for (const effectiveType of groupedVisitedFragmentSet.keys()) {
if (this.schema.isPossibleType(parentType, effectiveType)) {
if (effectiveType instanceof graphql_1.GraphQLObjectType &&
this.schema.isPossibleType(parentType, effectiveType)) {
possibleTypes.add(effectiveType);

@@ -298,3 +339,3 @@ }

continue;
for (const fragmentName of Object.keys(visitedFragmentSet)) {
for (const fragmentName of visitedFragmentSet.keys()) {
fragmentSpreads.add(fragmentName);

@@ -306,34 +347,8 @@ }

}
exports.Compiler = Compiler;
function augmentCompiledOperationWithFragments(compiledOperation, compiledFragments) {
const operationAndFragments = operationAndRelatedFragments(compiledOperation, compiledFragments);
compiledOperation.sourceWithFragments = operationAndFragments.map(operationOrFragment => {
return operationOrFragment.source;
}).join('\n');
const idBits = sjcl.hash.sha256.hash(compiledOperation.sourceWithFragments);
compiledOperation.operationId = sjcl.codec.hex.fromBits(idBits);
}
function operationAndRelatedFragments(compiledOperationOrFragment, allCompiledFragments) {
let result = lodash_1.flatMap(compiledOperationOrFragment.fragmentsReferenced, (fragmentName) => {
return operationAndRelatedFragments(allCompiledFragments[fragmentName], allCompiledFragments);
});
result.unshift(compiledOperationOrFragment);
result = lodash_1.uniqBy(result, (compiledOperationOrFragment) => {
return compiledOperationOrFragment.fragmentName;
});
result = result.sort((a, b) => {
return a.fragmentName > b.fragmentName;
});
return result;
}
function argumentsFromAST(args) {
return args && args.map(arg => {
return { name: arg.name.value, value: graphql_2.valueFromValueNode(arg.value) };
});
return (args &&
args.map(arg => {
return { name: arg.name.value, value: graphql_2.valueFromValueNode(arg.value) };
}));
}
function printIR({ fields, inlineFragments, fragmentSpreads }) {
return fields && printing_1.wrap('<', printing_1.join(fragmentSpreads, ', '), '> ')
+ printing_1.block(fields.map(field => `${field.name}: ${String(field.type)}` + printing_1.wrap(' ', printIR(field))).concat(inlineFragments && inlineFragments.map(inlineFragment => `${String(inlineFragment.typeCondition)}` + printing_1.wrap(' ', printIR(inlineFragment)))));
}
exports.printIR = printIR;
//# sourceMappingURL=compilation.js.map

@@ -194,4 +194,3 @@ "use strict";

if (graphql_1.isAbstractType(graphql_1.getNamedType(property.type || property.fieldType))) {
const possibleTypes = getPossibleTypes(generator, property);
const propertySets = Object.keys(possibleTypes)
const propertySets = getPossibleTypeNames(generator, property)
.map(type => {

@@ -242,5 +241,5 @@ const inlineFragment = property.inlineFragments.find(inlineFragment => {

exports.propertyDeclarations = propertyDeclarations;
function getPossibleTypes(generator, property) {
return generator.context.schema._possibleTypeMap[graphql_1.getNamedType(property.fieldType || property.type)];
function getPossibleTypeNames(generator, property) {
return generator.context.schema.getPossibleTypes(graphql_1.getNamedType(property.fieldType || property.type)).map(type => type.name);
}
//# sourceMappingURL=codeGeneration.js.map

@@ -19,3 +19,2 @@ "use strict";

const context = compilation_1.compileToIR(schema, document, options);
Object.assign(context, options);
let output = '';

@@ -43,3 +42,3 @@ switch (target) {

}
if (context.generateOperationIds) {
if (options.generateOperationIds) {
writeOperationIdsMap(context);

@@ -46,0 +45,0 @@ }

@@ -6,2 +6,3 @@ "use strict";

const printing_1 = require("../utilities/printing");
const CodeGenerator_1 = require("../utilities/CodeGenerator");
const language_1 = require("./language");

@@ -11,3 +12,2 @@ const naming_1 = require("./naming");

const types_1 = require("./types");
const CodeGenerator_1 = require("../utilities/CodeGenerator");
function generateSource(context, options) {

@@ -18,3 +18,3 @@ const generator = new CodeGenerator_1.default(context);

generator.printOnNewline('import Apollo');
language_1.namespaceDeclaration(generator, context.namespace, () => {
language_1.namespaceDeclaration(generator, context.options.namespace, () => {
context.typesUsed.forEach(type => {

@@ -33,3 +33,4 @@ typeDeclarationForGraphQLType(generator, type);

exports.generateSource = generateSource;
function classDeclarationForOperation(generator, { operationName, operationType, rootType, variables, fields, inlineFragments, fragmentSpreads, fragmentsReferenced, source, sourceWithFragments, operationId }) {
function classDeclarationForOperation(generator, operation) {
const { operationName, operationType, rootType, variables, fields, inlineFragments, fragmentSpreads, fragmentsReferenced, source, sourceWithFragments, operationId } = operation;
let className;

@@ -60,3 +61,3 @@ let protocol;

}
operationIdentifier(generator, { operationName, sourceWithFragments, operationId });
operationIdentifier(generator, operation);
if (fragmentsReferenced && fragmentsReferenced.length > 0) {

@@ -75,3 +76,4 @@ generator.printNewlineIfNeeded();

const typeName = types_1.typeNameFromGraphQLType(generator.context, type);
const isOptional = !(type instanceof graphql_1.GraphQLNonNull || type.ofType instanceof graphql_1.GraphQLNonNull);
const isOptional = !(type instanceof graphql_1.GraphQLNonNull ||
(type instanceof graphql_1.GraphQLList && type.ofType instanceof graphql_1.GraphQLNonNull));
return { name, propertyName, type, typeName, isOptional };

@@ -92,3 +94,3 @@ });

structDeclarationForSelectionSet(generator, {
structName: "Data",
structName: 'Data',
parentType: rootType,

@@ -121,4 +123,4 @@ fields,

exports.structDeclarationForFragment = structDeclarationForFragment;
function structDeclarationForSelectionSet(generator, { structName, adoptedProtocols = ['GraphQLSelectionSet'], parentType, fields, inlineFragments, fragmentSpreads, }, beforeClosure) {
const possibleTypes = parentType ? types_1.possibleTypesForType(generator.context, parentType) : null;
function structDeclarationForSelectionSet(generator, { structName, adoptedProtocols = ['GraphQLSelectionSet'], parentType, fields, inlineFragments, fragmentSpreads }, beforeClosure) {
const possibleTypes = types_1.possibleTypesForType(generator.context, parentType);
language_1.structDeclaration(generator, { structName, adoptedProtocols }, () => {

@@ -136,5 +138,8 @@ if (beforeClosure) {

generator.printOnNewline('public static let selections: [GraphQLSelection] = ');
selectionSetInitialization(generator, fields, inlineFragments);
selectionSetInitialization(generator, fields, inlineFragments, fragmentSpreads);
generator.printNewlineIfNeeded();
language_1.propertyDeclaration(generator, { propertyName: "snapshot", typeName: "Snapshot" });
language_1.propertyDeclaration(generator, {
propertyName: 'snapshot',
typeName: 'Snapshot'
});
generator.printNewlineIfNeeded();

@@ -150,3 +155,3 @@ generator.printOnNewline('public init(snapshot: Snapshot)');

.map(field => naming_1.propertyFromField(generator.context, field))
.filter(field => field.propertyName != "__typename");
.filter(field => field.propertyName != '__typename');
parametersForProperties(generator, properties);

@@ -156,3 +161,3 @@ generator.withinBlock(() => {

`"__typename": "${possibleTypes[0]}"`,
...properties.map(({ propertyName }) => `"${propertyName}": ${propertyName}`)
...properties.map(({ responseName, propertyName }) => `"${responseName}": ${propertyName}`)
], ', ') || ':', `])`));

@@ -165,7 +170,8 @@ });

generator.printOnNewline(`public static func make${possibleType}`);
const inlineFragment = inlineFragments && inlineFragments.find(inlineFragment => inlineFragment.typeCondition === possibleType);
const inlineFragment = inlineFragments &&
inlineFragments.find(inlineFragment => inlineFragment.typeCondition === possibleType);
const fieldsForPossibleType = inlineFragment ? inlineFragment.fields : fields;
const properties = fieldsForPossibleType
.map(field => naming_1.propertyFromField(generator.context, field, inlineFragment && naming_1.structNameForInlineFragment(inlineFragment)))
.filter(field => field.propertyName != "__typename");
.filter(field => field.propertyName != '__typename');
parametersForProperties(generator, properties);

@@ -176,3 +182,3 @@ generator.print(` -> ${structName}`);

`"__typename": "${possibleType}"`,
...properties.map(({ propertyName }) => `"${propertyName}": ${propertyName}`)
...properties.map(({ responseName, propertyName }) => `"${responseName}": ${propertyName}`)
], ', ') || ':', `])`));

@@ -183,3 +189,4 @@ });

fields.forEach(field => propertyDeclarationForField(generator, field));
inlineFragments && inlineFragments.forEach(inlineFragment => propertyDeclarationForInlineFragment(generator, inlineFragment));
inlineFragments &&
inlineFragments.forEach(inlineFragment => propertyDeclarationForInlineFragment(generator, inlineFragment));
if (fragmentSpreads && fragmentSpreads.length > 0) {

@@ -189,7 +196,7 @@ generator.printNewlineIfNeeded();

generator.withinBlock(() => {
generator.printOnNewline("get");
generator.printOnNewline('get');
generator.withinBlock(() => {
generator.printOnNewline(`return Fragments(snapshot: snapshot)`);
});
generator.printOnNewline("set");
generator.printOnNewline('set');
generator.withinBlock(() => {

@@ -201,3 +208,3 @@ generator.printOnNewline(`snapshot = newValue.snapshot`);

if (inlineFragments && inlineFragments.length > 0) {
inlineFragments.forEach((inlineFragment) => {
inlineFragments.forEach(inlineFragment => {
structDeclarationForSelectionSet(generator, {

@@ -208,3 +215,2 @@ structName: naming_1.structNameForInlineFragment(inlineFragment),

fields: inlineFragment.fields,
inlineFragments: inlineFragment.inlineFragments,
fragmentSpreads: inlineFragment.fragmentSpreads

@@ -218,3 +224,6 @@ });

}, () => {
language_1.propertyDeclaration(generator, { propertyName: "snapshot", typeName: "Snapshot" });
language_1.propertyDeclaration(generator, {
propertyName: 'snapshot',
typeName: 'Snapshot'
});
fragmentSpreads.forEach(fragmentSpread => {

@@ -226,3 +235,3 @@ const { propertyName, bareTypeName, typeName, fragment } = naming_1.propertyFromFragmentSpread(generator.context, fragmentSpread);

generator.withinBlock(() => {
generator.printOnNewline("get");
generator.printOnNewline('get');
generator.withinBlock(() => {

@@ -234,3 +243,3 @@ if (isOptional) {

});
generator.printOnNewline("set");
generator.printOnNewline('set');
generator.withinBlock(() => {

@@ -249,10 +258,13 @@ if (isOptional) {

}
fields.filter(field => graphql_1.isCompositeType(graphql_1.getNamedType(field.type))).forEach(field => {
structDeclarationForSelectionSet(generator, {
structName: naming_1.structNameForPropertyName(field.responseName),
parentType: graphql_1.getNamedType(field.type),
fields: field.fields,
inlineFragments: field.inlineFragments,
fragmentSpreads: field.fragmentSpreads
});
fields.forEach(field => {
const bareFieldType = graphql_1.getNamedType(field.type);
if (graphql_1.isCompositeType(bareFieldType) && field.fields) {
structDeclarationForSelectionSet(generator, {
structName: naming_1.structNameForPropertyName(field.responseName),
parentType: bareFieldType,
fields: field.fields,
inlineFragments: field.inlineFragments,
fragmentSpreads: field.fragmentSpreads
});
}
});

@@ -282,3 +294,3 @@ });

if (isList) {
generator.printOnNewline("get");
generator.printOnNewline('get');
generator.withinBlock(() => {

@@ -290,5 +302,5 @@ const snapshotTypeName = types_1.typeNameFromGraphQLType(generator.context, type, 'Snapshot', isOptional);

});
generator.printOnNewline("set");
generator.printOnNewline('set');
generator.withinBlock(() => {
let newValueExpression = "newValue" + mapExpressionForType(generator.context, type, `$0.snapshot`);
let newValueExpression = 'newValue' + mapExpressionForType(generator.context, type, `$0.snapshot`);
generator.printOnNewline(`snapshot.updateValue(${newValueExpression}, forKey: "${responseName}")`);

@@ -298,3 +310,3 @@ });

else {
generator.printOnNewline("get");
generator.printOnNewline('get');
generator.withinBlock(() => {

@@ -308,3 +320,3 @@ if (isOptional) {

});
generator.printOnNewline("set");
generator.printOnNewline('set');
generator.withinBlock(() => {

@@ -323,7 +335,7 @@ let newValueExpression;

else {
generator.printOnNewline("get");
generator.printOnNewline('get');
generator.withinBlock(() => {
generator.printOnNewline(`return snapshot["${responseName}"]! as! ${typeName}`);
});
generator.printOnNewline("set");
generator.printOnNewline('set');
generator.withinBlock(() => {

@@ -343,3 +355,3 @@ generator.printOnNewline(`snapshot.updateValue(newValue, forKey: "${responseName}")`);

const structName = naming_1.structNameForInlineFragment(inlineFragment);
generator.printOnNewline("get");
generator.printOnNewline('get');
generator.withinBlock(() => {

@@ -349,3 +361,3 @@ generator.printOnNewline(`if !${structName}.possibleTypes.contains(__typename) { return nil }`);

});
generator.printOnNewline("set");
generator.printOnNewline('set');
generator.withinBlock(() => {

@@ -393,9 +405,6 @@ generator.printOnNewline(`guard let newValue = newValue else { return }`);

generator.print('(');
generator.print(printing_1.join(properties.map(({ propertyName, type, typeName, isOptional }) => printing_1.join([
`${propertyName}: ${typeName}`,
isOptional && ' = nil'
])), ', '));
generator.print(printing_1.join(properties.map(({ propertyName, typeName, isOptional }) => printing_1.join([`${propertyName}: ${typeName}`, isOptional && ' = nil'])), ', '));
generator.print(')');
}
function selectionSetInitialization(generator, fields, inlineFragments) {
function selectionSetInitialization(generator, fields, inlineFragments, fragmentSpreads) {
generator.print('[');

@@ -415,6 +424,14 @@ generator.withIndent(() => {

});
inlineFragments && inlineFragments.forEach(InlineFragment => {
const structName = naming_1.structNameForInlineFragment(InlineFragment);
generator.printOnNewline(`GraphQLFragmentSpread(${structName}.self),`);
});
inlineFragments &&
inlineFragments.forEach(inlineFragment => {
const structName = naming_1.structNameForInlineFragment(inlineFragment);
generator.printOnNewline(`GraphQLFragmentSpread(${structName}.self),`);
});
if (!generator.context.options.mergeInFieldsFromFragmentSpreads) {
fragmentSpreads &&
fragmentSpreads.forEach(fragmentName => {
const structName = naming_1.structNameForFragmentName(fragmentName);
generator.printOnNewline(`GraphQLFragmentSpread(${structName}.self),`);
});
}
});

@@ -463,6 +480,3 @@ generator.printOnNewline(']');

generator.print('(');
generator.print(printing_1.join(properties.map(({ propertyName, type, typeName, isOptional }) => printing_1.join([
`${propertyName}: ${typeName}`,
isOptional && ' = nil'
])), ', '));
generator.print(printing_1.join(properties.map(({ propertyName, type, typeName, isOptional }) => printing_1.join([`${propertyName}: ${typeName}`, isOptional && ' = nil'])), ', '));
generator.print(')');

@@ -477,7 +491,7 @@ generator.withinBlock(() => {

generator.withinBlock(() => {
generator.printOnNewline("get");
generator.printOnNewline('get');
generator.withinBlock(() => {
generator.printOnNewline(`return graphQLMap["${propertyName}"] as! ${typeName}`);
});
generator.printOnNewline("set");
generator.printOnNewline('set');
generator.withinBlock(() => {

@@ -484,0 +498,0 @@ generator.printOnNewline(`graphQLMap.updateValue(newValue, forKey: "${propertyName}")`);

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

function comment(generator, comment) {
comment && comment.split('\n')
.forEach(line => {
generator.printOnNewline(`/// ${line.trim()}`);
});
comment &&
comment.split('\n').forEach(line => {
generator.printOnNewline(`/// ${line.trim()}`);
});
}

@@ -26,3 +26,3 @@ exports.comment = comment;

exports.namespaceDeclaration = namespaceDeclaration;
function classDeclaration(generator, { className, modifiers, superClass, adoptedProtocols = [], properties }, closure) {
function classDeclaration(generator, { className, modifiers, superClass, adoptedProtocols = [] }, closure) {
generator.printNewlineIfNeeded();

@@ -29,0 +29,0 @@ generator.printOnNewline(printing_1.wrap('', printing_1.join(modifiers, ' '), ' ') + `class ${className}`);

@@ -38,6 +38,3 @@ "use strict";

if (graphql_1.isCompositeType(bareType)) {
const bareTypeName = printing_1.join([
namespace,
language_1.escapeIdentifierIfNeeded(change_case_1.pascalCase(Inflector.singularize(name)))
], '.');
const bareTypeName = printing_1.join([namespace, language_1.escapeIdentifierIfNeeded(change_case_1.pascalCase(Inflector.singularize(name)))], '.');
const typeName = types_1.typeNameFromGraphQLType(context, type, bareTypeName, isOptional);

@@ -71,4 +68,4 @@ return Object.assign({}, field, { propertyName, typeName, bareTypeName, isOptional, isList, isComposite: true });

function isMetaFieldName(name) {
return name.startsWith("__");
return name.startsWith('__');
}
//# sourceMappingURL=naming.js.map

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

[graphql_1.GraphQLBoolean.name]: 'Bool',
[graphql_1.GraphQLID.name]: 'GraphQLID',
[graphql_1.GraphQLID.name]: 'GraphQLID'
};

@@ -42,3 +42,6 @@ function possibleTypesForType(context, type) {

function typeNameForScalarType(context, type) {
return builtInScalarMap[type.name] || (context.passthroughCustomScalars ? context.customScalarsPrefix + type.name : graphql_1.GraphQLString);
return (builtInScalarMap[type.name] ||
(context.options.passthroughCustomScalars
? context.options.customScalarsPrefix + type.name
: graphql_1.GraphQLString.name));
}

@@ -61,4 +64,7 @@ function fieldTypeEnum(context, type, structName) {

}
else {
throw new Error(`Unknown field type: ${type}`);
}
}
exports.fieldTypeEnum = fieldTypeEnum;
//# sourceMappingURL=types.js.map

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

exports.escapedString = escapedString;
function multilineString(context, string) {
function multilineString(generator, string) {
const lines = string.split('\n');
lines.forEach((line, index) => {
const isLastLine = index != lines.length - 1;
context.printOnNewline(`"${escapedString(line)}"` + (isLastLine ? ' +' : ''));
generator.printOnNewline(`"${escapedString(line)}"` + (isLastLine ? ' +' : ''));
});

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

@@ -194,4 +194,3 @@ "use strict";

if (graphql_1.isAbstractType(graphql_1.getNamedType(property.type || property.fieldType))) {
const possibleTypes = getPossibleTypes(generator, property);
const propertySets = Object.keys(possibleTypes)
const propertySets = getPossibleTypeNames(generator, property)
.map(type => {

@@ -242,5 +241,5 @@ const inlineFragment = property.inlineFragments.find(inlineFragment => {

exports.propertyDeclarations = propertyDeclarations;
function getPossibleTypes(generator, property) {
return generator.context.schema._possibleTypeMap[graphql_1.getNamedType(property.fieldType || property.type)];
function getPossibleTypeNames(generator, property) {
return generator.context.schema.getPossibleTypes(graphql_1.getNamedType(property.fieldType || property.type)).map(type => type.name);
}
//# sourceMappingURL=codeGeneration.js.map

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

leave: {
SelectionSet: node => {
SelectionSet: (node) => {
const parentType = typeInfo.getParentType();

@@ -25,2 +25,5 @@ if (!isOperationRootType(parentType)) {

}
else {
return undefined;
}
}

@@ -36,3 +39,3 @@ }

function filePathForNode(node) {
const name = node.loc.source && node.loc.source.name;
const name = node.loc && node.loc.source && node.loc.source.name;
return (name === "GraphQL") ? undefined : name;

@@ -42,28 +45,24 @@ }

function valueFromValueNode(valueNode) {
const kind = valueNode.kind;
if (kind === 'IntValue' || kind === 'FloatValue') {
return Number(valueNode.value);
switch (valueNode.kind) {
case 'IntValue':
case 'FloatValue':
return Number(valueNode.value);
case 'NullValue':
return null;
case 'ListValue':
return valueNode.values.map(valueFromValueNode);
case 'ObjectValue':
return valueNode.fields.reduce((object, field) => {
object[field.name.value] = valueFromValueNode(field.value);
return object;
}, {});
case 'Variable':
return { kind: 'Variable', variableName: valueNode.name.value };
default:
return valueNode.value;
}
else if (kind === 'NullValue') {
return null;
}
else if (kind === 'ListValue') {
return valueNode.values.map(valueFromValueNode);
}
else if (kind === 'ObjectValue') {
return valueNode.fields.reduce((object, field) => {
object[field.name.value] = valueFromValueNode(field.value);
return object;
}, {});
}
else if (kind === 'Variable') {
return { kind, variableName: valueNode.name.value };
}
else {
return valueNode.value;
}
}
exports.valueFromValueNode = valueFromValueNode;
function isTypeProperSuperTypeOf(schema, maybeSuperType, subType) {
return graphql_1.isEqualType(maybeSuperType, subType) || (graphql_1.isAbstractType(maybeSuperType) && schema.isPossibleType(maybeSuperType, subType));
return graphql_1.isEqualType(maybeSuperType, subType) || subType instanceof graphql_1.GraphQLObjectType && (graphql_1.isAbstractType(maybeSuperType) && schema.isPossibleType(maybeSuperType, subType));
}

@@ -112,4 +111,5 @@ exports.isTypeProperSuperTypeOf = isTypeProperSuperTypeOf;

}
return undefined;
}
exports.getFieldDef = getFieldDef;
//# sourceMappingURL=graphql.js.map
{
"name": "apollo-codegen",
"version": "0.16.2",
"version": "0.16.3",
"description": "Generate API code or type annotations based on a GraphQL schema and query documents",

@@ -29,3 +29,2 @@ "main": "./lib/index.js",

"@types/jest": "^20.0.5",
"@types/lodash": "^4.14.71",
"@types/mkdirp": "^0.5.0",

@@ -46,6 +45,4 @@ "@types/node-fetch": "^1.6.7",

"inflected": "^2.0.2",
"lodash": "^4.2.0",
"mkdirp": "^0.5.1",
"node-fetch": "^1.5.3",
"sjcl": "^1.0.6",
"source-map-support": "^0.4.15",

@@ -52,0 +49,0 @@ "yargs": "^8.0.1"

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

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc