@auto-engineer/server-generator-apollo-emmett 1.45.3 → 1.47.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -833,4 +833,250 @@ describe('query.resolver.ts.ejs', () => {
833
833
  expect(resolverFile?.contents).toContain('sets!: { reps: number; weight: number }[];');
834
834
  expect(resolverFile?.contents).toContain('@Field(() => [GraphQLJSON])');
835
835
  });
836
+
837
+ it('should use message type name in graphqlType and not treat it as enum', async () => {
838
+ const spec: SpecsSchema = {
839
+ variant: 'specs',
840
+ narratives: [
841
+ {
842
+ name: 'recipe-flow',
843
+ slices: [
844
+ {
845
+ type: 'query',
846
+ name: 'find-recipe-matches',
847
+ request: `
848
+ query FindRecipeMatches($pantryId: ID!) {
849
+ recipeMatches(pantryId: $pantryId) {
850
+ recipeName
851
+ missingIngredients {
852
+ name
853
+ amount
854
+ }
855
+ }
856
+ }
857
+ `,
858
+ client: { specs: [] },
859
+ server: {
860
+ description: '',
861
+ data: {
862
+ items: [
863
+ {
864
+ origin: {
865
+ type: 'projection',
866
+ idField: 'pantryId',
867
+ name: 'RecipeMatchesProjection',
868
+ },
869
+ target: {
870
+ type: 'State',
871
+ name: 'RecipeMatchesRecipes',
872
+ },
873
+ },
874
+ ],
875
+ },
876
+ specs: [],
877
+ },
878
+ },
879
+ ],
880
+ },
881
+ ],
882
+ messages: [
883
+ {
884
+ type: 'state',
885
+ name: 'Ingredient',
886
+ fields: [
887
+ { name: 'name', type: 'string' },
888
+ { name: 'amount', type: 'number' },
889
+ ],
890
+ },
891
+ {
892
+ type: 'state',
893
+ name: 'RecipeMatchesRecipes',
894
+ fields: [
895
+ { name: 'recipeName', type: 'string', required: true },
896
+ { name: 'missingIngredients', type: 'Array<Ingredient>', required: true },
897
+ ],
898
+ },
899
+ ],
900
+ };
901
+
902
+ const plans = await generateScaffoldFilePlans(spec.narratives, spec.messages, undefined, 'src/domain/flows');
903
+ const resolverFile = plans.find((p) => p.outputPath.endsWith('query.resolver.ts'));
904
+
905
+ expect(resolverFile?.contents).toMatchInlineSnapshot(`
906
+ "import { Query, Resolver, Arg, Ctx, ObjectType, Field, ID, Float } from 'type-graphql';
907
+ import { type GraphQLContext, ReadModel } from '../../../shared';
908
+
909
+ @ObjectType()
910
+ export class Ingredient {
911
+ @Field(() => String)
912
+ name!: string;
913
+
914
+ @Field(() => Float)
915
+ amount!: number;
916
+ }
917
+
918
+ @ObjectType()
919
+ export class RecipeMatchesRecipes {
920
+ @Field(() => String)
921
+ recipeName!: string;
922
+
923
+ @Field(() => [Ingredient])
924
+ missingIngredients!: Ingredient[];
925
+
926
+ // IMPORTANT: Index signature required for ReadModel<T extends Record<string, unknown>> compatibility
927
+ [key: string]: unknown;
928
+ }
929
+
930
+ @Resolver()
931
+ export class FindRecipeMatchesQueryResolver {
932
+ @Query(() => [RecipeMatchesRecipes])
933
+ async recipeMatches(
934
+ @Ctx() ctx: GraphQLContext,
935
+ @Arg('pantryId', () => ID, { nullable: true }) pantryId?: string,
936
+ ): Promise<RecipeMatchesRecipes[]> {
937
+ const model = new ReadModel<RecipeMatchesRecipes>(ctx.database, 'RecipeMatchesProjection');
938
+
939
+ // ## IMPLEMENTATION INSTRUCTIONS ##
940
+ // You can query the projection using the ReadModel API:
941
+ // - model.getAll() — fetch all documents
942
+ // - model.getById(id) — fetch a single document by ID (default key: 'id')
943
+ // - model.find(filterFn) — filter documents using a predicate
944
+ // - model.first(filterFn) — fetch the first document matching a predicate
945
+ //
946
+ // Example below uses \\\`.find()\\\` to filter
947
+ // change the logic for the query as needed to meet the requirements for the current slice.
948
+
949
+ return model.find((item) => {
950
+ if (pantryId !== undefined && item.pantryId !== pantryId) return false;
951
+
952
+ return true;
953
+ });
954
+ }
955
+ }
956
+ "
957
+ `);
958
+ });
959
+
960
+ it('should generate @ObjectType classes for referenced message types', async () => {
961
+ const spec: SpecsSchema = {
962
+ variant: 'specs',
963
+ narratives: [
964
+ {
965
+ name: 'recipe-flow',
966
+ slices: [
967
+ {
968
+ type: 'query',
969
+ name: 'find-recipe-matches',
970
+ request: `
971
+ query FindRecipeMatches($pantryId: ID!) {
972
+ recipeMatches(pantryId: $pantryId) {
973
+ recipeName
974
+ missingIngredients {
975
+ name
976
+ amount
977
+ }
978
+ }
979
+ }
980
+ `,
981
+ client: { specs: [] },
982
+ server: {
983
+ description: '',
984
+ data: {
985
+ items: [
986
+ {
987
+ origin: {
988
+ type: 'projection',
989
+ idField: 'pantryId',
990
+ name: 'RecipeMatchesProjection',
991
+ },
992
+ target: {
993
+ type: 'State',
994
+ name: 'RecipeMatchesRecipes',
995
+ },
996
+ },
997
+ ],
998
+ },
999
+ specs: [],
1000
+ },
1001
+ },
1002
+ ],
1003
+ },
1004
+ ],
1005
+ messages: [
1006
+ {
1007
+ type: 'state',
1008
+ name: 'Ingredient',
1009
+ fields: [
1010
+ { name: 'name', type: 'string' },
1011
+ { name: 'amount', type: 'number' },
1012
+ ],
1013
+ },
1014
+ {
1015
+ type: 'state',
1016
+ name: 'RecipeMatchesRecipes',
1017
+ fields: [
1018
+ { name: 'recipeName', type: 'string', required: true },
1019
+ { name: 'missingIngredients', type: 'Array<Ingredient>', required: true },
1020
+ ],
1021
+ },
1022
+ ],
1023
+ };
1024
+
1025
+ const plans = await generateScaffoldFilePlans(spec.narratives, spec.messages, undefined, 'src/domain/flows');
1026
+ const resolverFile = plans.find((p) => p.outputPath.endsWith('query.resolver.ts'));
1027
+
1028
+ expect(resolverFile?.contents).toMatchInlineSnapshot(`
1029
+ "import { Query, Resolver, Arg, Ctx, ObjectType, Field, ID, Float } from 'type-graphql';
1030
+ import { type GraphQLContext, ReadModel } from '../../../shared';
1031
+
1032
+ @ObjectType()
1033
+ export class Ingredient {
1034
+ @Field(() => String)
1035
+ name!: string;
1036
+
1037
+ @Field(() => Float)
1038
+ amount!: number;
1039
+ }
1040
+
1041
+ @ObjectType()
1042
+ export class RecipeMatchesRecipes {
1043
+ @Field(() => String)
1044
+ recipeName!: string;
1045
+
1046
+ @Field(() => [Ingredient])
1047
+ missingIngredients!: Ingredient[];
1048
+
1049
+ // IMPORTANT: Index signature required for ReadModel<T extends Record<string, unknown>> compatibility
1050
+ [key: string]: unknown;
1051
+ }
1052
+
1053
+ @Resolver()
1054
+ export class FindRecipeMatchesQueryResolver {
1055
+ @Query(() => [RecipeMatchesRecipes])
1056
+ async recipeMatches(
1057
+ @Ctx() ctx: GraphQLContext,
1058
+ @Arg('pantryId', () => ID, { nullable: true }) pantryId?: string,
1059
+ ): Promise<RecipeMatchesRecipes[]> {
1060
+ const model = new ReadModel<RecipeMatchesRecipes>(ctx.database, 'RecipeMatchesProjection');
1061
+
1062
+ // ## IMPLEMENTATION INSTRUCTIONS ##
1063
+ // You can query the projection using the ReadModel API:
1064
+ // - model.getAll() — fetch all documents
1065
+ // - model.getById(id) — fetch a single document by ID (default key: 'id')
1066
+ // - model.find(filterFn) — filter documents using a predicate
1067
+ // - model.first(filterFn) — fetch the first document matching a predicate
1068
+ //
1069
+ // Example below uses \\\`.find()\\\` to filter
1070
+ // change the logic for the query as needed to meet the requirements for the current slice.
1071
+
1072
+ return model.find((item) => {
1073
+ if (pantryId !== undefined && item.pantryId !== pantryId) return false;
1074
+
1075
+ return true;
1076
+ });
1077
+ }
1078
+ }
1079
+ "
1080
+ `);
1081
+ });
836
1082
  });
