-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathQueryBuilder.cs
More file actions
331 lines (288 loc) · 16.3 KB
/
QueryBuilder.cs
File metadata and controls
331 lines (288 loc) · 16.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using Azure.DataApiBuilder.Auth;
using Azure.DataApiBuilder.Config.DatabasePrimitives;
using Azure.DataApiBuilder.Config.ObjectModel;
using Azure.DataApiBuilder.Service.GraphQLBuilder.Directives;
using HotChocolate.Language;
using HotChocolate.Types;
using static Azure.DataApiBuilder.Service.GraphQLBuilder.GraphQLNaming;
using static Azure.DataApiBuilder.Service.GraphQLBuilder.GraphQLUtils;
namespace Azure.DataApiBuilder.Service.GraphQLBuilder.Queries
{
public static class QueryBuilder
{
public const string PAGINATION_FIELD_NAME = "items";
public const string PAGINATION_TOKEN_FIELD_NAME = "endCursor";
public const string PAGINATION_TOKEN_ARGUMENT_NAME = "after";
public const string HAS_NEXT_PAGE_FIELD_NAME = "hasNextPage";
public const string PAGE_START_ARGUMENT_NAME = "first";
public const string PAGINATION_OBJECT_TYPE_SUFFIX = "Connection";
public const string FILTER_FIELD_NAME = "filter";
public const string ORDER_BY_FIELD_NAME = "orderBy";
public const string PARTITION_KEY_FIELD_NAME = "_partitionKeyValue";
public const string ID_FIELD_NAME = "id";
public const string GROUP_BY_FIELD_NAME = "groupBy";
public const string GROUP_BY_FIELDS_FIELD_NAME = "fields";
public const string GROUP_BY_AGGREGATE_FIELD_NAME = "aggregations";
public const string GROUP_BY_AGGREGATE_FIELD_ARG_NAME = "field";
public const string GROUP_BY_AGGREGATE_FIELD_DISTINCT_NAME = "distinct";
public const string GROUP_BY_AGGREGATE_FIELD_HAVING_NAME = "having";
// Define the enabled database types for aggregation
public static readonly HashSet<DatabaseType> AggregationEnabledDatabaseTypes = new()
{
DatabaseType.MSSQL,
DatabaseType.DWSQL,
};
/// <summary>
/// Creates a DocumentNode containing FieldDefinitionNodes representing the FindByPK and FindAll queries
/// Also populates the DocumentNode with return types.
/// </summary>
/// <param name="root">Root of GraphQL schema</param>
/// <param name="databaseTypes">EnitityName to database Type of entity.</param>
/// <param name="entities">Map of entityName -> EntityMetadata</param>
/// <param name="entityPermissionsMap">Permissions metadata defined in runtime config.</param>
/// <param name="dbObjects">Database object metadata</param>
/// <returns>Queries DocumentNode</returns>
public static DocumentNode Build(
DocumentNode root,
Dictionary<string, DatabaseType> databaseTypes,
RuntimeEntities entities,
Dictionary<string, InputObjectTypeDefinitionNode> inputTypes,
Dictionary<string, EntityMetadata>? entityPermissionsMap = null,
Dictionary<string, DatabaseObject>? dbObjects = null,
bool _isAggregationEnabled = false
)
{
List<FieldDefinitionNode> queryFields = new();
List<ObjectTypeDefinitionNode> returnTypes = new();
foreach (IDefinitionNode definition in root.Definitions)
{
if (definition is ObjectTypeDefinitionNode objectTypeDefinitionNode && IsModelType(objectTypeDefinitionNode))
{
NameNode name = objectTypeDefinitionNode.Name;
string entityName = ObjectTypeToEntityName(objectTypeDefinitionNode);
// Skip types that don't have a corresponding entity configuration
// This can happen when merging schemas from multiple data sources
if (!entities.ContainsKey(entityName))
{
continue;
}
Entity entity = entities[entityName];
if (entity.Source.Type is EntitySourceType.StoredProcedure)
{
// Check runtime configuration of the stored procedure entity to check that the GraphQL operation type was overridden to 'query' from the default 'mutation.'
bool isSPDefinedAsQuery = entity.GraphQL.Operation is GraphQLOperation.Query;
IEnumerable<string> rolesAllowedForExecute = IAuthorizationResolver.GetRolesForOperation(entityName, operation: EntityActionOperation.Execute, entityPermissionsMap);
if (isSPDefinedAsQuery && rolesAllowedForExecute.Any())
{
if (dbObjects is not null && dbObjects.TryGetValue(entityName, out DatabaseObject? dbObject) && dbObject is not null)
{
queryFields.Add(GraphQLStoredProcedureBuilder.GenerateStoredProcedureSchema(name, entity, dbObject, rolesAllowedForExecute));
}
}
}
else
{
IEnumerable<string> rolesAllowedForRead = IAuthorizationResolver.GetRolesForOperation(entityName, operation: EntityActionOperation.Read, entityPermissionsMap);
bool isAggregationEnabledForEntity = _isAggregationEnabled && AggregationEnabledDatabaseTypes.Contains(databaseTypes[entityName]);
ObjectTypeDefinitionNode paginationReturnType = GenerateReturnType(name, isAggregationEnabledForEntity);
if (rolesAllowedForRead.Any())
{
queryFields.Add(GenerateGetAllQuery(objectTypeDefinitionNode, name, paginationReturnType, inputTypes, entity, rolesAllowedForRead));
queryFields.Add(GenerateByPKQuery(objectTypeDefinitionNode, name, databaseTypes[entityName], entity, rolesAllowedForRead));
}
if (paginationReturnType is not null)
{
returnTypes.Add(paginationReturnType);
}
}
}
}
List<IDefinitionNode> definitionNodes = new()
{
new ObjectTypeDefinitionNode(location: null, new NameNode("Query"), description: null, new List<DirectiveNode>(), new List<NamedTypeNode>(), queryFields),
};
definitionNodes.AddRange(returnTypes);
return new(definitionNodes);
}
public static FieldDefinitionNode GenerateByPKQuery(
ObjectTypeDefinitionNode objectTypeDefinitionNode,
NameNode name,
DatabaseType databaseType,
Entity entity,
IEnumerable<string>? rolesAllowedForRead = null)
{
IEnumerable<FieldDefinitionNode> primaryKeyFields =
FindPrimaryKeyFields(objectTypeDefinitionNode, databaseType);
List<InputValueDefinitionNode> inputValues = new();
List<DirectiveNode> fieldDefinitionNodeDirectives = new();
if (CreateAuthorizationDirectiveIfNecessary(
rolesAllowedForRead,
out DirectiveNode? authorizeDirective))
{
fieldDefinitionNodeDirectives.Add(authorizeDirective!);
}
foreach (FieldDefinitionNode primaryKeyField in primaryKeyFields)
{
inputValues.Add(new InputValueDefinitionNode(
location: null,
primaryKeyField.Name,
description: null,
primaryKeyField.Type,
defaultValue: null,
new List<DirectiveNode>()));
}
return new(
location: null,
new NameNode(GenerateByPKQueryName(name.Value, entity)),
new StringValueNode($"Get a {GetDefinedSingularName(name.Value, entity)} from the database by its ID/primary key"),
inputValues,
new NamedTypeNode(name),
fieldDefinitionNodeDirectives
);
}
public static FieldDefinitionNode GenerateGetAllQuery(
ObjectTypeDefinitionNode objectTypeDefinitionNode,
NameNode name,
ObjectTypeDefinitionNode returnType,
Dictionary<string, InputObjectTypeDefinitionNode> inputTypes,
Entity entity,
IEnumerable<string>? rolesAllowedForRead = null)
{
string filterInputName = InputTypeBuilder.GenerateObjectInputFilterName(objectTypeDefinitionNode.Name.Value);
if (!inputTypes.ContainsKey(filterInputName))
{
InputTypeBuilder.GenerateFilterInputTypeForObjectType(objectTypeDefinitionNode, inputTypes);
}
string orderByInputName = InputTypeBuilder.GenerateObjectInputOrderByName(objectTypeDefinitionNode.Name.Value);
if (!inputTypes.ContainsKey(orderByInputName))
{
InputTypeBuilder.GenerateOrderByInputTypeForObjectType(objectTypeDefinitionNode, inputTypes);
}
List<DirectiveNode> fieldDefinitionNodeDirectives = new();
if (CreateAuthorizationDirectiveIfNecessary(
rolesAllowedForRead,
out DirectiveNode? authorizeDirective))
{
fieldDefinitionNodeDirectives.Add(authorizeDirective!);
}
// Query field for the parent object type
// Generates a file like:
// books(first: Int, after: String, filter: BooksFilterInput, orderBy: BooksOrderByInput): BooksConnection!
return new(
location: null,
new NameNode(GenerateListQueryName(name.Value, entity)),
new StringValueNode($"Get a list of all the {GetDefinedSingularName(name.Value, entity)} items from the database"),
QueryArgumentsForField(filterInputName, orderByInputName),
new NonNullTypeNode(new NamedTypeNode(returnType.Name)),
fieldDefinitionNodeDirectives
);
}
public static List<InputValueDefinitionNode> QueryArgumentsForField(string filterInputName, string orderByInputName)
{
return new()
{
new(location: null, new NameNode(PAGE_START_ARGUMENT_NAME), description: new StringValueNode("The number of items to return from the page start point"), new IntType().ToTypeNode(), defaultValue: null, new List<DirectiveNode>()),
new(location: null, new NameNode(PAGINATION_TOKEN_ARGUMENT_NAME), new StringValueNode("A pagination token from a previous query to continue through a paginated list"), new StringType().ToTypeNode(), defaultValue: null, new List<DirectiveNode>()),
new(location: null, new NameNode(FILTER_FIELD_NAME), new StringValueNode("Filter options for query"), new NamedTypeNode(filterInputName), defaultValue: null, new List<DirectiveNode>()),
new(location: null, new NameNode(ORDER_BY_FIELD_NAME), new StringValueNode("Ordering options for query"), new NamedTypeNode(orderByInputName), defaultValue: null, new List<DirectiveNode>()),
};
}
public static ObjectTypeDefinitionNode AddQueryArgumentsForRelationships(ObjectTypeDefinitionNode node, Dictionary<string, InputObjectTypeDefinitionNode> inputObjects)
{
IEnumerable<FieldDefinitionNode> relationshipFields =
node.Fields.Where(field => field.Directives.Any(d => d.Name.Value == RelationshipDirectiveType.DirectiveName));
foreach (FieldDefinitionNode field in relationshipFields)
{
if (RelationshipDirectiveType.Cardinality(field) != Cardinality.Many)
{
continue;
}
string target = RelationshipDirectiveType.Target(field);
string targetFilterInputName = InputTypeBuilder.GenerateObjectInputFilterName(target);
string targetOrderByInputName = InputTypeBuilder.GenerateObjectInputOrderByName(target);
List<InputValueDefinitionNode> args = QueryArgumentsForField(targetFilterInputName, targetOrderByInputName);
List<FieldDefinitionNode> fields = node.Fields.ToList();
fields[fields.FindIndex(f => f.Name == field.Name)] = field.WithArguments(args);
node = node.WithFields(fields);
}
return node;
}
public static ObjectType PaginationTypeToModelType(ObjectType underlyingFieldType, IReadOnlyCollection<ITypeDefinition> types)
{
IEnumerable<ObjectType> modelTypes = types.Where(t => t is ObjectType)
.Cast<ObjectType>()
.Where(IsModelType);
return modelTypes.First(t => t.Name == underlyingFieldType.Name.Replace(PAGINATION_OBJECT_TYPE_SUFFIX, ""));
}
public static bool IsPaginationType(ObjectType objectType)
{
return objectType.Name.EndsWith(PAGINATION_OBJECT_TYPE_SUFFIX);
}
public static bool IsPaginationType(NamedTypeNode objectType)
{
return objectType.Name.Value.EndsWith(PAGINATION_OBJECT_TYPE_SUFFIX);
}
public static ObjectTypeDefinitionNode GenerateReturnType(NameNode name, bool isAggregationEnabled = false)
{
string scalarFieldsEnumName = EnumTypeBuilder.GenerateScalarFieldsEnumName(name.Value);
List<FieldDefinitionNode> fields = new() {
new(
location: null,
new NameNode(PAGINATION_FIELD_NAME),
new StringValueNode("The list of items that matched the filter"),
new List<InputValueDefinitionNode>(),
new NonNullTypeNode(new ListTypeNode(new NonNullTypeNode(new NamedTypeNode(name)))),
new List<DirectiveNode>()),
new(
location: null,
new NameNode(PAGINATION_TOKEN_FIELD_NAME),
new StringValueNode("A pagination token to provide to subsequent pages of a query"),
new List<InputValueDefinitionNode>(),
new StringType().ToTypeNode(),
new List<DirectiveNode>()),
new(
location: null,
new NameNode(HAS_NEXT_PAGE_FIELD_NAME),
new StringValueNode("Indicates if there are more pages of items to return"),
new List<InputValueDefinitionNode>(),
new NonNullType(new BooleanType()).ToTypeNode(),
new List<DirectiveNode>())
};
if (isAggregationEnabled)
{
fields.Add(
new(
location: null,
new NameNode(GROUP_BY_FIELD_NAME),
new StringValueNode("Group results by specified fields"),
new List<InputValueDefinitionNode>
{
new(
location: null,
new NameNode(GROUP_BY_FIELDS_FIELD_NAME),
new StringValueNode("Fields to group by"),
new ListTypeNode(new NonNullTypeNode(new NamedTypeNode(scalarFieldsEnumName))),
defaultValue: null,
new List<DirectiveNode>()
)
},
new NonNullTypeNode(new ListTypeNode(new NonNullTypeNode(new NamedTypeNode($"{name.Value}GroupBy")))),
new List<DirectiveNode>())
);
}
return new(
location: null,
new NameNode(GeneratePaginationTypeName(name.Value)),
new StringValueNode("The return object from a filter query that supports a pagination token for paging through results"),
new List<DirectiveNode>(),
new List<NamedTypeNode>(),
fields);
}
public static string GeneratePaginationTypeName(string name)
{
return $"{name}{PAGINATION_OBJECT_TYPE_SUFFIX}";
}
}
}