File tree 3 files changed +47
-11
lines changed
packages/ai-constructs/src/conversation/runtime/event-tools-provider
3 files changed +47
-11
lines changed Original file line number Diff line number Diff line change
1
+ ---
2
+ ' @aws-amplify/ai-constructs ' : patch
3
+ ---
4
+
5
+ fix invalid graphql in tool query generation
Original file line number Diff line number Diff line change @@ -37,9 +37,7 @@ const testCases: Array<TestCase> = [
37
37
} ,
38
38
expectedQuery : `
39
39
query ToolQuery($property1: String!, $property2: Int) {
40
- testQueryName1(property1: $property1, property2: $property2) {
41
- testSelection1 testSelection2
42
- }
40
+ testQueryName1(property1: $property1, property2: $property2) { testSelection1 testSelection2 }
43
41
}
44
42
` ,
45
43
} ,
@@ -58,9 +56,30 @@ const testCases: Array<TestCase> = [
58
56
} ,
59
57
expectedQuery : `
60
58
query ToolQuery {
61
- testQueryName2 {
62
- testSelection3 testSelection4
63
- }
59
+ testQueryName2 { testSelection3 testSelection4 }
60
+ }
61
+ ` ,
62
+ } ,
63
+ {
64
+ toolDefinition : {
65
+ name : 'toolName3' ,
66
+ description : 'toolDescription3' ,
67
+ inputSchema : {
68
+ json : {
69
+ type : 'object' ,
70
+ properties : { } ,
71
+ required : [ ] ,
72
+ } ,
73
+ } ,
74
+ graphqlRequestInputDescriptor : {
75
+ queryName : 'testQueryName3' ,
76
+ selectionSet : '' ,
77
+ propertyTypes : { } ,
78
+ } ,
79
+ } ,
80
+ expectedQuery : `
81
+ query ToolQuery {
82
+ testQueryName3
64
83
}
65
84
` ,
66
85
} ,
Original file line number Diff line number Diff line change @@ -15,12 +15,11 @@ export class GraphQlQueryFactory {
15
15
const { graphqlRequestInputDescriptor } = toolDefinition ;
16
16
const { selectionSet, queryName } = graphqlRequestInputDescriptor ;
17
17
const [ topLevelQueryArgs , queryArgs ] = this . createQueryArgs ( toolDefinition ) ;
18
-
18
+ const fieldSelection =
19
+ selectionSet . length > 0 ? ` { ${ selectionSet } }` : '' ;
19
20
const query = `
20
21
query ToolQuery${ topLevelQueryArgs } {
21
- ${ queryName } ${ queryArgs } {
22
- ${ selectionSet }
23
- }
22
+ ${ queryName } ${ queryArgs } ${ fieldSelection }
24
23
}
25
24
` ;
26
25
@@ -36,7 +35,20 @@ export class GraphQlQueryFactory {
36
35
}
37
36
38
37
const { properties } = inputSchema . json as InputSchemaJson ;
39
- if ( ! properties ) {
38
+
39
+ // The conversation resolver should not pass an empty object as input,
40
+ // but we're defensively checking for it here anyway because if `properties: {}`
41
+ // is passed, it will generate invalid GraphQL. e.g.
42
+ // Valid:
43
+ // query ToolQuery {
44
+ // exampleQuery
45
+ // }
46
+ //
47
+ // Invalid:
48
+ // query ToolQuery {
49
+ // exampleQuery()
50
+ // }
51
+ if ( ! properties || Object . keys ( properties ) . length === 0 ) {
40
52
return [ '' , '' ] ;
41
53
}
42
54
const { propertyTypes } = toolDefinition . graphqlRequestInputDescriptor ;
You can’t perform that action at this time.
0 commit comments