@@ -10,14 +10,16 @@ const resolverClassName = `${pascalCase(slice.name)}QueryResolver`;
10
10
  const usesID = parsedRequest?.args?.some(arg => graphqlType(arg.tsType) === 'ID');
11
11
 
12
12
  const messageFields = message?.fields ?? [];
13
- const usesDate = messageFields.some(f => fieldUsesDate(f.type)) ||
13
+ const refFields = (referencedTypes ?? []).flatMap(rt => rt.fields ?? []);
14
+ const usesDate = [...messageFields, ...refFields].some(f => fieldUsesDate(f.type)) ||
14
15
  (parsedRequest?.args ?? []).some(a => fieldUsesDate(a.tsType));
15
- const usesJSON = messageFields.some(f => fieldUsesJSON(f.type)) ||
16
+ const usesJSON = [...messageFields, ...refFields].some(f => fieldUsesJSON(f.type)) ||
16
17
  (parsedRequest?.args ?? []).some(a => fieldUsesJSON(a.tsType));
17
- const usesFloat = messageFields.some(f => fieldUsesFloat(f.type)) ||
18
+ const usesFloat = [...messageFields, ...refFields].some(f => fieldUsesFloat(f.type)) ||
18
19
  (parsedRequest?.args ?? []).some(a => fieldUsesFloat(a.tsType));
