apollo-codegen
Advanced tools
Comparing version 0.17.0-alpha.8 to 0.17.0-alpha.9
@@ -12,3 +12,3 @@ "use strict"; | ||
inspect() { | ||
return `${util_1.inspect(this.possibleTypes)} -> ${util_1.inspect(collectAndMergeFields_1.collectAndMergeFields(this, false).map(field => field.responseKey))}\n`; | ||
return `${util_1.inspect(this.possibleTypes)} -> ${util_1.inspect(collectAndMergeFields_1.collectAndMergeFields(this, false).map(field => field.responseKey))} ${util_1.inspect(this.fragmentSpreads.map(fragmentSpread => fragmentSpread.fragmentName))}\n`; | ||
} | ||
@@ -36,35 +36,18 @@ } | ||
if (mergeInFragmentSpreads) { | ||
for (const { possibleTypes, selections } of typeCaseForSelectionSet({ | ||
typeCase.merge(typeCaseForSelectionSet({ | ||
possibleTypes: selectionSet.possibleTypes.filter(type => selection.selectionSet.possibleTypes.includes(type)), | ||
selections: selection.selectionSet.selections | ||
}).defaultAndVariants) { | ||
if (selections.length < 1) | ||
continue; | ||
for (const variant of typeCase.disjointVariantsFor(possibleTypes)) { | ||
variant.selections.push(...selections); | ||
} | ||
} | ||
}, mergeInFragmentSpreads)); | ||
} | ||
break; | ||
case 'TypeCondition': | ||
for (const { possibleTypes, selections } of typeCaseForSelectionSet({ | ||
typeCase.merge(typeCaseForSelectionSet({ | ||
possibleTypes: selectionSet.possibleTypes.filter(type => selection.selectionSet.possibleTypes.includes(type)), | ||
selections: selection.selectionSet.selections | ||
}).defaultAndVariants) { | ||
if (selections.length < 1) | ||
continue; | ||
for (const variant of typeCase.disjointVariantsFor(possibleTypes)) { | ||
variant.selections.push(...selections); | ||
} | ||
} | ||
}, mergeInFragmentSpreads)); | ||
break; | ||
case 'BooleanCondition': | ||
for (const { possibleTypes, selections } of typeCaseForSelectionSet(selection.selectionSet) | ||
.defaultAndVariants) { | ||
for (const variant of typeCase.disjointVariantsFor(possibleTypes)) { | ||
if (selections.length < 1) | ||
continue; | ||
variant.selections.push(Object.assign({}, selection, { selectionSet: { possibleTypes: variant.possibleTypes, selections } })); | ||
} | ||
} | ||
typeCase.merge(typeCaseForSelectionSet(selection.selectionSet, mergeInFragmentSpreads), selectionSet => [ | ||
Object.assign({}, selection, { selectionSet }) | ||
]); | ||
break; | ||
@@ -132,2 +115,14 @@ } | ||
} | ||
merge(otherTypeCase, transform) { | ||
for (const otherVariant of otherTypeCase.defaultAndVariants) { | ||
if (otherVariant.selections.length < 1) | ||
continue; | ||
for (const variant of this.disjointVariantsFor(otherVariant.possibleTypes)) { | ||
if (otherVariant.fragmentSpreads.length > 0) { | ||
variant.fragmentSpreads = [...variant.fragmentSpreads, ...otherVariant.fragmentSpreads].filter((a, index, array) => array.findIndex(b => b.fragmentName == a.fragmentName) == index); | ||
} | ||
variant.selections.push(...(transform ? transform(otherVariant) : otherVariant.selections)); | ||
} | ||
} | ||
} | ||
inspect() { | ||
@@ -134,0 +129,0 @@ return (`TypeCase\n` + |
@@ -122,15 +122,37 @@ "use strict"; | ||
} | ||
structDeclarationForSelectionSet({ structName, adoptedProtocols = ['GraphQLSelectionSet'], selectionSet }, beforeClosure) { | ||
structDeclarationForSelectionSet({ structName, adoptedProtocols = ['GraphQLSelectionSet'], selectionSet }, before) { | ||
const typeCase = typeCase_1.typeCaseForSelectionSet(selectionSet, this.context.options.mergeInFieldsFromFragmentSpreads); | ||
this.structDeclarationForVariant({ | ||
structName, | ||
adoptedProtocols, | ||
variant: typeCase.default, | ||
typeCase | ||
}, before, () => { | ||
const variants = typeCase.variants.map(this.helpers.propertyFromVariant, this.helpers); | ||
for (const variant of variants) { | ||
this.propertyDeclarationForVariant(variant); | ||
this.structDeclarationForVariant({ | ||
structName: variant.structName, | ||
variant | ||
}); | ||
} | ||
}); | ||
} | ||
structDeclarationForVariant({ structName, adoptedProtocols = ['GraphQLSelectionSet'], variant, typeCase }, before, after) { | ||
this.structDeclaration({ structName, adoptedProtocols }, () => { | ||
if (beforeClosure) { | ||
beforeClosure(); | ||
if (before) { | ||
before(); | ||
} | ||
this.printNewlineIfNeeded(); | ||
this.printOnNewline('public static let possibleTypes = ['); | ||
this.print(printing_1.join(selectionSet.possibleTypes.map(type => `"${type.name}"`), ', ')); | ||
this.print(printing_1.join(variant.possibleTypes.map(type => `"${type.name}"`), ', ')); | ||
this.print(']'); | ||
this.printNewlineIfNeeded(); | ||
this.printOnNewline('public static let selections: [GraphQLSelection] = '); | ||
this.typeCaseInitialization(typeCase); | ||
if (typeCase) { | ||
this.typeCaseInitialization(typeCase); | ||
} | ||
else { | ||
this.selectionSetInitialization(variant); | ||
} | ||
this.printNewlineIfNeeded(); | ||
@@ -146,7 +168,11 @@ this.propertyDeclaration({ | ||
}); | ||
this.initializersForTypeCase(typeCase); | ||
const fields = collectAndMergeFields_1.collectAndMergeFields(typeCase.default, this.context.options.mergeInFieldsFromFragmentSpreads).map(field => this.helpers.propertyFromField(field)); | ||
const variants = typeCase.variants.map(this.helpers.propertyFromVariant, this.helpers); | ||
const fragmentSpreads = typeCase.default.fragmentSpreads.map(fragmentSpread => { | ||
const isConditional = selectionSet.possibleTypes.some(type => !fragmentSpread.selectionSet.possibleTypes.includes(type)); | ||
if (typeCase) { | ||
this.initializersForTypeCase(typeCase); | ||
} | ||
else { | ||
this.initializersForVariant(variant); | ||
} | ||
const fields = collectAndMergeFields_1.collectAndMergeFields(variant, this.context.options.mergeInFieldsFromFragmentSpreads).map(field => this.helpers.propertyFromField(field)); | ||
const fragmentSpreads = variant.fragmentSpreads.map(fragmentSpread => { | ||
const isConditional = variant.possibleTypes.some(type => !fragmentSpread.selectionSet.possibleTypes.includes(type)); | ||
return this.helpers.propertyFromFragmentSpread(fragmentSpread, isConditional); | ||
@@ -201,9 +227,2 @@ }); | ||
} | ||
for (const variant of variants) { | ||
this.propertyDeclarationForVariant(variant); | ||
this.structDeclarationForSelectionSet({ | ||
structName: variant.structName, | ||
selectionSet: variant.selectionSet | ||
}); | ||
} | ||
for (const field of fields) { | ||
@@ -217,2 +236,5 @@ if (graphql_1.isCompositeType(graphql_1.getNamedType(field.type)) && field.selectionSet) { | ||
} | ||
if (after) { | ||
after(); | ||
} | ||
}); | ||
@@ -222,15 +244,23 @@ } | ||
const variants = typeCase.variants; | ||
const propertiesForSelectionSet = (selectionSet, namespace) => { | ||
return collectAndMergeFields_1.collectAndMergeFields(selectionSet, true) | ||
.filter(field => field.name != '__typename') | ||
.map(field => this.helpers.propertyFromField(field, namespace)); | ||
}; | ||
if (variants.length == 0 && typeCase.default.possibleTypes.length == 1) { | ||
if (variants.length == 0) { | ||
this.initializersForVariant(typeCase.default); | ||
} | ||
else { | ||
const remainder = typeCase.remainder; | ||
for (const variant of remainder ? [remainder, ...variants] : variants) { | ||
this.initializersForVariant(variant, variant === remainder ? undefined : this.helpers.structNameForVariant(variant), false); | ||
} | ||
} | ||
} | ||
initializersForVariant(variant, namespace, useInitializerIfPossible = true) { | ||
if (useInitializerIfPossible && variant.possibleTypes.length == 1) { | ||
const properties = this.helpers.propertiesForSelectionSet(variant); | ||
if (!properties) | ||
return; | ||
this.printNewlineIfNeeded(); | ||
this.printOnNewline(`public init`); | ||
const properties = propertiesForSelectionSet(typeCase.default); | ||
this.parametersForProperties(properties); | ||
this.withinBlock(() => { | ||
this.printOnNewline(printing_1.wrap(`self.init(snapshot: [`, printing_1.join([ | ||
`"__typename": "${typeCase.default.possibleTypes[0]}"`, | ||
`"__typename": "${variant.possibleTypes[0]}"`, | ||
...properties.map(this.propertyAssignmentForField, this) | ||
@@ -241,21 +271,17 @@ ], ', ') || ':', `])`)); | ||
else { | ||
const remainder = typeCase.remainder; | ||
for (const variant of remainder ? [remainder, ...variants] : variants) { | ||
const structName = this.scope.typeName; | ||
for (const possibleType of variant.possibleTypes) { | ||
const properties = propertiesForSelectionSet({ | ||
possibleTypes: [possibleType], | ||
selections: variant.selections | ||
}, variant === remainder ? undefined : this.helpers.structNameForVariant(variant)); | ||
this.printNewlineIfNeeded(); | ||
this.printOnNewline(`public static func make${possibleType}`); | ||
this.parametersForProperties(properties); | ||
this.print(` -> ${structName}`); | ||
this.withinBlock(() => { | ||
this.printOnNewline(printing_1.wrap(`return ${structName}(snapshot: [`, printing_1.join([ | ||
`"__typename": "${possibleType}"`, | ||
...properties.map(this.propertyAssignmentForField, this) | ||
], ', ') || ':', `])`)); | ||
}); | ||
} | ||
const structName = this.scope.typeName; | ||
for (const possibleType of variant.possibleTypes) { | ||
const properties = this.helpers.propertiesForSelectionSet({ | ||
possibleTypes: [possibleType], | ||
selections: variant.selections | ||
}, namespace); | ||
if (!properties) | ||
continue; | ||
this.printNewlineIfNeeded(); | ||
this.printOnNewline(`public static func make${possibleType}`); | ||
this.parametersForProperties(properties); | ||
this.print(` -> ${structName}`); | ||
this.withinBlock(() => { | ||
this.printOnNewline(printing_1.wrap(`return ${structName}(snapshot: [`, printing_1.join([`"__typename": "${possibleType}"`, ...properties.map(this.propertyAssignmentForField, this)], ', ') || ':', `])`)); | ||
}); | ||
} | ||
@@ -267,3 +293,3 @@ } | ||
const valueExpression = graphql_1.isCompositeType(graphql_1.getNamedType(type)) | ||
? this.helpers.mapExpressionForType(type, (identifier) => `${identifier}.snapshot`, language_1.escapeIdentifierIfNeeded(propertyName)) | ||
? this.helpers.mapExpressionForType(type, identifier => `${identifier}.snapshot`, language_1.escapeIdentifierIfNeeded(propertyName)) | ||
: language_1.escapeIdentifierIfNeeded(propertyName); | ||
@@ -293,3 +319,3 @@ return `"${responseKey}": ${valueExpression}`; | ||
} | ||
getter += this.helpers.mapExpressionForType(type, (identifier) => `${structName}(snapshot: ${identifier})`); | ||
getter += this.helpers.mapExpressionForType(type, identifier => `${structName}(snapshot: ${identifier})`); | ||
this.printOnNewline(getter); | ||
@@ -299,3 +325,3 @@ }); | ||
this.withinBlock(() => { | ||
let newValueExpression = this.helpers.mapExpressionForType(type, (identifier) => `${identifier}.snapshot`, 'newValue'); | ||
let newValueExpression = this.helpers.mapExpressionForType(type, identifier => `${identifier}.snapshot`, 'newValue'); | ||
this.printOnNewline(`snapshot.updateValue(${newValueExpression}, forKey: "${responseKey}")`); | ||
@@ -302,0 +328,0 @@ }); |
@@ -8,2 +8,3 @@ "use strict"; | ||
const graphql_2 = require("../utilities/graphql"); | ||
const collectAndMergeFields_1 = require("../compiler/visitors/collectAndMergeFields"); | ||
const builtInScalarMap = { | ||
@@ -102,8 +103,7 @@ [graphql_1.GraphQLString.name]: 'String', | ||
const structName = this.structNameForVariant(variant); | ||
return { | ||
return Object.assign(variant, { | ||
propertyName: change_case_1.camelCase(structName), | ||
typeName: structName + '?', | ||
structName, | ||
selectionSet: variant | ||
}; | ||
structName | ||
}); | ||
} | ||
@@ -126,2 +126,12 @@ propertyFromFragmentSpread(fragmentSpread, isConditional) { | ||
} | ||
propertiesForSelectionSet(selectionSet, namespace) { | ||
const properties = collectAndMergeFields_1.collectAndMergeFields(selectionSet, true) | ||
.filter(field => field.name !== '__typename') | ||
.map(field => this.propertyFromField(field, namespace)); | ||
if (selectionSet.selections.some(selection => selection.kind === 'FragmentSpread') && | ||
properties.some(property => graphql_1.isCompositeType(graphql_1.getNamedType(property.type)))) { | ||
return undefined; | ||
} | ||
return properties; | ||
} | ||
dictionaryLiteralForFieldArguments(args) { | ||
@@ -128,0 +138,0 @@ function expressionFromValue(value) { |
{ | ||
"name": "apollo-codegen", | ||
"version": "0.17.0-alpha.8", | ||
"version": "0.17.0-alpha.9", | ||
"description": "Generate API code or type annotations based on a GraphQL schema and query documents", | ||
@@ -35,3 +35,3 @@ "main": "./lib/index.js", | ||
"common-tags": "^1.4.0", | ||
"jest": "^21.1.0", | ||
"jest": "^21.2.1", | ||
"jest-matcher-utils": "^21.1.0", | ||
@@ -38,0 +38,0 @@ "ts-jest": "^21.0.1", |
@@ -18,3 +18,3 @@ import { inspect } from 'util'; | ||
collectAndMergeFields(this, false).map(field => field.responseKey) | ||
)}\n`; | ||
)} ${inspect(this.fragmentSpreads.map(fragmentSpread => fragmentSpread.fragmentName))}\n`; | ||
} | ||
@@ -46,2 +46,3 @@ } | ||
variant.fragmentSpreads.push(selection); | ||
if (!mergeInFragmentSpreads) { | ||
@@ -52,39 +53,38 @@ variant.selections.push(selection); | ||
if (mergeInFragmentSpreads) { | ||
for (const { possibleTypes, selections } of typeCaseForSelectionSet({ | ||
possibleTypes: selectionSet.possibleTypes.filter(type => | ||
selection.selectionSet.possibleTypes.includes(type) | ||
), | ||
selections: selection.selectionSet.selections | ||
}).defaultAndVariants) { | ||
if (selections.length < 1) continue; | ||
for (const variant of typeCase.disjointVariantsFor(possibleTypes)) { | ||
variant.selections.push(...selections); | ||
} | ||
} | ||
typeCase.merge( | ||
typeCaseForSelectionSet( | ||
{ | ||
possibleTypes: selectionSet.possibleTypes.filter(type => | ||
selection.selectionSet.possibleTypes.includes(type) | ||
), | ||
selections: selection.selectionSet.selections | ||
}, | ||
mergeInFragmentSpreads | ||
) | ||
); | ||
} | ||
break; | ||
case 'TypeCondition': | ||
for (const { possibleTypes, selections } of typeCaseForSelectionSet({ | ||
possibleTypes: selectionSet.possibleTypes.filter(type => | ||
selection.selectionSet.possibleTypes.includes(type) | ||
), | ||
selections: selection.selectionSet.selections | ||
}).defaultAndVariants) { | ||
if (selections.length < 1) continue; | ||
for (const variant of typeCase.disjointVariantsFor(possibleTypes)) { | ||
variant.selections.push(...selections); | ||
} | ||
} | ||
typeCase.merge( | ||
typeCaseForSelectionSet( | ||
{ | ||
possibleTypes: selectionSet.possibleTypes.filter(type => | ||
selection.selectionSet.possibleTypes.includes(type) | ||
), | ||
selections: selection.selectionSet.selections | ||
}, | ||
mergeInFragmentSpreads | ||
) | ||
); | ||
break; | ||
case 'BooleanCondition': | ||
for (const { possibleTypes, selections } of typeCaseForSelectionSet(selection.selectionSet) | ||
.defaultAndVariants) { | ||
for (const variant of typeCase.disjointVariantsFor(possibleTypes)) { | ||
if (selections.length < 1) continue; | ||
variant.selections.push({ | ||
typeCase.merge( | ||
typeCaseForSelectionSet(selection.selectionSet, mergeInFragmentSpreads), | ||
selectionSet => [ | ||
{ | ||
...selection, | ||
selectionSet: { possibleTypes: variant.possibleTypes, selections } | ||
}); | ||
} | ||
} | ||
selectionSet | ||
} | ||
] | ||
); | ||
break; | ||
@@ -183,2 +183,17 @@ } | ||
merge(otherTypeCase: TypeCase, transform?: (selectionSet: SelectionSet) => Selection[]) { | ||
for (const otherVariant of otherTypeCase.defaultAndVariants) { | ||
if (otherVariant.selections.length < 1) continue; | ||
for (const variant of this.disjointVariantsFor(otherVariant.possibleTypes)) { | ||
if (otherVariant.fragmentSpreads.length > 0) { | ||
// Union of variant.fragmentSpreads and otherVariant.fragmentSpreads | ||
variant.fragmentSpreads = [...variant.fragmentSpreads, ...otherVariant.fragmentSpreads].filter( | ||
(a, index, array) => array.findIndex(b => b.fragmentName == a.fragmentName) == index | ||
); | ||
} | ||
variant.selections.push(...(transform ? transform(otherVariant) : otherVariant.selections)); | ||
} | ||
} | ||
} | ||
inspect() { | ||
@@ -185,0 +200,0 @@ return ( |
@@ -20,3 +20,3 @@ import { | ||
import { typeCaseForSelectionSet, TypeCase } from '../compiler/visitors/typeCase'; | ||
import { typeCaseForSelectionSet, TypeCase, Variant } from '../compiler/visitors/typeCase'; | ||
import { collectFragmentsReferenced } from '../compiler/visitors/collectFragmentsReferenced'; | ||
@@ -194,9 +194,50 @@ import { generateOperationId } from '../compiler/visitors/generateOperationId'; | ||
}, | ||
beforeClosure?: Function | ||
before?: Function | ||
) { | ||
const typeCase = typeCaseForSelectionSet(selectionSet, this.context.options.mergeInFieldsFromFragmentSpreads); | ||
const typeCase = typeCaseForSelectionSet( | ||
selectionSet, | ||
this.context.options.mergeInFieldsFromFragmentSpreads | ||
); | ||
this.structDeclarationForVariant( | ||
{ | ||
structName, | ||
adoptedProtocols, | ||
variant: typeCase.default, | ||
typeCase | ||
}, | ||
before, | ||
() => { | ||
const variants = typeCase.variants.map(this.helpers.propertyFromVariant, this.helpers); | ||
for (const variant of variants) { | ||
this.propertyDeclarationForVariant(variant); | ||
this.structDeclarationForVariant({ | ||
structName: variant.structName, | ||
variant | ||
}); | ||
} | ||
} | ||
); | ||
} | ||
structDeclarationForVariant( | ||
{ | ||
structName, | ||
adoptedProtocols = ['GraphQLSelectionSet'], | ||
variant, | ||
typeCase | ||
}: { | ||
structName: string; | ||
adoptedProtocols?: string[]; | ||
variant: Variant; | ||
typeCase?: TypeCase; | ||
}, | ||
before?: Function, | ||
after?: Function | ||
) { | ||
this.structDeclaration({ structName, adoptedProtocols }, () => { | ||
if (beforeClosure) { | ||
beforeClosure(); | ||
if (before) { | ||
before(); | ||
} | ||
@@ -206,3 +247,3 @@ | ||
this.printOnNewline('public static let possibleTypes = ['); | ||
this.print(join(selectionSet.possibleTypes.map(type => `"${type.name}"`), ', ')); | ||
this.print(join(variant.possibleTypes.map(type => `"${type.name}"`), ', ')); | ||
this.print(']'); | ||
@@ -212,3 +253,7 @@ | ||
this.printOnNewline('public static let selections: [GraphQLSelection] = '); | ||
this.typeCaseInitialization(typeCase); | ||
if (typeCase) { | ||
this.typeCaseInitialization(typeCase); | ||
} else { | ||
this.selectionSetInitialization(variant); | ||
} | ||
@@ -228,13 +273,15 @@ this.printNewlineIfNeeded(); | ||
this.initializersForTypeCase(typeCase); | ||
if (typeCase) { | ||
this.initializersForTypeCase(typeCase); | ||
} else { | ||
this.initializersForVariant(variant); | ||
} | ||
const fields = collectAndMergeFields( | ||
typeCase.default, | ||
variant, | ||
this.context.options.mergeInFieldsFromFragmentSpreads | ||
).map(field => this.helpers.propertyFromField(field as Field)); | ||
const variants = typeCase.variants.map(this.helpers.propertyFromVariant, this.helpers); | ||
const fragmentSpreads = typeCase.default.fragmentSpreads.map(fragmentSpread => { | ||
const isConditional = selectionSet.possibleTypes.some( | ||
const fragmentSpreads = variant.fragmentSpreads.map(fragmentSpread => { | ||
const isConditional = variant.possibleTypes.some( | ||
type => !fragmentSpread.selectionSet.possibleTypes.includes(type) | ||
@@ -302,11 +349,2 @@ ); | ||
for (const variant of variants) { | ||
this.propertyDeclarationForVariant(variant); | ||
this.structDeclarationForSelectionSet({ | ||
structName: variant.structName, | ||
selectionSet: variant.selectionSet | ||
}); | ||
} | ||
for (const field of fields) { | ||
@@ -320,2 +358,6 @@ if (isCompositeType(getNamedType(field.type)) && field.selectionSet) { | ||
} | ||
if (after) { | ||
after(); | ||
} | ||
}); | ||
@@ -327,17 +369,24 @@ } | ||
const propertiesForSelectionSet = ( | ||
selectionSet: SelectionSet, | ||
namespace?: string | ||
): (Field & Property)[] => { | ||
return collectAndMergeFields(selectionSet, true) | ||
.filter(field => field.name != '__typename') | ||
.map(field => this.helpers.propertyFromField(field, namespace)); | ||
}; | ||
if (variants.length == 0) { | ||
this.initializersForVariant(typeCase.default); | ||
} else { | ||
const remainder = typeCase.remainder; | ||
for (const variant of remainder ? [remainder, ...variants] : variants) { | ||
this.initializersForVariant( | ||
variant, | ||
variant === remainder ? undefined : this.helpers.structNameForVariant(variant), | ||
false | ||
); | ||
} | ||
} | ||
} | ||
if (variants.length == 0 && typeCase.default.possibleTypes.length == 1) { | ||
initializersForVariant(variant: Variant, namespace?: string, useInitializerIfPossible: boolean = true) { | ||
if (useInitializerIfPossible && variant.possibleTypes.length == 1) { | ||
const properties = this.helpers.propertiesForSelectionSet(variant); | ||
if (!properties) return; | ||
this.printNewlineIfNeeded(); | ||
this.printOnNewline(`public init`); | ||
const properties = propertiesForSelectionSet(typeCase.default); | ||
this.parametersForProperties(properties); | ||
@@ -351,3 +400,3 @@ | ||
[ | ||
`"__typename": "${typeCase.default.possibleTypes[0]}"`, | ||
`"__typename": "${variant.possibleTypes[0]}"`, | ||
...properties.map(this.propertyAssignmentForField, this) | ||
@@ -362,39 +411,34 @@ ], | ||
} else { | ||
const remainder = typeCase.remainder; | ||
const structName = this.scope.typeName; | ||
for (const variant of remainder ? [remainder, ...variants] : variants) { | ||
const structName = this.scope.typeName; | ||
for (const possibleType of variant.possibleTypes) { | ||
const properties = this.helpers.propertiesForSelectionSet( | ||
{ | ||
possibleTypes: [possibleType], | ||
selections: variant.selections | ||
}, | ||
namespace | ||
); | ||
for (const possibleType of variant.possibleTypes) { | ||
const properties = propertiesForSelectionSet( | ||
{ | ||
possibleTypes: [possibleType], | ||
selections: variant.selections | ||
}, | ||
variant === remainder ? undefined : this.helpers.structNameForVariant(variant) | ||
); | ||
if (!properties) continue; | ||
this.printNewlineIfNeeded(); | ||
this.printOnNewline(`public static func make${possibleType}`); | ||
this.printNewlineIfNeeded(); | ||
this.printOnNewline(`public static func make${possibleType}`); | ||
this.parametersForProperties(properties); | ||
this.parametersForProperties(properties); | ||
this.print(` -> ${structName}`); | ||
this.print(` -> ${structName}`); | ||
this.withinBlock(() => { | ||
this.printOnNewline( | ||
wrap( | ||
`return ${structName}(snapshot: [`, | ||
join( | ||
[ | ||
`"__typename": "${possibleType}"`, | ||
...properties.map(this.propertyAssignmentForField, this) | ||
], | ||
', ' | ||
) || ':', | ||
`])` | ||
) | ||
); | ||
}); | ||
} | ||
this.withinBlock(() => { | ||
this.printOnNewline( | ||
wrap( | ||
`return ${structName}(snapshot: [`, | ||
join( | ||
[`"__typename": "${possibleType}"`, ...properties.map(this.propertyAssignmentForField, this)], | ||
', ' | ||
) || ':', | ||
`])` | ||
) | ||
); | ||
}); | ||
} | ||
@@ -404,6 +448,10 @@ } | ||
propertyAssignmentForField(field: { responseKey: string, propertyName: string, type: GraphQLType }) { | ||
propertyAssignmentForField(field: { responseKey: string; propertyName: string; type: GraphQLType }) { | ||
const { responseKey, propertyName, type } = field; | ||
const valueExpression = isCompositeType(getNamedType(type)) | ||
? this.helpers.mapExpressionForType(type, (identifier) => `${identifier}.snapshot`, escapeIdentifierIfNeeded(propertyName)) | ||
? this.helpers.mapExpressionForType( | ||
type, | ||
identifier => `${identifier}.snapshot`, | ||
escapeIdentifierIfNeeded(propertyName) | ||
) | ||
: escapeIdentifierIfNeeded(propertyName); | ||
@@ -438,3 +486,6 @@ return `"${responseKey}": ${valueExpression}`; | ||
} | ||
getter += this.helpers.mapExpressionForType(type, (identifier) => `${structName}(snapshot: ${identifier})`); | ||
getter += this.helpers.mapExpressionForType( | ||
type, | ||
identifier => `${structName}(snapshot: ${identifier})` | ||
); | ||
this.printOnNewline(getter); | ||
@@ -444,3 +495,7 @@ }); | ||
this.withinBlock(() => { | ||
let newValueExpression = this.helpers.mapExpressionForType(type, (identifier) => `${identifier}.snapshot`, 'newValue'); | ||
let newValueExpression = this.helpers.mapExpressionForType( | ||
type, | ||
identifier => `${identifier}.snapshot`, | ||
'newValue' | ||
); | ||
this.printOnNewline(`snapshot.updateValue(${newValueExpression}, forKey: "${responseKey}")`); | ||
@@ -647,3 +702,3 @@ }); | ||
this.comment(value.description); | ||
this.deprecationAttributes(value.isDeprecated, value.deprecationReason) | ||
this.deprecationAttributes(value.isDeprecated, value.deprecationReason); | ||
this.printOnNewline( | ||
@@ -650,0 +705,0 @@ `case ${escapeIdentifierIfNeeded(this.helpers.enumCaseName(value.name))} = "${value.value}"` |
@@ -14,3 +14,3 @@ import { | ||
getNamedType, | ||
GraphQLInputField, | ||
GraphQLInputField | ||
} from 'graphql'; | ||
@@ -25,3 +25,5 @@ | ||
import { CompilerOptions, SelectionSet, Field, FragmentSpread, Argument } from '../compiler'; | ||
import { isMetaFieldName } from "../utilities/graphql"; | ||
import { isMetaFieldName } from '../utilities/graphql'; | ||
import { Variant } from '../compiler/visitors/typeCase'; | ||
import { collectAndMergeFields } from '../compiler/visitors/collectAndMergeFields'; | ||
@@ -139,14 +141,16 @@ const builtInScalarMap = { | ||
propertyFromVariant(variant: SelectionSet): { selectionSet: SelectionSet } & Property & Struct { | ||
propertyFromVariant(variant: Variant): Variant & Property & Struct { | ||
const structName = this.structNameForVariant(variant); | ||
return { | ||
return Object.assign(variant, { | ||
propertyName: camelCase(structName), | ||
typeName: structName + '?', | ||
structName, | ||
selectionSet: variant | ||
}; | ||
structName | ||
}); | ||
} | ||
propertyFromFragmentSpread(fragmentSpread: FragmentSpread, isConditional: boolean): FragmentSpread & Property & Struct { | ||
propertyFromFragmentSpread( | ||
fragmentSpread: FragmentSpread, | ||
isConditional: boolean | ||
): FragmentSpread & Property & Struct { | ||
const structName = this.structNameForFragmentName(fragmentSpread.fragmentName); | ||
@@ -170,2 +174,22 @@ | ||
propertiesForSelectionSet( | ||
selectionSet: SelectionSet, | ||
namespace?: string | ||
): (Field & Property)[] | undefined { | ||
const properties = collectAndMergeFields(selectionSet, true) | ||
.filter(field => field.name !== '__typename') | ||
.map(field => this.propertyFromField(field, namespace)); | ||
// If we're not merging in fields from fragment spreads, there is no guarantee there will a generated | ||
// type for a composite field, so to avoid compiler errors we skip the initializer for now. | ||
if ( | ||
selectionSet.selections.some(selection => selection.kind === 'FragmentSpread') && | ||
properties.some(property => isCompositeType(getNamedType(property.type))) | ||
) { | ||
return undefined; | ||
} | ||
return properties; | ||
} | ||
// Expressions | ||
@@ -207,3 +231,7 @@ | ||
mapExpressionForType(type: GraphQLType, expression: (identifier: string) => string, identifier = ''): string { | ||
mapExpressionForType( | ||
type: GraphQLType, | ||
expression: (identifier: string) => string, | ||
identifier = '' | ||
): string { | ||
let isOptional; | ||
@@ -219,3 +247,7 @@ if (type instanceof GraphQLNonNull) { | ||
if (isOptional) { | ||
return `${identifier}.flatMap { $0.map { ${this.mapExpressionForType(type.ofType, expression, '$0')} } }`; | ||
return `${identifier}.flatMap { $0.map { ${this.mapExpressionForType( | ||
type.ofType, | ||
expression, | ||
'$0' | ||
)} } }`; | ||
} else { | ||
@@ -222,0 +254,0 @@ return `${identifier}.map { ${this.mapExpressionForType(type.ofType, expression, '$0')} }`; |
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
447789
7839