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(ai-constructs): invalid graphql generation for query tools #1988

Merged
merged 3 commits into from
Sep 11, 2024
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
5 changes: 5 additions & 0 deletions .changeset/fuzzy-poems-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@aws-amplify/ai-constructs': patch
---

fix invalid graphql in tool query generation
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ const testCases: Array<TestCase> = [
},
expectedQuery: `
query ToolQuery($property1: String!, $property2: Int) {
testQueryName1(property1: $property1, property2: $property2) {
testSelection1 testSelection2
}
testQueryName1(property1: $property1, property2: $property2) { testSelection1 testSelection2 }
}
`,
},
Expand All @@ -58,9 +56,30 @@ const testCases: Array<TestCase> = [
},
expectedQuery: `
query ToolQuery {
testQueryName2 {
testSelection3 testSelection4
}
testQueryName2 { testSelection3 testSelection4 }
}
`,
},
{
toolDefinition: {
name: 'toolName3',
description: 'toolDescription3',
inputSchema: {
json: {
type: 'object',
properties: {},
required: [],
},
},
graphqlRequestInputDescriptor: {
queryName: 'testQueryName3',
selectionSet: '',
propertyTypes: {},
},
},
expectedQuery: `
query ToolQuery {
testQueryName3
}
`,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ export class GraphQlQueryFactory {
const { graphqlRequestInputDescriptor } = toolDefinition;
const { selectionSet, queryName } = graphqlRequestInputDescriptor;
const [topLevelQueryArgs, queryArgs] = this.createQueryArgs(toolDefinition);

const fieldSelection =
selectionSet.length > 0 ? ` { ${selectionSet} }` : '';
const query = `
query ToolQuery${topLevelQueryArgs} {
${queryName}${queryArgs} {
${selectionSet}
}
${queryName}${queryArgs}${fieldSelection}
}
`;

Expand All @@ -36,7 +35,20 @@ export class GraphQlQueryFactory {
}

const { properties } = inputSchema.json as InputSchemaJson;
if (!properties) {

// The conversation resolver should not pass an empty object as input,
// but we're defensively checking for it here anyway because if `properties: {}`
// is passed, it will generate invalid GraphQL. e.g.
// Valid:
// query ToolQuery {
// exampleQuery
// }
//
// Invalid:
// query ToolQuery {
// exampleQuery()
// }
if (!properties || Object.keys(properties).length === 0) {
return ['', ''];
}
const { propertyTypes } = toolDefinition.graphqlRequestInputDescriptor;
Expand Down