Skip to content

Commit

Permalink
add generation route mis types
Browse files Browse the repository at this point in the history
  • Loading branch information
atierian committed Aug 15, 2024
1 parent 48e373a commit 1c12ea7
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
},
"conversations": {
"$ref": "#/definitions/SchemaConversationRoutes"
},
"generations": {
"$ref": "#/definitions/SchemaQueries"
}
},
"required": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,32 @@ exports[`Conversation Route Introspection Visitor Metadata snapshot should gener
]
}
},
\\"nonModels\\": {},
\\"nonModels\\": {
\\"ContentBlock\\": {
\\"name\\": \\"ContentBlock\\",
\\"fields\\": {
\\"type\\": {
\\"name\\": \\"type\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": false,
\\"attributes\\": []
}
}
},
\\"ToolConfiguration\\": {
\\"name\\": \\"ToolConfiguration\\",
\\"fields\\": {
\\"type\\": {
\\"name\\": \\"type\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": false,
\\"attributes\\": []
}
}
}
},
\\"conversations\\": {
\\"pirateChat\\": {
\\"name\\": \\"pirateChat\\",
Expand Down Expand Up @@ -182,15 +207,6 @@ exports[`Conversation Route Introspection Visitor Metadata snapshot should gener
\\"attributes\\": [],
\\"isArrayNullable\\": true
},
\\"assistantContent\\": {
\\"name\\": \\"assistantContent\\",
\\"isArray\\": false,
\\"type\\": {
\\"nonModel\\": \\"ContentBlock\\"
},
\\"isRequired\\": false,
\\"attributes\\": []
},
\\"createdAt\\": {
\\"name\\": \\"createdAt\\",
\\"isArray\\": false,
Expand Down Expand Up @@ -278,8 +294,25 @@ exports[`Conversation Route Introspection Visitor Metadata snapshot should gener
},
\\"content\\": {
\\"name\\": \\"content\\",
\\"isArray\\": true,
\\"type\\": {
\\"input\\": \\"ContentBlockInput\\"
},
\\"isRequired\\": false,
\\"isArrayNullable\\": true
},
\\"aiContext\\": {
\\"name\\": \\"aiContext\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"type\\": \\"AWSJSON\\",
\\"isRequired\\": false
},
\\"toolConfiguration\\": {
\\"name\\": \\"toolConfiguration\\",
\\"isArray\\": false,
\\"type\\": {
\\"input\\": \\"ToolConfigurationInput\\"
},
\\"isRequired\\": false
}
}
Expand All @@ -302,6 +335,30 @@ exports[`Conversation Route Introspection Visitor Metadata snapshot should gener
}
}
}
},
\\"inputs\\": {
\\"ContentBlockInput\\": {
\\"name\\": \\"ContentBlockInput\\",
\\"attributes\\": {
\\"type\\": {
\\"name\\": \\"type\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": false
}
}
},
\\"ToolConfigurationInput\\": {
\\"name\\": \\"ToolConfigurationInput\\",
\\"attributes\\": {
\\"type\\": {
\\"name\\": \\"type\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": false
}
}
}
}
}"
`;
Expand Down Expand Up @@ -2282,6 +2339,61 @@ exports[`Custom queries/mutations/subscriptions & input type tests should genera
}"
`;

exports[`Generation Route Introspection Visitor Metadata snapshot should generate correct model intropection file validated by JSON schema 1`] = `
"{
\\"version\\": 1,
\\"models\\": {},
\\"enums\\": {},
\\"nonModels\\": {
\\"Recipe\\": {
\\"name\\": \\"Recipe\\",
\\"fields\\": {
\\"name\\": {
\\"name\\": \\"name\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": false,
\\"attributes\\": []
},
\\"ingredients\\": {
\\"name\\": \\"ingredients\\",
\\"isArray\\": true,
\\"type\\": \\"String\\",
\\"isRequired\\": false,
\\"attributes\\": [],
\\"isArrayNullable\\": true
},
\\"instructions\\": {
\\"name\\": \\"instructions\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": false,
\\"attributes\\": []
}
}
}
},
\\"generations\\": {
\\"generateRecipe\\": {
\\"name\\": \\"generateRecipe\\",
\\"isArray\\": false,
\\"type\\": {
\\"nonModel\\": \\"Recipe\\"
},
\\"isRequired\\": false,
\\"arguments\\": {
\\"description\\": {
\\"name\\": \\"description\\",
\\"isArray\\": false,
\\"type\\": \\"String\\",
\\"isRequired\\": false
}
}
}
}
}"
`;

exports[`Model Introspection Visitor Metadata snapshot should generate correct model intropection file validated by JSON schema 1`] = `
"{
\\"version\\": 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,58 @@ const getVisitor = (schema: string, settings: any = {}, directives: readonly Dir
return visitor;
};

