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(graphql-model-transformer): fix model transformer ID generation when ID field is not specified #8633

Merged
merged 1 commit into from
Nov 3, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1056,4 +1056,37 @@ describe('ModelTransformer: ', () => {

validateModelSchema(parsed);
});

it('should generate the ID field when not specified', () => {
const validSchema = `type Todo @model {
name: String
}`;

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

const out = transformer.transform(validSchema);
expect(out).toBeDefined();

const definition = out.schema;
expect(definition).toBeDefined();

const parsed = parse(definition);
validateModelSchema(parsed);

const createTodoInput = getInputType(parsed, 'CreateTodoInput');
expect(createTodoInput).toBeDefined();

expectFieldsOnInputType(createTodoInput!, ['id', 'name']);

const idField = createTodoInput!.fields!.find(f => f.name.value === 'id');
expect((idField!.type as NamedTypeNode).name!.value).toEqual('ID');
expect((idField!.type as NamedTypeNode).kind).toEqual('NamedType');

const updateTodoInput = getInputType(parsed, 'UpdateTodoInput');
expect(updateTodoInput).toBeDefined();

expectFieldsOnInputType(updateTodoInput!, ['name']);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ export const makeUpdateInputField = (
input.fields.forEach(f => f.makeNullable());

// Add id field and make it optional
if (!hasIdField) {
input.addField(InputFieldWrapper.create('id', 'ID', false));
} else {
if (hasIdField) {
const idField = input.fields.find(f => f.name === 'id');
if (idField) {
idField.makeNonNullable();
Expand Down Expand Up @@ -127,7 +125,7 @@ export const makeCreateInputField = (

// Add id field and make it optional
if (!hasIdField) {
input.addField(InputFieldWrapper.create('id', 'ID'));
input.addField(InputFieldWrapper.create('id', 'ID', true));
} else {
const idField = input.fields.find(f => f.name === 'id');
if (idField) {
Expand Down