apollo-codegen
Advanced tools
Comparing version 0.11.3 to 0.12.0
@@ -136,7 +136,2 @@ 'use strict'; | ||
generator.printOnNewline('public static let selectionSet: [Selection] = '); | ||
selectionSetInitialization(generator, selectionSet, 'Data'); | ||
generator.printNewlineIfNeeded(); | ||
if (variables && variables.length > 0) { | ||
@@ -152,5 +147,8 @@ const properties = variables.map((_ref3) => { | ||
}); | ||
(0, _language.propertyDeclarations)(generator, properties); | ||
generator.printNewlineIfNeeded(); | ||
initializerDeclarationForProperties(generator, properties); | ||
generator.printNewlineIfNeeded(); | ||
@@ -203,3 +201,3 @@ generator.printOnNewline(`public var variables: GraphQLMap?`); | ||
var _ref6$adoptedProtocol = _ref6.adoptedProtocols; | ||
let adoptedProtocols = _ref6$adoptedProtocol === undefined ? ['GraphQLMappable'] : _ref6$adoptedProtocol, | ||
let adoptedProtocols = _ref6$adoptedProtocol === undefined ? ['GraphQLSelectionSet'] : _ref6$adoptedProtocol, | ||
parentType = _ref6.parentType, | ||
@@ -219,25 +217,20 @@ possibleTypes = _ref6.possibleTypes, | ||
generator.print(']'); | ||
generator.printNewlineIfNeeded(); | ||
generator.printOnNewline('public static let selectionSet: [Selection] = '); | ||
selectionSetInitialization(generator, selectionSet, structName); | ||
} | ||
generator.printNewlineIfNeeded(); | ||
generator.printOnNewline('public static let selections: [Selection] = '); | ||
selectionSetInitialization(generator, selectionSet, structName); | ||
const properties = (0, _naming.propertiesFromSelectionSet)(generator.context, selectionSet); | ||
generator.printNewlineIfNeeded(); | ||
properties.forEach((_ref7) => { | ||
let kind = _ref7.kind, | ||
propertyName = _ref7.propertyName, | ||
typeName = _ref7.typeName; | ||
(0, _language.propertyDeclaration)(generator, { propertyName: "snapshot", typeName: "Snapshot" }); | ||
if (kind === 'FragmentSpread') return; | ||
(0, _language.propertyDeclaration)(generator, { | ||
propertyName: propertyName, | ||
typeName: typeName | ||
}); | ||
generator.printNewlineIfNeeded(); | ||
generator.printOnNewline('public init(snapshot: Snapshot)'); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline(`self.snapshot = snapshot`); | ||
}); | ||
const properties = (0, _naming.propertiesFromSelectionSet)(generator.context, selectionSet); | ||
const fields = properties.filter(property => property.kind === 'Field'); | ||
@@ -247,2 +240,3 @@ const inlineFragments = properties.filter(property => property.kind === 'InlineFragment'); | ||
if (!(0, _graphql2.isTypeProperSuperTypeOf)(generator.context.schema, fragmentSpread.fragment.typeCondition, parentType)) { | ||
fragmentSpread.isOptional = true; | ||
fragmentSpread.typeName += '?'; | ||
@@ -253,42 +247,52 @@ } | ||
if (fragmentSpreads.length > 0) { | ||
generator.printNewlineIfNeeded(); | ||
(0, _language.propertyDeclaration)(generator, { propertyName: 'fragments', typeName: 'Fragments' }); | ||
} | ||
generator.printNewlineIfNeeded(); | ||
generator.printOnNewline(`public init`); | ||
generator.print('('); | ||
generator.print((0, _printing.join)(fields.map((_ref7) => { | ||
let propertyName = _ref7.propertyName, | ||
type = _ref7.type, | ||
typeName = _ref7.typeName, | ||
isOptional = _ref7.isOptional; | ||
return (0, _printing.join)([`${propertyName}: ${typeName}`, isOptional && ' = nil']); | ||
}), ', ')); | ||
generator.print(')'); | ||
generator.printNewlineIfNeeded(); | ||
generator.printOnNewline('public init(values: [Any?])'); | ||
generator.withinBlock(() => { | ||
properties.forEach((_ref8, index) => { | ||
let kind = _ref8.kind, | ||
propertyName = _ref8.propertyName, | ||
typeName = _ref8.typeName; | ||
generator.printOnNewline((0, _printing.wrap)(`self.snapshot = [`, (0, _printing.join)(fields.map((_ref8) => { | ||
let name = _ref8.name, | ||
propertyName = _ref8.propertyName; | ||
return `"${propertyName}": ${propertyName}`; | ||
}), ', ') || ':', `]`)); | ||
}); | ||
if (kind == 'FragmentSpread') { | ||
generator.printOnNewline(`let ${propertyName} = values[${index}] as! ${typeName}`); | ||
} else { | ||
generator.printOnNewline(`${propertyName} = values[${index}] as! ${typeName}`); | ||
} | ||
}); | ||
properties.forEach(property => { | ||
if (property.kind === 'FragmentSpread') return; | ||
if (fragmentSpreads.length > 0) { | ||
generator.printNewlineIfNeeded(); | ||
generator.printOnNewline(`fragments = Fragments(`); | ||
generator.print((0, _printing.join)(fragmentSpreads.map((_ref9) => { | ||
let propertyName = _ref9.propertyName; | ||
return `${propertyName}: ${propertyName}`; | ||
}), ', ')); | ||
generator.print(')'); | ||
} | ||
generator.printNewlineIfNeeded(); | ||
propertyDeclarationForSelection(generator, property); | ||
}); | ||
if (fragmentSpreads.length > 0) { | ||
generator.printNewlineIfNeeded(); | ||
generator.printOnNewline(`public var fragments: Fragments`); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline("get"); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline(`return Fragments(snapshot: snapshot)`); | ||
}); | ||
generator.printOnNewline("set"); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline(`snapshot = newValue.snapshot`); | ||
}); | ||
}); | ||
} | ||
if (inlineFragments.length > 0) { | ||
inlineFragments.forEach((_ref10) => { | ||
let bareTypeName = _ref10.bareTypeName, | ||
typeCondition = _ref10.typeCondition, | ||
selectionSet = _ref10.selectionSet; | ||
inlineFragments.forEach((_ref9) => { | ||
let structName = _ref9.structName, | ||
typeCondition = _ref9.typeCondition, | ||
selectionSet = _ref9.selectionSet; | ||
structDeclarationForSelectionSet(generator, { | ||
structName: bareTypeName, | ||
structName: structName, | ||
parentType: typeCondition, | ||
@@ -306,7 +310,24 @@ possibleTypes: (0, _types.possibleTypesForType)(generator.context, typeCondition), | ||
}, () => { | ||
fragmentSpreads.forEach((_ref11) => { | ||
let propertyName = _ref11.propertyName, | ||
typeName = _ref11.typeName; | ||
(0, _language.propertyDeclaration)(generator, { propertyName: "snapshot", typeName: "Snapshot" }); | ||
fragmentSpreads.forEach((_ref10) => { | ||
let propertyName = _ref10.propertyName, | ||
bareTypeName = _ref10.bareTypeName, | ||
typeName = _ref10.typeName, | ||
isOptional = _ref10.isOptional; | ||
(0, _language.propertyDeclaration)(generator, { propertyName: propertyName, typeName: typeName }); | ||
generator.printNewlineIfNeeded(); | ||
generator.printOnNewline(`public var ${propertyName}: ${typeName}`); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline("get"); | ||
generator.withinBlock(() => { | ||
if (isOptional) { | ||
generator.printOnNewline(`if !${typeName}.possibleTypes.contains(__typename) { return nil }`); | ||
} | ||
generator.printOnNewline(`return ${typeName}(snapshot: snapshot)`); | ||
}); | ||
generator.printOnNewline("set"); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline(`snapshot = newValue.snapshot`); | ||
}); | ||
}); | ||
}); | ||
@@ -326,10 +347,108 @@ }); | ||
function mapExpressionForType(context, type, expression) { | ||
let prefix = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : ''; | ||
let isOptional; | ||
if (type instanceof _graphql.GraphQLNonNull) { | ||
isOptional = false; | ||
type = type.ofType; | ||
} else { | ||
isOptional = true; | ||
} | ||
if (type instanceof _graphql.GraphQLList) { | ||
if (isOptional) { | ||
return `${prefix}.flatMap { $0.map { ${mapExpressionForType(context, type.ofType, expression, '$0')} } }`; | ||
} else { | ||
return `${prefix}.map { ${mapExpressionForType(context, type.ofType, expression, '$0')} }`; | ||
} | ||
} else if (isOptional) { | ||
return `${prefix}.flatMap { ${expression} }`; | ||
} else { | ||
return expression; | ||
} | ||
} | ||
function propertyDeclarationForSelection(generator, selection) { | ||
const kind = selection.kind, | ||
propertyName = selection.propertyName, | ||
typeName = selection.typeName, | ||
type = selection.type, | ||
isConditional = selection.isConditional, | ||
description = selection.description; | ||
generator.printOnNewline(description && ` /// ${description}`); | ||
generator.printOnNewline(`public var ${propertyName}: ${typeName}`); | ||
generator.withinBlock(() => { | ||
const namedType = (0, _graphql.getNamedType)(type); | ||
if (kind === 'InlineFragment') { | ||
const structName = (0, _naming.structNameForInlineFragment)(selection); | ||
generator.printOnNewline("get"); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline(`if !${structName}.possibleTypes.contains(__typename) { return nil }`); | ||
generator.printOnNewline(`return ${structName}(snapshot: snapshot)`); | ||
}); | ||
generator.printOnNewline("set"); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline(`guard let newValue = newValue else { return }`); | ||
generator.printOnNewline(`snapshot = newValue.snapshot`); | ||
}); | ||
} else if ((0, _graphql.isCompositeType)(namedType)) { | ||
const isOptional = isConditional || !(type instanceof _graphql.GraphQLNonNull); | ||
const isList = type instanceof _graphql.GraphQLList || type.ofType instanceof _graphql.GraphQLList; | ||
const structName = (0, _language.escapeIdentifierIfNeeded)((0, _naming.structNameForPropertyName)(propertyName)); | ||
if (isList) { | ||
generator.printOnNewline("get"); | ||
generator.withinBlock(() => { | ||
const snapshotTypeName = (0, _types.typeNameFromGraphQLType)(generator.context, type, 'Snapshot', isOptional); | ||
let getter = `return (snapshot["${propertyName}"]! as! ${snapshotTypeName})`; | ||
getter += mapExpressionForType(generator.context, type, `${structName}(snapshot: $0)`); | ||
generator.printOnNewline(getter); | ||
}); | ||
generator.printOnNewline("set"); | ||
generator.withinBlock(() => { | ||
let setter = `snapshot["${propertyName}"] = newValue`; | ||
setter += mapExpressionForType(generator.context, type, `$0.snapshot`); | ||
generator.printOnNewline(setter); | ||
}); | ||
} else { | ||
generator.printOnNewline("get"); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline(`return ${structName}(snapshot: snapshot["${propertyName}"]! as! Snapshot)`); | ||
}); | ||
generator.printOnNewline("set"); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline(`snapshot["${propertyName}"] = `); | ||
if (isOptional) { | ||
generator.print('newValue?.snapshot'); | ||
} else { | ||
generator.print('newValue.snapshot'); | ||
} | ||
}); | ||
} | ||
} else { | ||
generator.printOnNewline("get"); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline(`return snapshot["${propertyName}"]! as! ${typeName}`); | ||
}); | ||
generator.printOnNewline("set"); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline(`snapshot["${propertyName}"] = newValue`); | ||
}); | ||
} | ||
}); | ||
} | ||
function initializerDeclarationForProperties(generator, properties) { | ||
generator.printOnNewline(`public init`); | ||
generator.print('('); | ||
generator.print((0, _printing.join)(properties.map((_ref12) => { | ||
let propertyName = _ref12.propertyName, | ||
type = _ref12.type, | ||
typeName = _ref12.typeName, | ||
isOptional = _ref12.isOptional; | ||
generator.print((0, _printing.join)(properties.map((_ref11) => { | ||
let propertyName = _ref11.propertyName, | ||
type = _ref11.type, | ||
typeName = _ref11.typeName, | ||
isOptional = _ref11.isOptional; | ||
return (0, _printing.join)([`${propertyName}: ${typeName}`, isOptional && ' = nil']); | ||
@@ -340,4 +459,4 @@ }), ', ')); | ||
generator.withinBlock(() => { | ||
properties.forEach((_ref13) => { | ||
let propertyName = _ref13.propertyName; | ||
properties.forEach((_ref12) => { | ||
let propertyName = _ref12.propertyName; | ||
@@ -357,4 +476,3 @@ generator.printOnNewline(`self.${propertyName} = ${propertyName}`); | ||
args = selection.args, | ||
type = selection.type, | ||
fieldSelectionSet = selection.selectionSet; | ||
type = selection.type; | ||
@@ -365,6 +483,2 @@ const structName = (0, _printing.join)([parentStructName, (0, _naming.structNameForPropertyName)(responseName)], '.'); | ||
generator.print((0, _printing.join)([`"${fieldName}"`, responseName != fieldName ? `alias: "${responseName}"` : null, args && args.length && `arguments: ${(0, _values3.dictionaryLiteralForFieldArguments)(args)}`, `type: ${(0, _types.fieldTypeEnum)(generator.context, type, structName)}`], ', ')); | ||
if (fieldSelectionSet && fieldSelectionSet.length > 0) { | ||
generator.print(', selectionSet: '); | ||
selectionSetInitialization(generator, selection.selectionSet, structName); | ||
} | ||
generator.print('),'); | ||
@@ -421,7 +535,7 @@ } else if (selection.kind === 'FragmentSpread') { | ||
generator.print('('); | ||
generator.print((0, _printing.join)(properties.map((_ref14) => { | ||
let propertyName = _ref14.propertyName, | ||
type = _ref14.type, | ||
typeName = _ref14.typeName, | ||
isOptional = _ref14.isOptional; | ||
generator.print((0, _printing.join)(properties.map((_ref13) => { | ||
let propertyName = _ref13.propertyName, | ||
type = _ref13.type, | ||
typeName = _ref13.typeName, | ||
isOptional = _ref13.isOptional; | ||
return (0, _printing.join)([`${propertyName}: ${typeName}`, isOptional && ' = nil']); | ||
@@ -432,5 +546,5 @@ }), ', ')); | ||
generator.withinBlock(() => { | ||
generator.printOnNewline((0, _printing.wrap)(`graphQLMap = [`, (0, _printing.join)(properties.map((_ref15) => { | ||
let name = _ref15.name, | ||
propertyName = _ref15.propertyName; | ||
generator.printOnNewline((0, _printing.wrap)(`graphQLMap = [`, (0, _printing.join)(properties.map((_ref14) => { | ||
let name = _ref14.name, | ||
propertyName = _ref14.propertyName; | ||
return `"${name}": ${propertyName}`; | ||
@@ -437,0 +551,0 @@ }), ', ') || ':', `]`)); |
@@ -80,4 +80,4 @@ 'use strict'; | ||
generator.printOnNewline(`public let ${propertyName}: ${typeName}`); | ||
generator.print(description && ` /// ${description}`); | ||
generator.printOnNewline(description && ` /// ${description}`); | ||
generator.printOnNewline(`public var ${propertyName}: ${typeName}`); | ||
} | ||
@@ -84,0 +84,0 @@ |
@@ -73,2 +73,3 @@ 'use strict'; | ||
const type = field.type; | ||
const isList = type instanceof _graphql.GraphQLList || type.ofType instanceof _graphql.GraphQLList; | ||
const isOptional = field.isConditional || !(type instanceof _graphql.GraphQLNonNull); | ||
@@ -80,6 +81,6 @@ const bareType = (0, _graphql.getNamedType)(type); | ||
const typeName = (0, _types.typeNameFromGraphQLType)(context, type, bareTypeName, isOptional); | ||
return (0, _extends3.default)({}, field, { propertyName: propertyName, typeName: typeName, bareTypeName: bareTypeName, isOptional: isOptional, isComposite: true }); | ||
return (0, _extends3.default)({}, field, { propertyName: propertyName, typeName: typeName, bareTypeName: bareTypeName, isOptional: isOptional, isList: isList, isComposite: true }); | ||
} else { | ||
const typeName = (0, _types.typeNameFromGraphQLType)(context, type, undefined, isOptional); | ||
return (0, _extends3.default)({}, field, { propertyName: propertyName, typeName: typeName, isOptional: isOptional, isComposite: false }); | ||
return (0, _extends3.default)({}, field, { propertyName: propertyName, typeName: typeName, isOptional: isOptional, isList: isList, isComposite: false }); | ||
} | ||
@@ -89,6 +90,6 @@ } | ||
function propertyFromInlineFragment(context, inlineFragment) { | ||
const bareTypeName = structNameForInlineFragment(inlineFragment); | ||
const propertyName = (0, _changeCase.camelCase)(bareTypeName); | ||
const typeName = bareTypeName + '?'; | ||
return (0, _extends3.default)({ propertyName: propertyName, typeName: typeName, bareTypeName: bareTypeName }, inlineFragment); | ||
const structName = structNameForInlineFragment(inlineFragment); | ||
const propertyName = (0, _changeCase.camelCase)(structName); | ||
const typeName = structName + '?'; | ||
return (0, _extends3.default)({ propertyName: propertyName, typeName: typeName, structName: structName, isComposite: true }, inlineFragment); | ||
} | ||
@@ -104,3 +105,3 @@ | ||
const typeName = structNameForFragmentName(fragmentName); | ||
return (0, _extends3.default)({ propertyName: propertyName, typeName: typeName, fragment: fragment }, fragmentSpread); | ||
return (0, _extends3.default)({ propertyName: propertyName, typeName: typeName, fragment: fragment, isComposite: true }, fragmentSpread); | ||
} | ||
@@ -107,0 +108,0 @@ |
{ | ||
"name": "apollo-codegen", | ||
"version": "0.11.3", | ||
"version": "0.12.0", | ||
"description": "Generate API code or type annotations based on a GraphQL schema and query documents", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
454406
8036