describe('Generation Route Introspection Visitor', () => {
const schema = /* GraphQL */ `
type Recipe {
name: String
ingredients: [String]
instructions: String
}
type Query {
generateRecipe(description: String): Recipe
@generation(aiModel: "Claude3Haiku", systemPrompt: "You are a recipe generator.")
}
`;
// TODO: Update to amplify-graphql-directives version that includes generation directive
const generationDirective: Directive = {
name: 'generation',
definition: /* GraphQL */ `
directive @generation(
aiModel: String,
systemPrompt: String
) on FIELD_DEFINITION
`,
defaults: {},
}
const visitor: AppSyncModelIntrospectionVisitor = getVisitor(schema, {}, [...DefaultDirectives, generationDirective]);
describe('Metadata snapshot', () => {
it('should generate correct model intropection file validated by JSON schema', () => {
expect(visitor.generate()).toMatchSnapshot();
});
});
});

describe('Conversation Route Introspection Visitor', () => {
const schema = /* GraphQL */ `
enum ConversationParticipantRole {
user
assistant
}
type ContentBlock {
type: String
}
type ToolConfiguration {
type: String
}
input ContentBlockInput {
type: String
}
input ToolConfigurationInput {
type: String
}
interface ConversationMessage {
id: ID!
conversationId: ID!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
subscriptions?: SchemaSubscriptions;
inputs?: SchemaInputs;
conversations?: SchemaConversationRoutes;
generations?: SchemaQueries;
};
/**
* Top-level Entities on a Schema
Expand Down
2 changes: 1 addition & 1 deletion packages/appsync-modelgen-plugin/src/validate-cjs.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,17 @@ export class AppSyncModelIntrospectionVisitor<
// Skip the field if the field type is union/interface
// TODO: Remove this skip once these types are supported for stakeholder usages
const fieldType = this.getType(queryObj.type) as any;
if (this.isUnionFieldType(fieldType) || this.isInterfaceFieldType(fieldType)) {
if (this.isUnionFieldType(fieldType) || this.isInterfaceFieldType(fieldType) || queryObj.directives.find((directive) => directive.name === 'generation')) {
return acc;
}
return { ...acc, [queryObj.name]: this.generateGraphQLOperationMetadata<CodeGenQuery, SchemaQuery>(queryObj) };
}, {})
const generations = Object.values(this.queryMap).reduce((acc, queryObj: CodeGenQuery) => {
if (!queryObj.directives.find((directive) => directive.name === 'generation')) {
return acc;
}
return { ...acc, [queryObj.name]: this.generateGenerationMetadata(queryObj) };
}, {});
const mutations = Object.values(this.mutationMap).reduce((acc, mutationObj: CodeGenMutation) => {
// Skip the field if the field type is union/interface
// TODO: Remove this skip once these types are supported for stakeholder usages
Expand Down Expand Up @@ -109,6 +115,9 @@ export class AppSyncModelIntrospectionVisitor<
if (Object.keys(conversations).length > 0) {
result = { ...result, conversations }
}
if (Object.keys(generations).length > 0) {
result = { ...result, generations };
}
if (Object.keys(subscriptions).length > 0) {
result = { ...result, subscriptions };
}
Expand Down Expand Up @@ -247,6 +256,10 @@ export class AppSyncModelIntrospectionVisitor<
return operationMeta as V;
}

private generateGenerationMetadata(generationObj: CodeGenQuery): SchemaQuery {
return this.generateGraphQLOperationMetadata<CodeGenQuery, SchemaQuery>(generationObj);
}

private generateConversationMetadata(mutationObj: CodeGenMutation): SchemaConversationRoute {
const routeName = pascalCase(mutationObj.name)
const conversationModelName = `Conversation${routeName}`;
Expand Down

0 comments on commit 1c12ea7

Please sign in to comment.