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

Conversation

atierian
Copy link
Member

Problem

1. Data tool definitions with inputSchema.properties: {} generates invalid GraphQL.

This is a test we have today, which works as expected.

  {
    toolDefinition: {
      name: 'toolName2',
      description: 'toolDescription2',
      inputSchema: {
        json: {},
      },
      graphqlRequestInputDescriptor: {
        queryName: 'testQueryName2',
        selectionSet: 'testSelection3 testSelection4',
        propertyTypes: {},
      },
    },
    expectedQuery: `
    query ToolQuery {
      testQueryName2 {
        testSelection3 testSelection4
      }
    }
    `,
  },

The inputSchema: { json: {} } is what the resolver should be passing the lambda if there aren’t any arguments. But apparently in some cases. we’re sending this (which is also a valid tool definition):

toolDefinition: {
  name: 'toolName3',
  description: 'toolDescription3',
  inputSchema: {
    json: {
      type: 'object',
      properties: {},
      required: [],
    },
  },
},

I'll make the fix in the transformer to ensure we send json: {} for input-less queries, but let's still be defensive about checking for the second case in the lambda.

2. Scalar return types in tools generates invalid GraphQL selection set ({ } )

A scalar return type shouldn’t have a selection set. e.g.

graphqlRequestInputDescriptor: {
  queryName: 'testQueryName3',
  selectionSet: '',
  propertyTypes: {},
},

should generate

query ToolQuery {
  testQueryName3
}

but it currently generates

query ToolQuery {
  testQueryName3 { }
}

which is invalid GraphQL.

That’s because we wrap the provided selection set in { } here.

Issue number, if available:

Changes

  1. Adds check for Object.keys(properties).length === 0) to catch inputSchema.json.properties: {} case.

  2. Conditionally wraps provided selection set in { } only if the selection set is not empty.

Corresponding docs PR, if applicable:

Validation

Checklist

  • If this PR includes a functional change to the runtime behavior of the code, I have added or updated automated test coverage for this change.
  • If this PR requires a change to the Project Architecture README, I have included that update in this PR.
  • If this PR requires a docs update, I have linked to that docs PR above.
  • If this PR modifies E2E tests, makes changes to resource provisioning, or makes SDK calls, I have run the PR checks with the run-e2e label set.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@atierian atierian requested a review from a team as a code owner September 11, 2024 13:57
Copy link

changeset-bot bot commented Sep 11, 2024

🦋 Changeset detected

Latest commit: cf82bd2

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@aws-amplify/ai-constructs Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@atierian atierian changed the title fix(ai-constructs): fix(ai-constructs): invalid graphql generation for query tools Sep 11, 2024
@sobolk sobolk merged commit 8e964e1 into main Sep 11, 2024
37 checks passed
@sobolk sobolk deleted the fix-invalid-graphql-query-tool branch September 11, 2024 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants