Skip to content

Commit

Permalink
fix: aws-amplify#3438, many-to-many with conflict resolution generate…
Browse files Browse the repository at this point in the history
…d wrong schema (aws-amplify#4171)
  • Loading branch information
Attila Hajdrik authored May 6, 2020
1 parent 0af90d6 commit 9e8606c
Show file tree
Hide file tree
Showing 6 changed files with 976 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
InputObjectTypeDefinitionNode,
InputValueDefinitionNode,
} from 'graphql';
import { GraphQLTransform } from 'graphql-transformer-core';
import { GraphQLTransform, ConflictHandlerType, TRANSFORM_CURRENT_VERSION } from 'graphql-transformer-core';
import { ResolverResourceIDs } from 'graphql-transformer-common';
import { ModelConnectionTransformer } from '../ModelConnectionTransformer';
import { DynamoDBModelTransformer } from 'graphql-dynamodb-transformer';
Expand Down Expand Up @@ -549,6 +549,92 @@ test('Test ModelConnectionTransformer for One-to-One getItem with composite sort
expect(relatedField.type.kind).toEqual(Kind.NAMED_TYPE);
});

test('Many-to-many without conflict resolution generates correct schema', () => {
const validSchema = `
type Post @model {
id: ID!
title: String!
editors: [PostEditor] @connection(keyName: "byPost", fields: ["id"])
}
# Create a join model and disable queries as you don't need them
# and can query through Post.editors and User.posts
type PostEditor
@model(queries: null)
@key(name: "byPost", fields: ["postID", "editorID"])
@key(name: "byEditor", fields: ["editorID", "postID"]) {
id: ID!
postID: ID!
editorID: ID!
post: Post! @connection(fields: ["postID"])
editor: User! @connection(fields: ["editorID"])
}
type User @model {
id: ID!
username: String!
posts: [PostEditor] @connection(keyName: "byEditor", fields: ["id"])
}
`;

const transformer = new GraphQLTransform({
transformers: [new DynamoDBModelTransformer(), new KeyTransformer(), new ModelConnectionTransformer()],
transformConfig: {
Version: TRANSFORM_CURRENT_VERSION,
},
});
const out = transformer.transform(validSchema);
expect(out).toBeDefined();

expect(out.schema).toMatchSnapshot();
});

test('Many-to-many with conflict resolution generates correct schema', () => {
const validSchema = `
type Post @model {
id: ID!
title: String!
editors: [PostEditor] @connection(keyName: "byPost", fields: ["id"])
}
# Create a join model and disable queries as you don't need them
# and can query through Post.editors and User.posts
type PostEditor
@model(queries: null)
@key(name: "byPost", fields: ["postID", "editorID"])
@key(name: "byEditor", fields: ["editorID", "postID"]) {
id: ID!
postID: ID!
editorID: ID!
post: Post! @connection(fields: ["postID"])
editor: User! @connection(fields: ["editorID"])
}
type User @model {
id: ID!
username: String!
posts: [PostEditor] @connection(keyName: "byEditor", fields: ["id"])
}
`;

const transformer = new GraphQLTransform({
transformers: [new DynamoDBModelTransformer(), new KeyTransformer(), new ModelConnectionTransformer()],
transformConfig: {
Version: TRANSFORM_CURRENT_VERSION,
ResolverConfig: {
project: {
ConflictHandler: ConflictHandlerType.AUTOMERGE,
ConflictDetection: 'VERSION',
},
},
},
});
const out = transformer.transform(validSchema);
expect(out).toBeDefined();

expect(out.schema).toMatchSnapshot();
});

function getInputType(doc: DocumentNode, type: string): InputObjectTypeDefinitionNode | undefined {
return doc.definitions.find((def: DefinitionNode) => def.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION && def.name.value === type) as
| InputObjectTypeDefinitionNode
Expand Down
Loading

0 comments on commit 9e8606c

Please sign in to comment.