Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: require @primaryKey sort fields to be non-nullable #8562

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test('throws if multiple primary keys are defined on an object', () => {
}).toThrow(`You may only supply one primary key on type 'Test'.`);
});

test('throws if primary key is nullable', () => {
test('throws if partition key is nullable', () => {
const schema = `
type Test @model {
id: ID @primaryKey
Expand All @@ -36,6 +36,22 @@ test('throws if primary key is nullable', () => {
}).toThrow(`The primary key on type 'Test' must reference non-null fields.`);
});

test('throws if sort key is nullable', () => {
const schema = `
type Test @model {
id: ID @primaryKey(sortKeyFields: ["email"])
email: String
}`;

const transformer = new GraphQLTransform({
transformers: [new ModelTransformer(), new PrimaryKeyTransformer()],
});

expect(() => {
transformer.transform(schema);
}).toThrow(`The primary key on type 'Test' must reference non-null fields.`);
});

test('throws if @primaryKey is used in a non-@model type', () => {
const schema = `
type Test {
Expand Down Expand Up @@ -213,7 +229,7 @@ test('a primary key with a composite sort key is properly configured', () => {
type Test @model {
email: String! @primaryKey(sortKeyFields: ["kind", "other"])
kind: Int!
other: AWSDateTime
other: AWSDateTime!
yetAnother: String
andAnother: String!
}`;
Expand Down Expand Up @@ -265,7 +281,7 @@ test('a primary key with a composite sort key is properly configured', () => {
expect(createInput).toBeDefined();
expect(createInput.fields.find((f: any) => f.name.value === 'email' && f.type.kind === Kind.NON_NULL_TYPE)).toBeDefined();
expect(createInput.fields.find((f: any) => f.name.value === 'kind' && f.type.kind === Kind.NON_NULL_TYPE)).toBeDefined();
expect(createInput.fields.find((f: any) => f.name.value === 'other' && f.type.kind === Kind.NAMED_TYPE)).toBeDefined();
expect(createInput.fields.find((f: any) => f.name.value === 'other' && f.type.kind === Kind.NON_NULL_TYPE)).toBeDefined();
expect(createInput.fields.find((f: any) => f.name.value === 'yetAnother' && f.type.kind === Kind.NAMED_TYPE)).toBeDefined();
expect(createInput.fields.find((f: any) => f.name.value === 'andAnother' && f.type.kind === Kind.NON_NULL_TYPE)).toBeDefined();
expect(createInput.fields.find((f: any) => f.name.value === 'id')).toBeUndefined();
Expand Down Expand Up @@ -427,7 +443,7 @@ test('resolvers can be renamed by @model', () => {
mutations: { create: "testCreate", delete: "testDelete", update: "testUpdate" }
) {
id: ID! @primaryKey(sortKeyFields: ["email"])
email: String
email: String!
}`;

const transformer = new GraphQLTransform({
Expand Down Expand Up @@ -471,7 +487,7 @@ test('individual resolvers can be made null by @model', () => {
const inputSchema = `
type Test @model(queries: { get: "testGet", list: null }) {
id: ID! @primaryKey(sortKeyFields: ["email"])
email: String
email: String!
}`;

const transformer = new GraphQLTransform({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@ function validate(config: PrimaryKeyDirectiveConfiguration, ctx: TransformerCont
);
}

if (!isNonNullType(sortField.type)) {
throw new InvalidDirectiveError(`The primary key on type '${object.name.value}' must reference non-null fields.`);
}

config.sortKey.push(sortField);
}
}