apollo-codegen
Advanced tools
Comparing version 0.17.0-alpha.2 to 0.17.0-alpha.3
@@ -13,3 +13,3 @@ "use strict"; | ||
if (options.addTypename) { | ||
document = graphql_2.withTypenameFieldAddedWhereNeeded(schema, document); | ||
document = graphql_2.withTypenameFieldAddedWhereNeeded(document); | ||
} | ||
@@ -16,0 +16,0 @@ const compiler = new Compiler(schema, options); |
@@ -59,5 +59,5 @@ "use strict"; | ||
const typeCase = new typeCase_1.TypeCase(selectionSet, this.options.mergeInFieldsFromFragmentSpreads); | ||
const fields = this.transformFieldsToLegacyIR(collectAndMergeFields_1.collectAndMergeFields(typeCase.default)); | ||
const fields = this.transformFieldsToLegacyIR(collectAndMergeFields_1.collectAndMergeFields(typeCase.default, false)); | ||
const inlineFragments = typeCase.variants.flatMap(variant => { | ||
const fields = this.transformFieldsToLegacyIR(collectAndMergeFields_1.collectAndMergeFields(variant)); | ||
const fields = this.transformFieldsToLegacyIR(collectAndMergeFields_1.collectAndMergeFields(variant, false)); | ||
if (selectionSet.possibleTypes.every(type => variant.possibleTypes.includes(type)) && | ||
@@ -64,0 +64,0 @@ fields.length < 1) |
@@ -16,3 +16,3 @@ "use strict"; | ||
const document = loading_1.loadAndMergeQueryDocuments(inputPaths, tagName); | ||
validation_1.validateQueryDocument(schema, document, target); | ||
validation_1.validateQueryDocument(schema, document); | ||
let output; | ||
@@ -19,0 +19,0 @@ if (target === 'swift') { |
@@ -231,3 +231,3 @@ "use strict"; | ||
`"__typename": "${typeCase.default.possibleTypes[0]}"`, | ||
...properties.map(({ responseKey, propertyName }) => `"${responseKey}": ${language_1.escapeIdentifierIfNeeded(propertyName)}`) | ||
...properties.map(this.propertyAssignmentForField, this) | ||
], ', ') || ':', `])`)); | ||
@@ -252,3 +252,3 @@ }); | ||
`"__typename": "${possibleType}"`, | ||
...properties.map(({ responseKey, propertyName }) => `"${responseKey}": ${language_1.escapeIdentifierIfNeeded(propertyName)}`) | ||
...properties.map(this.propertyAssignmentForField, this) | ||
], ', ') || ':', `])`)); | ||
@@ -260,2 +260,9 @@ }); | ||
} | ||
propertyAssignmentForField(field) { | ||
const { responseKey, propertyName, type } = field; | ||
const valueExpression = graphql_1.isCompositeType(graphql_1.getNamedType(type)) | ||
? this.helpers.mapExpressionForType(type, `$0.snapshot`, language_1.escapeIdentifierIfNeeded(propertyName)) | ||
: language_1.escapeIdentifierIfNeeded(propertyName); | ||
return `"${responseKey}": ${valueExpression}`; | ||
} | ||
propertyDeclarationForField(field) { | ||
@@ -266,2 +273,3 @@ const { responseKey, propertyName, typeName, type, isOptional } = field; | ||
this.comment(field.description); | ||
this.deprecationAttributes(field.isDeprecated, field.deprecationReason); | ||
this.printOnNewline(`public var ${propertyName}: ${typeName}`); | ||
@@ -454,2 +462,3 @@ this.withinBlock(() => { | ||
this.comment(value.description); | ||
this.deprecationAttributes(value.isDeprecated, value.deprecationReason); | ||
this.printOnNewline(`case ${language_1.escapeIdentifierIfNeeded(this.helpers.enumCaseName(value.name))} = "${value.value}"`); | ||
@@ -456,0 +465,0 @@ }); |
@@ -29,2 +29,6 @@ "use strict"; | ||
exports.escapeIdentifierIfNeeded = escapeIdentifierIfNeeded; | ||
function escapeQuotesIfNeeded(string) { | ||
return string.replace('"', '\"'); | ||
} | ||
exports.escapeQuotesIfNeeded = escapeQuotesIfNeeded; | ||
class SwiftGenerator extends CodeGenerator_1.default { | ||
@@ -47,2 +51,8 @@ constructor(context) { | ||
} | ||
deprecationAttributes(isDeprecated, deprecationReason) { | ||
if (isDeprecated !== undefined && isDeprecated) { | ||
deprecationReason = (deprecationReason !== undefined && deprecationReason.length > 0) ? deprecationReason : ""; | ||
this.printOnNewline(`@available(*, deprecated, message: "${escapeQuotesIfNeeded(deprecationReason)}")`); | ||
} | ||
} | ||
namespaceDeclaration(namespace, closure) { | ||
@@ -49,0 +59,0 @@ if (namespace) { |
@@ -18,22 +18,22 @@ "use strict"; | ||
const typenameField = { kind: graphql_1.Kind.FIELD, name: { kind: graphql_1.Kind.NAME, value: '__typename' } }; | ||
function withTypenameFieldAddedWhereNeeded(schema, ast) { | ||
function isOperationRootType(type) { | ||
return type === schema.getQueryType() || | ||
type === schema.getMutationType() || | ||
type === schema.getSubscriptionType(); | ||
} | ||
const typeInfo = new graphql_1.TypeInfo(schema); | ||
return graphql_1.visit(ast, graphql_1.visitWithTypeInfo(typeInfo, { | ||
leave: { | ||
SelectionSet: (node) => { | ||
const parentType = typeInfo.getParentType(); | ||
if (!isOperationRootType(parentType)) { | ||
return Object.assign({}, node, { selections: [typenameField, ...node.selections] }); | ||
} | ||
else { | ||
return undefined; | ||
} | ||
function withTypenameFieldAddedWhereNeeded(ast) { | ||
return graphql_1.visit(ast, { | ||
enter: { | ||
SelectionSet(node) { | ||
return Object.assign({}, node, { selections: node.selections.filter(selection => !(selection.kind === 'Field' && selection.name.value === '__typename')) }); | ||
} | ||
}, | ||
leave(node) { | ||
if (!(node.kind === 'Field' || node.kind === 'FragmentDefinition')) | ||
return undefined; | ||
if (!node.selectionSet) | ||
return undefined; | ||
if (true) { | ||
return Object.assign({}, node, { selectionSet: Object.assign({}, node.selectionSet, { selections: [typenameField, ...node.selectionSet.selections] }) }); | ||
} | ||
else { | ||
return undefined; | ||
} | ||
} | ||
})); | ||
}); | ||
} | ||
@@ -40,0 +40,0 @@ exports.withTypenameFieldAddedWhereNeeded = withTypenameFieldAddedWhereNeeded; |
@@ -5,8 +5,7 @@ "use strict"; | ||
const errors_1 = require("./errors"); | ||
function validateQueryDocument(schema, document, target) { | ||
const specifiedRulesToBeRemoved = [graphql_1.NoUnusedFragments]; | ||
function validateQueryDocument(schema, document) { | ||
const specifiedRulesToBeRemoved = [graphql_1.NoUnusedFragmentsRule]; | ||
const rules = [ | ||
NoAnonymousQueries, | ||
NoTypenameAlias, | ||
...(target === 'swift' ? [NoExplicitTypename] : []), | ||
...graphql_1.specifiedRules.filter(rule => !specifiedRulesToBeRemoved.includes(rule)) | ||
@@ -34,13 +33,2 @@ ]; | ||
exports.NoAnonymousQueries = NoAnonymousQueries; | ||
function NoExplicitTypename(context) { | ||
return { | ||
Field(node) { | ||
const fieldName = node.name.value; | ||
if (fieldName == '__typename') { | ||
context.reportError(new graphql_1.GraphQLError('Apollo inserts __typename automatically when needed, please do not include it explicitly', [node])); | ||
} | ||
} | ||
}; | ||
} | ||
exports.NoExplicitTypename = NoExplicitTypename; | ||
function NoTypenameAlias(context) { | ||
@@ -47,0 +35,0 @@ return { |
{ | ||
"name": "apollo-codegen", | ||
"version": "0.17.0-alpha.2", | ||
"version": "0.17.0-alpha.3", | ||
"description": "Generate API code or type annotations based on a GraphQL schema and query documents", | ||
@@ -27,3 +27,3 @@ "main": "./lib/index.js", | ||
"@types/glob": "^5.0.30", | ||
"@types/graphql": "^0.10.2", | ||
"@types/graphql": "^0.11.3", | ||
"@types/inflected": "^1.1.29", | ||
@@ -36,5 +36,5 @@ "@types/jest": "^20.0.6", | ||
"common-tags": "^1.4.0", | ||
"jest": "^20.0.4", | ||
"jest-matcher-utils": "^20.0.3", | ||
"typescript": "^2.4.2" | ||
"jest": "^21.1.0", | ||
"jest-matcher-utils": "^21.1.0", | ||
"typescript": "^2.5.2" | ||
}, | ||
@@ -45,3 +45,3 @@ "dependencies": { | ||
"glob": "^7.1.2", | ||
"graphql": "^0.10.5", | ||
"graphql": "^0.11.3", | ||
"inflected": "^2.0.2", | ||
@@ -48,0 +48,0 @@ "mkdirp": "^0.5.1", |
@@ -134,3 +134,3 @@ import { | ||
if (options.addTypename) { | ||
document = withTypenameFieldAddedWhereNeeded(schema, document); | ||
document = withTypenameFieldAddedWhereNeeded(document); | ||
} | ||
@@ -137,0 +137,0 @@ |
@@ -159,6 +159,6 @@ import { GraphQLSchema, GraphQLType, GraphQLObjectType, GraphQLCompositeType, DocumentNode } from 'graphql'; | ||
const fields: LegacyField[] = this.transformFieldsToLegacyIR(collectAndMergeFields(typeCase.default)); | ||
const fields: LegacyField[] = this.transformFieldsToLegacyIR(collectAndMergeFields(typeCase.default, false)); | ||
const inlineFragments: LegacyInlineFragment[] = typeCase.variants.flatMap(variant => { | ||
const fields = this.transformFieldsToLegacyIR(collectAndMergeFields(variant)); | ||
const fields = this.transformFieldsToLegacyIR(collectAndMergeFields(variant, false)); | ||
@@ -165,0 +165,0 @@ if ( |
@@ -27,3 +27,3 @@ import * as fs from 'fs'; | ||
validateQueryDocument(schema, document, target); | ||
validateQueryDocument(schema, document); | ||
@@ -30,0 +30,0 @@ let output; |
@@ -345,6 +345,3 @@ import { | ||
`"__typename": "${typeCase.default.possibleTypes[0]}"`, | ||
...properties.map( | ||
({ responseKey, propertyName }) => | ||
`"${responseKey}": ${escapeIdentifierIfNeeded(propertyName)}` | ||
) | ||
...properties.map(this.propertyAssignmentForField, this) | ||
], | ||
@@ -386,6 +383,3 @@ ', ' | ||
`"__typename": "${possibleType}"`, | ||
...properties.map( | ||
({ responseKey, propertyName }) => | ||
`"${responseKey}": ${escapeIdentifierIfNeeded(propertyName)}` | ||
) | ||
...properties.map(this.propertyAssignmentForField, this) | ||
], | ||
@@ -403,2 +397,10 @@ ', ' | ||
propertyAssignmentForField(field: Field & Property) { | ||
const { responseKey, propertyName, type } = field; | ||
const valueExpression = isCompositeType(getNamedType(type)) | ||
? this.helpers.mapExpressionForType(type, `$0.snapshot`, escapeIdentifierIfNeeded(propertyName)) | ||
: escapeIdentifierIfNeeded(propertyName); | ||
return `"${responseKey}": ${valueExpression}`; | ||
} | ||
propertyDeclarationForField(field: Field & Property) { | ||
@@ -412,2 +414,3 @@ const { responseKey, propertyName, typeName, type, isOptional } = field; | ||
this.comment(field.description); | ||
this.deprecationAttributes(field.isDeprecated, field.deprecationReason); | ||
@@ -636,2 +639,3 @@ this.printOnNewline(`public var ${propertyName}: ${typeName}`); | ||
this.comment(value.description); | ||
this.deprecationAttributes(value.isDeprecated, value.deprecationReason) | ||
this.printOnNewline( | ||
@@ -638,0 +642,0 @@ `case ${escapeIdentifierIfNeeded(this.helpers.enumCaseName(value.name))} = "${value.value}"` |
@@ -55,2 +55,6 @@ import CodeGenerator from '../utilities/CodeGenerator'; | ||
export function escapeQuotesIfNeeded(string: string) { | ||
return string.replace('"', '\"'); | ||
} | ||
export class SwiftGenerator<Context> extends CodeGenerator<Context, { typeName: string }> { | ||
@@ -76,2 +80,9 @@ constructor(context: Context) { | ||
deprecationAttributes(isDeprecated: boolean | undefined, deprecationReason: string | undefined) { | ||
if (isDeprecated !== undefined && isDeprecated) { | ||
deprecationReason = (deprecationReason !== undefined && deprecationReason.length > 0) ? deprecationReason : "" | ||
this.printOnNewline(`@available(*, deprecated, message: "${escapeQuotesIfNeeded(deprecationReason)}")`) | ||
} | ||
} | ||
namespaceDeclaration(namespace: string | undefined, closure: Function) { | ||
@@ -78,0 +89,0 @@ if (namespace) { |
import { | ||
visit, | ||
visitWithTypeInfo, | ||
Kind, | ||
TypeInfo, | ||
isEqualType, | ||
@@ -25,6 +23,6 @@ isAbstractType, | ||
ASTNode, | ||
SelectionSetNode, | ||
Location, | ||
ValueNode, | ||
OperationDefinitionNode, | ||
SelectionSetNode, | ||
FieldNode, | ||
@@ -52,24 +50,31 @@ GraphQLField, | ||
export function withTypenameFieldAddedWhereNeeded(schema: GraphQLSchema, ast: ASTNode) { | ||
function isOperationRootType(type: GraphQLType) { | ||
return type === schema.getQueryType() || | ||
type === schema.getMutationType() || | ||
type === schema.getSubscriptionType(); | ||
} | ||
export function withTypenameFieldAddedWhereNeeded(ast: ASTNode) { | ||
return visit(ast, { | ||
enter: { | ||
SelectionSet(node: SelectionSetNode) { | ||
return { | ||
...node, | ||
selections: node.selections.filter( | ||
selection => !(selection.kind === 'Field' && (selection as FieldNode).name.value === '__typename') | ||
) | ||
}; | ||
} | ||
}, | ||
leave(node: ASTNode) { | ||
if (!(node.kind === 'Field' || node.kind === 'FragmentDefinition')) return undefined; | ||
if (!node.selectionSet) return undefined; | ||
const typeInfo = new TypeInfo(schema); | ||
return visit(ast, visitWithTypeInfo(typeInfo, { | ||
leave: { | ||
SelectionSet: (node: SelectionSetNode) => { | ||
const parentType = typeInfo.getParentType(); | ||
if (!isOperationRootType(parentType)) { | ||
return { ...node, selections: [typenameField, ...node.selections] }; | ||
} else { | ||
return undefined; | ||
} | ||
if (true) { | ||
return { | ||
...node, | ||
selectionSet: { | ||
...node.selectionSet, | ||
selections: [typenameField, ...node.selectionSet.selections] | ||
} | ||
}; | ||
} else { | ||
return undefined; | ||
} | ||
} | ||
})); | ||
}); | ||
} | ||
@@ -76,0 +81,0 @@ |
import { | ||
validate, | ||
specifiedRules, | ||
NoUnusedFragments, | ||
NoUnusedFragmentsRule, | ||
GraphQLError, | ||
@@ -13,11 +13,6 @@ FieldNode, | ||
// FIXME: Submit a PR to add this to @types/graphql | ||
declare module 'graphql' { | ||
export function NoUnusedFragments(context: ValidationContext): any | ||
} | ||
import { ToolError, logError } from './errors'; | ||
export function validateQueryDocument(schema: GraphQLSchema, document: DocumentNode, target: string) { | ||
const specifiedRulesToBeRemoved = [NoUnusedFragments]; | ||
export function validateQueryDocument(schema: GraphQLSchema, document: DocumentNode) { | ||
const specifiedRulesToBeRemoved = [NoUnusedFragmentsRule]; | ||
@@ -27,3 +22,2 @@ const rules = [ | ||
NoTypenameAlias, | ||
...(target === 'swift' ? [NoExplicitTypename] : []), | ||
...specifiedRules.filter(rule => !specifiedRulesToBeRemoved.includes(rule)) | ||
@@ -52,18 +46,2 @@ ]; | ||
export function NoExplicitTypename(context: ValidationContext) { | ||
return { | ||
Field(node: FieldNode) { | ||
const fieldName = node.name.value; | ||
if (fieldName == '__typename') { | ||
context.reportError( | ||
new GraphQLError( | ||
'Apollo inserts __typename automatically when needed, please do not include it explicitly', | ||
[node] | ||
) | ||
); | ||
} | ||
} | ||
}; | ||
} | ||
export function NoTypenameAlias(context: ValidationContext) { | ||
@@ -70,0 +48,0 @@ return { |
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
436893
7649
+ Addedgraphql@0.11.7(transitive)
+ Addediterall@1.1.3(transitive)
- Removedgraphql@0.10.5(transitive)
- Removediterall@1.3.0(transitive)
Updatedgraphql@^0.11.3