Skip to content

fix: Fix handling of empty args #251

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions src/schema-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,9 @@ export class Field extends GraphqlType implements IField {
*/
public argsToString(): string {
if (!this.fieldOptions || !this.fieldOptions.args) { return ''; }
return Object.keys(this.fieldOptions.args).reduce((acc, key) =>
`${acc}${key}: ${this.fieldOptions?.args?.[key].toString()} `, '(').slice(0, -1) + ')';
const argString = Object.keys(this.fieldOptions.args).reduce((acc, key) =>
`${acc}${key}: ${this.fieldOptions?.args?.[key].toString()} `, '(').slice(0, -1);
return argString === '' ? argString : argString + ')';
}

/**
Expand Down
37 changes: 37 additions & 0 deletions test/appsync-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,43 @@ describe('basic testing schema definition mode `code`', () => {
});
});

test('definition mode `code` allows for api to addQuery with empty args', () => {
// WHEN
const schema = new CodeFirstSchema();
new appsync.GraphqlApi(stack, 'API', {
name: 'demo',
schema,
});
schema.addQuery('test', new ResolvableField({
returnType: t.string,
args: {},
}));

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::AppSync::GraphQLSchema', {
Definition: 'schema {\n query: Query\n}\ntype Query {\n test: String\n}\n',
});
});

test('definition mode `code` allows for api to addQuery with args', () => {
// WHEN
const schema = new CodeFirstSchema();
new appsync.GraphqlApi(stack, 'API', {
name: 'demo',
schema,
});
schema.addQuery('test', new ResolvableField({
returnType: t.string,
args: { id: t.string },
}));

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::AppSync::GraphQLSchema', {
Definition: 'schema {\n query: Query\n}\ntype Query {\n test(id: String): String\n}\n',
});
});


test('definition mode `code` allows for schema to addMutation', () => {
// WHEN
const schema = new CodeFirstSchema();
Expand Down