19
20
 
20
- const enumList = collectEnumNames([...messageFields, ...(parsedRequest?.args ?? [])]);
21
+ const enumList = collectEnumNames([...messageFields, ...(parsedRequest?.args ?? []), ...refFields]);
22
+ const filteredReferencedTypes = (referencedTypes ?? []).filter(rt => rt.name !== viewType);
21
23
 
22
24
  const embeddedTypes = [];
23
25
  for (const field of messageFields) {
@@ -51,6 +53,23 @@ export class <%= typeName %> {
51
53
  <% } %>
52
54
  }
53
55
  <% } %>
56
+ <% for (const ref of filteredReferencedTypes) {
57
+ const parsedRefFields = (ref.fields ?? []).map(f => ({
58
+ name: f.name,
59
+ tsType: f.type ?? 'string',
60
+ gqlType: graphqlType(f.type ?? 'string'),
61
+ nullable: isNullable(f.type ?? 'string'),
62
+ }));
63
+ %>
64
+
65
+ @ObjectType()
66
+ export class <%= ref.name %> {
67
+ <% for (const f of parsedRefFields) { %>
68
+ @Field(() => <%= f.gqlType %><%= f.nullable ? ', { nullable: true }' : '' %>)
69
+ <%= f.name %><%= f.nullable ? '?' : '!' %>: <%= toTsFieldType(f.tsType) %>;
70
+ <% } %>
71
+ }
72
+ <% } %>
54
73
 
55
74
  @ObjectType()
56
75
  export class <%= viewType %> {
@@ -110,7 +129,9 @@ async <%= queryName %>(
110
129
  defaultValue = 'false';
111
130
  } else if (baseType === 'Date') {
112
131
  defaultValue = 'new Date()';
113
- } else if (baseType.startsWith('Array<')) {
132
+ } else if (messageNames && messageNames.has(baseType)) {
133
+ defaultValue = '{}';
134
+ } else if (baseType.startsWith('Array<') || baseType.endsWith('[]')) {
114
135
  defaultValue = '[]';
115
136
  } else if (isEnumType(baseType)) {
116
137
  const firstValue = baseType.match(/['"]([^'"]+)['"]/)?.[1] ?? '';