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

build(deps): [security] bump npm from 6.13.1 to 6.13.4 #7

Closed
wants to merge 3 commits into from
Closed
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 @@ -100,6 +100,24 @@ describe('AppSyncModelVisitor', () => {
const visitor = new AppSyncModelVisitor(builtSchema, { directives, target: 'android', generate: CodeGenGenerateEnum.code }, {});
expect(() => visit(ast, { leave: visitor })).toThrowError();
});
it('should have id as the first field to ensure arity of constructors', () => {
const schema = /* GraphQL */ `
type Post @model {
title: String!
content: String!
id: ID!
}
`;
const ast = parse(schema);
const builtSchema = buildSchemaWithDirectives(schema);
const visitor = new AppSyncModelVisitor(builtSchema, { directives, target: 'android', generate: CodeGenGenerateEnum.code }, {});
visit(ast, { leave: visitor });
const postFields = visitor.types.Post.fields;
expect(postFields[0].name).toEqual('id');
expect(postFields[0].type).toEqual('ID');
expect(postFields[0].isNullable).toEqual(false);
expect(postFields[0].isList).toEqual(false);
});

describe(' 2 Way Connection', () => {
const schema = /* GraphQL */ `
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '@graphql-codegen/visitor-plugin-common';
import { constantCase, pascalCase } from 'change-case';
import { plural } from 'pluralize';
import * as crypto from 'crypto';
import crypto from 'crypto';
import {
DefinitionNode,
DirectiveNode,
Expand Down Expand Up @@ -196,6 +196,7 @@ export class AppSyncModelVisitor<
fields,
};
this.ensureIdField(model);
this.sortFields(model);
this.typeMap[node.name.value] = model;
}
}
Expand Down Expand Up @@ -364,6 +365,22 @@ export class AppSyncModelVisitor<
.toString('hex');
}

/**
* Sort the fields to ensure id is always the first field
* @param model
*/
protected sortFields(model: CodeGenModel) {
// sort has different behavior in node 10 and 11. Using reduce instead
model.fields = model.fields.reduce((acc, field) => {
if (field.name === 'id') {
acc.unshift(field);
} else {
acc.push(field);
}
return acc;
}, [] as CodeGenField[]);
}

protected ensureIdField(model: CodeGenModel) {
const idField = model.fields.find(field => field.name === 'id');
if (idField) {
Expand Down
Loading