graphql-connection-transformer
Advanced tools
Comparing version 1.1.0-alpha.57358250 to 1.1.0-alpha.94252084
@@ -6,2 +6,21 @@ # Change Log | ||
<a name="1.0.29"></a> | ||
## [1.0.29](https://github.com/aws-amplify/amplify-cli/compare/graphql-connection-transformer@1.0.29-beta.0...graphql-connection-transformer@1.0.29) (2018-10-23) | ||
**Note:** Version bump only for package graphql-connection-transformer | ||
<a name="1.0.29-beta.0"></a> | ||
## [1.0.29-beta.0](https://github.com/aws-amplify/amplify-cli/compare/graphql-connection-transformer@1.0.12...graphql-connection-transformer@1.0.29-beta.0) (2018-10-23) | ||
### Bug Fixes | ||
* **graphql-connection-transformer:** Remove unused types ([87c3e0a](https://github.com/aws-amplify/amplify-cli/commit/87c3e0a)) | ||
<a name="1.0.28"></a> | ||
@@ -8,0 +27,0 @@ ## [1.0.28](https://github.com/aws-amplify/amplify-cli/compare/graphql-connection-transformer@1.0.28-beta.0...graphql-connection-transformer@1.0.28) (2018-10-18) |
@@ -248,2 +248,32 @@ "use strict"; | ||
}); | ||
test('Test ModelConnectionTransformer throws with invalid key fields', function () { | ||
var transformer = new graphql_transformer_core_1.default({ | ||
transformers: [ | ||
new graphql_appsync_transformer_1.default(), | ||
new graphql_dynamodb_transformer_1.default(), | ||
new ModelConnectionTransformer_1.ModelConnectionTransformer() | ||
] | ||
}); | ||
var invalidSchema = "\n type Post @model {\n id: ID!\n title: String!\n comments: [Comment] @connection(keyField: \"postId\")\n }\n type Comment @model {\n id: ID!\n content: String\n\n # Key fields must be String or ID.\n postId: [String]\n }\n "; | ||
expect(function () { return transformer.transform(invalidSchema); }).toThrow(); | ||
var invalidSchema2 = "\n type Post @model {\n id: ID!\n title: String!\n comments: [Comment] @connection(name: \"PostComments\", keyField: \"postId\")\n }\n type Comment @model {\n id: ID!\n content: String\n\n # Key fields must be String or ID.\n postId: [String]\n\n post: Post @connection(name: \"PostComments\", keyField: \"postId\")\n }\n "; | ||
expect(function () { return transformer.transform(invalidSchema2); }).toThrow(); | ||
var invalidSchema3 = "\n type Post @model {\n id: ID!\n title: String!\n }\n type Comment @model {\n id: ID!\n content: String\n\n # Key fields must be String or ID.\n postId: [String]\n\n post: Post @connection(keyField: \"postId\")\n }\n "; | ||
expect(function () { return transformer.transform(invalidSchema3); }).toThrow(); | ||
}); | ||
test('Test ModelConnectionTransformer does not throw with valid key fields', function () { | ||
var transformer = new graphql_transformer_core_1.default({ | ||
transformers: [ | ||
new graphql_appsync_transformer_1.default(), | ||
new graphql_dynamodb_transformer_1.default(), | ||
new ModelConnectionTransformer_1.ModelConnectionTransformer() | ||
] | ||
}); | ||
var validSchema = "\n type Post @model {\n id: ID!\n title: String!\n comments: [Comment] @connection(keyField: \"postId\")\n }\n type Comment @model {\n id: ID!\n content: String\n\n # Key fields must be String or ID.\n postId: String\n }\n "; | ||
expect(function () { return transformer.transform(validSchema); }).toBeTruthy(); | ||
var validSchema2 = "\n type Post @model {\n id: ID!\n title: String!\n comments: [Comment] @connection(name: \"PostComments\", keyField: \"postId\")\n }\n type Comment @model {\n id: ID!\n content: String\n\n # Key fields must be String or ID.\n postId: ID\n\n post: Post @connection(name: \"PostComments\", keyField: \"postId\")\n }\n "; | ||
expect(function () { return transformer.transform(validSchema2); }).toBeTruthy(); | ||
var validSchema3 = "\n type Post @model {\n id: ID!\n title: String!\n }\n type Comment @model {\n id: ID!\n content: String\n\n # Key fields must be String or ID.\n postId: String\n\n post: Post @connection(keyField: \"postId\")\n }\n "; | ||
expect(function () { return transformer.transform(validSchema3); }).toBeTruthy(); | ||
}); | ||
function expectFields(type, fields) { | ||
@@ -250,0 +280,0 @@ var _loop_1 = function (fieldName) { |
@@ -14,2 +14,8 @@ "use strict"; | ||
if (nonNull === void 0) { nonNull = false; } | ||
var keyFieldExists = Boolean(input.fields.find(function (f) { return f.name.value === connectionFieldName; })); | ||
// If the key field already exists then do not change the input. | ||
// The @connection field will validate that the key field is valid. | ||
if (keyFieldExists) { | ||
return input; | ||
} | ||
var updatedFields = input.fields.concat([ | ||
@@ -22,2 +28,8 @@ graphql_transformer_common_1.makeInputValueDefinition(connectionFieldName, nonNull ? graphql_transformer_common_1.makeNonNullType(graphql_transformer_common_1.makeNamedType('ID')) : graphql_transformer_common_1.makeNamedType('ID')) | ||
function updateUpdateInputWithConnectionField(input, connectionFieldName) { | ||
var keyFieldExists = Boolean(input.fields.find(function (f) { return f.name.value === connectionFieldName; })); | ||
// If the key field already exists then do not change the input. | ||
// The @connection field will validate that the key field is valid. | ||
if (keyFieldExists) { | ||
return input; | ||
} | ||
var updatedFields = input.fields.concat([ | ||
@@ -24,0 +36,0 @@ graphql_transformer_common_1.makeInputValueDefinition(connectionFieldName, graphql_transformer_common_1.makeNamedType('ID')) |
@@ -31,2 +31,15 @@ "use strict"; | ||
} | ||
function validateKeyField(field) { | ||
if (!field) { | ||
return; | ||
} | ||
var baseType = graphql_transformer_common_1.getBaseType(field.type); | ||
var isAList = graphql_transformer_common_1.isListType(field.type); | ||
// The only valid key fields are single String and ID fields. | ||
if ((baseType === 'ID' || baseType === 'String') && | ||
(!isAList)) { | ||
return; | ||
} | ||
throw new graphql_transformer_core_1.InvalidDirectiveError("If you define a field and specify it as a 'keyField', it must be of type 'ID' or 'String'."); | ||
} | ||
/** | ||
@@ -122,2 +135,5 @@ * The @connection transform. | ||
} | ||
// Validate the provided key field is legit. | ||
var existingKeyField = relatedType.fields.find(function (f) { return f.name.value === connectionAttributeName; }); | ||
validateKeyField(existingKeyField); | ||
var queryResolver = _this.resources.makeQueryConnectionResolver(parentTypeName, fieldName, relatedTypeName, connectionAttributeName, connectionName); | ||
@@ -139,2 +155,5 @@ ctx.setResource(graphql_transformer_common_2.ResolverResourceIDs.ResolverResourceID(parentTypeName, fieldName), queryResolver); | ||
} | ||
// Validate the provided key field is legit. | ||
var existingKeyField = parent.fields.find(function (f) { return f.name.value === connectionAttributeName; }); | ||
validateKeyField(existingKeyField); | ||
var tableLogicalId = graphql_transformer_common_2.ModelResourceIDs.ModelTableResourceID(parentTypeName); | ||
@@ -168,2 +187,5 @@ var table = ctx.getResource(tableLogicalId); | ||
} | ||
// Validate the provided key field is legit. | ||
var existingKeyField = relatedType.fields.find(function (f) { return f.name.value === connectionAttributeName; }); | ||
validateKeyField(existingKeyField); | ||
var tableLogicalId = graphql_transformer_common_2.ModelResourceIDs.ModelTableResourceID(relatedTypeName); | ||
@@ -197,2 +219,5 @@ var table = ctx.getResource(tableLogicalId); | ||
} | ||
// Validate the provided key field is legit. | ||
var existingKeyField = parent.fields.find(function (f) { return f.name.value === connectionAttributeName; }); | ||
validateKeyField(existingKeyField); | ||
var getResolver = _this.resources.makeGetItemConnectionResolver(parentTypeName, fieldName, relatedTypeName, connectionAttributeName); | ||
@@ -199,0 +224,0 @@ ctx.setResource(graphql_transformer_common_2.ResolverResourceIDs.ResolverResourceID(parentTypeName, fieldName), getResolver); |
@@ -92,3 +92,3 @@ "use strict"; | ||
key: graphql_mapping_template_1.obj({ | ||
id: graphql_mapping_template_1.ref("util.dynamodb.toDynamoDBJson($ctx.source." + connectionAttribute + ")") | ||
id: graphql_mapping_template_1.ref("util.dynamodb.toDynamoDBJson($util.defaultIfNullOrBlank($ctx.source." + connectionAttribute + ", \"" + graphql_transformer_common_1.NONE_VALUE + "\"))") | ||
}) | ||
@@ -95,0 +95,0 @@ })), |
{ | ||
"name": "graphql-connection-transformer", | ||
"version": "1.1.0-alpha.57358250", | ||
"version": "1.1.0-alpha.94252084", | ||
"description": "An AppSync model transform for connecting objects.", | ||
@@ -21,6 +21,6 @@ "main": "lib/index.js", | ||
"graphql": "^0.13.2", | ||
"graphql-dynamodb-transformer": "^1.1.0-alpha.57358250", | ||
"graphql-mapping-template": "^1.1.0-alpha.57358250", | ||
"graphql-transformer-common": "^1.1.0-alpha.57358250", | ||
"graphql-transformer-core": "^1.1.0-alpha.57358250" | ||
"graphql-dynamodb-transformer": "^1.1.0-alpha.94252084", | ||
"graphql-mapping-template": "^1.1.0-alpha.94252084", | ||
"graphql-transformer-common": "^1.1.0-alpha.94252084", | ||
"graphql-transformer-core": "^1.1.0-alpha.94252084" | ||
}, | ||
@@ -32,3 +32,3 @@ "devDependencies": { | ||
"aws-sdk": "^2.259.1", | ||
"graphql-appsync-transformer": "^1.1.0-alpha.57358250", | ||
"graphql-appsync-transformer": "^1.1.0-alpha.94252084", | ||
"jest": "^23.1.0", | ||
@@ -35,0 +35,0 @@ "ts-jest": "^22.4.6", |
@@ -6,3 +6,3 @@ import { | ||
} from 'graphql' | ||
import GraphQLTransform from 'graphql-transformer-core' | ||
import GraphQLTransform, { InvalidDirectiveError } from 'graphql-transformer-core' | ||
import { ResourceConstants, ResolverResourceIDs, ModelResourceIDs } from 'graphql-transformer-common' | ||
@@ -389,2 +389,124 @@ import DynamoDBModelTransformer from 'graphql-dynamodb-transformer' | ||
test('Test ModelConnectionTransformer throws with invalid key fields', () => { | ||
const transformer = new GraphQLTransform({ | ||
transformers: [ | ||
new AppSyncTransformer(), | ||
new DynamoDBModelTransformer(), | ||
new ModelConnectionTransformer() | ||
] | ||
}) | ||
const invalidSchema = ` | ||
type Post @model { | ||
id: ID! | ||
title: String! | ||
comments: [Comment] @connection(keyField: "postId") | ||
} | ||
type Comment @model { | ||
id: ID! | ||
content: String | ||
# Key fields must be String or ID. | ||
postId: [String] | ||
} | ||
` | ||
expect(() => transformer.transform(invalidSchema)).toThrow(); | ||
const invalidSchema2 = ` | ||
type Post @model { | ||
id: ID! | ||
title: String! | ||
comments: [Comment] @connection(name: "PostComments", keyField: "postId") | ||
} | ||
type Comment @model { | ||
id: ID! | ||
content: String | ||
# Key fields must be String or ID. | ||
postId: [String] | ||
post: Post @connection(name: "PostComments", keyField: "postId") | ||
} | ||
` | ||
expect(() => transformer.transform(invalidSchema2)).toThrow(); | ||
const invalidSchema3 = ` | ||
type Post @model { | ||
id: ID! | ||
title: String! | ||
} | ||
type Comment @model { | ||
id: ID! | ||
content: String | ||
# Key fields must be String or ID. | ||
postId: [String] | ||
post: Post @connection(keyField: "postId") | ||
} | ||
` | ||
expect(() => transformer.transform(invalidSchema3)).toThrow(); | ||
}) | ||
test('Test ModelConnectionTransformer does not throw with valid key fields', () => { | ||
const transformer = new GraphQLTransform({ | ||
transformers: [ | ||
new AppSyncTransformer(), | ||
new DynamoDBModelTransformer(), | ||
new ModelConnectionTransformer() | ||
] | ||
}) | ||
const validSchema = ` | ||
type Post @model { | ||
id: ID! | ||
title: String! | ||
comments: [Comment] @connection(keyField: "postId") | ||
} | ||
type Comment @model { | ||
id: ID! | ||
content: String | ||
# Key fields must be String or ID. | ||
postId: String | ||
} | ||
` | ||
expect(() => transformer.transform(validSchema)).toBeTruthy(); | ||
const validSchema2 = ` | ||
type Post @model { | ||
id: ID! | ||
title: String! | ||
comments: [Comment] @connection(name: "PostComments", keyField: "postId") | ||
} | ||
type Comment @model { | ||
id: ID! | ||
content: String | ||
# Key fields must be String or ID. | ||
postId: ID | ||
post: Post @connection(name: "PostComments", keyField: "postId") | ||
} | ||
` | ||
expect(() => transformer.transform(validSchema2)).toBeTruthy(); | ||
const validSchema3 = ` | ||
type Post @model { | ||
id: ID! | ||
title: String! | ||
} | ||
type Comment @model { | ||
id: ID! | ||
content: String | ||
# Key fields must be String or ID. | ||
postId: String | ||
post: Post @connection(keyField: "postId") | ||
} | ||
` | ||
expect(() => transformer.transform(validSchema3)).toBeTruthy(); | ||
}) | ||
function expectFields(type: ObjectTypeDefinitionNode, fields: string[]) { | ||
@@ -391,0 +513,0 @@ for (const fieldName of fields) { |
@@ -9,2 +9,8 @@ import { InputObjectTypeDefinitionNode } from 'graphql' | ||
): InputObjectTypeDefinitionNode { | ||
const keyFieldExists = Boolean(input.fields.find(f => f.name.value === connectionFieldName)) | ||
// If the key field already exists then do not change the input. | ||
// The @connection field will validate that the key field is valid. | ||
if (keyFieldExists) { | ||
return input; | ||
} | ||
const updatedFields = [ | ||
@@ -24,2 +30,8 @@ ...input.fields, | ||
): InputObjectTypeDefinitionNode { | ||
const keyFieldExists = Boolean(input.fields.find(f => f.name.value === connectionFieldName)) | ||
// If the key field already exists then do not change the input. | ||
// The @connection field will validate that the key field is valid. | ||
if (keyFieldExists) { | ||
return input; | ||
} | ||
const updatedFields = [ | ||
@@ -26,0 +38,0 @@ ...input.fields, |
@@ -28,2 +28,18 @@ import { Transformer, TransformerContext, InvalidDirectiveError } from 'graphql-transformer-core' | ||
function validateKeyField(field: FieldDefinitionNode): void { | ||
if (!field) { | ||
return | ||
} | ||
const baseType = getBaseType(field.type); | ||
const isAList = isListType(field.type) | ||
// The only valid key fields are single String and ID fields. | ||
if ( | ||
(baseType === 'ID' || baseType === 'String') && | ||
(!isAList) | ||
) { | ||
return; | ||
} | ||
throw new InvalidDirectiveError(`If you define a field and specify it as a 'keyField', it must be of type 'ID' or 'String'.`) | ||
} | ||
/** | ||
@@ -149,2 +165,6 @@ * The @connection transform. | ||
} | ||
// Validate the provided key field is legit. | ||
const existingKeyField = relatedType.fields.find(f => f.name.value === connectionAttributeName) | ||
validateKeyField(existingKeyField); | ||
const queryResolver = this.resources.makeQueryConnectionResolver( | ||
@@ -176,2 +196,6 @@ parentTypeName, | ||
} | ||
// Validate the provided key field is legit. | ||
const existingKeyField = parent.fields.find(f => f.name.value === connectionAttributeName) | ||
validateKeyField(existingKeyField); | ||
const tableLogicalId = ModelResourceIDs.ModelTableResourceID(parentTypeName) | ||
@@ -214,2 +238,6 @@ const table = ctx.getResource(tableLogicalId) as Table | ||
// Validate the provided key field is legit. | ||
const existingKeyField = relatedType.fields.find(f => f.name.value === connectionAttributeName) | ||
validateKeyField(existingKeyField); | ||
const tableLogicalId = ModelResourceIDs.ModelTableResourceID(relatedTypeName) | ||
@@ -251,2 +279,7 @@ const table = ctx.getResource(tableLogicalId) as Table | ||
} | ||
// Validate the provided key field is legit. | ||
const existingKeyField = parent.fields.find(f => f.name.value === connectionAttributeName) | ||
validateKeyField(existingKeyField); | ||
const getResolver = this.resources.makeGetItemConnectionResolver( | ||
@@ -253,0 +286,0 @@ parentTypeName, |
@@ -10,3 +10,3 @@ import Table, { GlobalSecondaryIndex, KeySchema, Projection, ProvisionedThroughput, AttributeDefinition } from 'cloudform/types/dynamoDb/table' | ||
} from 'graphql-mapping-template' | ||
import { ResourceConstants, ModelResourceIDs, DEFAULT_SCALARS } from 'graphql-transformer-common' | ||
import { ResourceConstants, ModelResourceIDs, DEFAULT_SCALARS, NONE_VALUE } from 'graphql-transformer-common' | ||
import { InvalidDirectiveError } from 'graphql-transformer-core'; | ||
@@ -110,3 +110,3 @@ | ||
key: obj({ | ||
id: ref(`util.dynamodb.toDynamoDBJson($ctx.source.${connectionAttribute})`) | ||
id: ref(`util.dynamodb.toDynamoDBJson($util.defaultIfNullOrBlank($ctx.source.${connectionAttribute}, "${NONE_VALUE}"))`) | ||
}) | ||
@@ -113,0 +113,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
Sorry, the diff of this file is not supported yet
141374
1917