Skip to content

Commit

Permalink
fix(graphql-model-transformer): fixed model transformer ID generation…
Browse files Browse the repository at this point in the history
… when ID field is not specified (#8633)
  • Loading branch information
lazpavel committed Nov 3, 2021
1 parent 5a76b3a commit b515d16
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
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']);
});
});
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

0 comments on commit b515d16

Please sign in to comment.