@@ -10,6 +10,12 @@ module rec QueryBuilderWriter =
1010 let [<Literal>] private QueryRootObjectName = " QueryRoot"
1111 let [<Literal>] private MutationRootObjectName = " Mutation"
1212
13+ // Fully qualify class names which might collide with System types
14+ let qualifiedPascalTypeName className =
15+ match toCasing Pascal className with
16+ | " Attribute" -> " ShopifySharp.GraphQL.Attribute"
17+ | pascalGenericType -> pascalGenericType
18+
1319 let private canAddFields = function
1420 | VisitedTypes.Class _ -> true
1521 | VisitedTypes.Interface _ -> true
@@ -22,17 +28,18 @@ module rec QueryBuilderWriter =
2228 let private canAddArguments ( type' : VisitedTypes ) =
2329 type'.IsOperation
2430
25- let private writeUnionTypeMutationJoins ( pascalParentClassName : string ) ( unionCaseName : string ) ( _ : IParsedContext ) writer : ValueTask =
31+ let private writeUnionCaseJoin ( pascalParentClassName : string ) ( unionCaseName : string ) ( fieldName : string ) ( _ : IParsedContext ) writer : ValueTask =
2632 pipeWriter writer {
2733 let pascalUnionCaseName = toCasing Pascal unionCaseName
28- let camelUnionCaseName = toCasing Camel unionCaseName
34+ let pascalFieldName = toCasing Pascal fieldName
35+ let camelFieldName = toCasing Camel fieldName
2936 let unionCaseQueryBuilderName = $" {pascalUnionCaseName}QueryBuilder"
3037
31- do ! Indented + $" public {pascalParentClassName} AddUnion{pascalUnionCaseName }(Func<{unionCaseQueryBuilderName}, {unionCaseQueryBuilderName}> build)"
38+ do ! Indented + $" public {pascalParentClassName} AddUnionCase{pascalFieldName }(Func<{unionCaseQueryBuilderName}, {unionCaseQueryBuilderName}> build)"
3239 do ! NewLine
3340 do ! Indented + " {"
3441 do ! NewLine
35- do ! DoubleIndented + $" AddUnion<{pascalUnionCaseName}>(\" {camelUnionCaseName }\" , build);"
42+ do ! DoubleIndented + $" AddUnion<{pascalUnionCaseName}, {unionCaseQueryBuilderName} >(\" {camelFieldName }\" , build);"
3643 do ! NewLine
3744 do ! DoubleIndented + " return this;"
3845 do ! NewLine
@@ -50,48 +57,47 @@ module rec QueryBuilderWriter =
5057 let camelFieldName = toCasing Camel fieldName
5158
5259 pipeWriter writer {
53- // TODO: does the context actually know about union cases/named types now that we parse the document definitions directly?
54- // (i.e. now that we aren't using the visitor pattern)
55- if context.TypeIsKnownUnionCase fieldName || context.IsNamedType ( NamedType.UnionType fieldName) then
56- yield ! writeUnionTypeMutationJoins pascalClassName fieldName context
57- else
58- match AstNodeMapper.unwrapFieldType fieldType with
59- | FieldValueType.GraphObjectType ( NamedType.UnionType graphObjectTypeName) ->
60- printfn $" {graphObjectTypeName} should have procced the writeUnionTypeMutationJoins fn"
61- yield ! writeUnionTypeMutationJoins pascalClassName fieldName context
62- | FieldValueType.GraphObjectType ( NamedType.Class graphObjectTypeName)
63- | FieldValueType.GraphObjectType ( NamedType.Interface graphObjectTypeName)
64- | FieldValueType.GraphObjectType ( NamedType.InputObject graphObjectTypeName) ->
65- let pascalTypeName = toCasing Pascal graphObjectTypeName
66- let queryBuilderName = $" {toCasing Pascal graphObjectTypeName}QueryBuilder"
67-
68- // TODO: if this is a collection type (not fieldType.IsFieldValueType), use the AddField collection overload
69-
70- yield ! writeDeprecationAttribute Indented None
71- do ! Indented + $" public {pascalClassName} AddField{pascalFieldName}(Func<{queryBuilderName}, {queryBuilderName}> build)"
72- do ! NewLine
73- do ! DoubleIndented + " {"
74- do ! NewLine
75- do ! TripleIndented + $" AddField<{pascalTypeName}, {queryBuilderName}>(\" {camelFieldName}\" , build);"
76- do ! NewLine
77- do ! TripleIndented + " return this;"
78- do ! NewLine
79- do ! DoubleIndented + " }"
80- do ! NewLine
81- | _ ->
82- // TODO: if this is a collection type (not fieldType.IsFieldValueType), use the AddField collection overload
83-
84- yield ! writeDeprecationAttribute Indented deprecationWarning
85- do ! Indented + $" public {pascalClassName} AddField{pascalFieldName}()"
86- do ! NewLine
87- do ! DoubleIndented + " {"
88- do ! NewLine
89- do ! DoubleIndented + $" AddField(\" {camelFieldName}\" );"
90- do ! NewLine
91- do ! TripleIndented + " return this;"
92- do ! NewLine
93- do ! DoubleIndented + " }"
94- do ! NewLine
60+ match AstNodeMapper.unwrapFieldType fieldType with
61+ | FieldValueType.GraphObjectType ( NamedType.UnionType (_, unionCaseNames)) ->
62+ // yield! writeUnionCaseJoin pascalClassName unionTypeName fieldName context
63+ for unionCaseName in unionCaseNames do
64+ yield ! writeUnionCaseJoin pascalClassName unionCaseName fieldName context
65+ | FieldValueType.GraphObjectType ( NamedType.Class graphObjectTypeName as namedType)
66+ | FieldValueType.GraphObjectType ( NamedType.Interface graphObjectTypeName as namedType)
67+ | FieldValueType.GraphObjectType ( NamedType.InputObject graphObjectTypeName as namedType) ->
68+ let pascalTypeName =
69+ if namedType.IsInterface
70+ then toCasing Pascal ( mapStrToInterfaceName graphObjectTypeName)
71+ else qualifiedPascalTypeName graphObjectTypeName
72+ let queryBuilderName = $" {toCasing Pascal graphObjectTypeName}QueryBuilder"
73+
74+ // TODO: if this is a collection type (not fieldType.IsFieldValueType), use the AddField collection overload
75+
76+ yield ! writeDeprecationAttribute Indented None
77+ do ! Indented + $" public {pascalClassName} AddField{pascalFieldName}(Func<{queryBuilderName}, {queryBuilderName}> build)"
78+ do ! NewLine
79+ do ! DoubleIndented + " {"
80+ do ! NewLine
81+ do ! TripleIndented + $" AddField<{pascalTypeName}, {queryBuilderName}>(\" {camelFieldName}\" , build);"
82+ do ! NewLine
83+ do ! TripleIndented + " return this;"
84+ do ! NewLine
85+ do ! DoubleIndented + " }"
86+ do ! NewLine
87+ | _ ->
88+ // TODO: if this is a collection type (not fieldType.IsFieldValueType), use the AddField collection overload
89+
90+ yield ! writeDeprecationAttribute Indented deprecationWarning
91+ do ! Indented + $" public {pascalClassName} AddField{pascalFieldName}()"
92+ do ! NewLine
93+ do ! DoubleIndented + " {"
94+ do ! NewLine
95+ do ! DoubleIndented + $" AddField(\" {camelFieldName}\" );"
96+ do ! NewLine
97+ do ! TripleIndented + " return this;"
98+ do ! NewLine
99+ do ! DoubleIndented + " }"
100+ do ! NewLine
95101 }
96102
97103 let private writeQueryBuilderAddFieldMethods ( pascalClassName : string ) ( type' : VisitedTypes ) ( context : IParsedContext ) writer : ValueTask =
@@ -114,7 +120,8 @@ module rec QueryBuilderWriter =
114120 match visitedType with
115121 | VisitedTypes.UnionType unionType ->
116122 for unionCase in unionType.Cases do
117- yield ! writeUnionTypeMutationJoins pascalClassName unionCase.Name context
123+ yield ! writeUnionCaseJoin pascalClassName unionCase.Name unionCase.Name context
124+ //yield! writeUnionTypeJoin pascalClassName unionCase.Name context
118125 | visitedType ->
119126 let fields =
120127 match visitedType with
@@ -190,23 +197,20 @@ module rec QueryBuilderWriter =
190197
191198 let genericType =
192199 match type' with
200+ | VisitedTypes.Interface interface' ->
201+ interface'.DotnetName
193202 | VisitedTypes.Operation operation ->
194203 match operation.ReturnType with
204+ | ReturnType.VisitedType ( VisitedTypes.Interface interface') ->
205+ interface'.DotnetName
195206 | ReturnType.VisitedType visitedTypes ->
196207 visitedTypes.Name
197208 | ReturnType.FieldType fieldType ->
198209 // Use the wrapper function to ensure primitives are wrapped in GraphQLValue<T> or GraphQLCollection<T>
199210 AstNodeMapper.mapFieldTypeToStringWithPrimitiveWrapper context.AssumeNullability fieldType
200211 | x -> x.Name
201212
202- // Fully qualify class names that might collide with System types
203- let qualifiedGenericType =
204- let pascalGenericType = toCasing Pascal genericType
205- // The type name should already have the "I" prefix if it's an interface
206- // (it comes from VisitedTypes.Name which includes the prefix)
207- match pascalGenericType with
208- | " Attribute" -> " ShopifySharp.GraphQL.Attribute"
209- | _ -> pascalGenericType
213+ let qualifiedGenericType = qualifiedPascalTypeName genericType
210214
211215 do ! $" public class {pascalClassName}(): GraphQueryBuilder<{qualifiedGenericType}>(\" {camelTypeName}\" )"
212216
0 commit comments