Skip to content

Commit da62644

Browse files
authored
Merge pull request #24 from Azure/mbhaskar/hot_chocolate_port
Porting code to use HotChocolate as the graphql engine
2 parents e210fb2 + 732861b commit da62644

File tree

15 files changed

+173
-379
lines changed

15 files changed

+173
-379
lines changed

Cosmos.GraphQL.Service/Cosmos.GraphQL.Service/Controllers/GraphQLController.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,30 +36,20 @@ public GraphQLController(ILogger<GraphQLController> logger, QueryEngine queryEng
3636
_schemaManager = schemaManager;
3737
}
3838

39-
[HttpGet]
40-
public IEnumerable<JObject> Get()
41-
{
42-
JObject data =JObject.Parse(JsonData);
43-
return Enumerable.Repeat(data, 1);
44-
}
45-
4639
[Route("addResolver")]
4740
[HttpPost]
4841
public void addResolver(GraphQLQueryResolver resolver)
4942
{
5043
_queryEngine.registerResolver(resolver);
51-
_schemaManager.attachQueryResolverToSchema(resolver.id);
5244
}
5345

5446
[Route("addMutationResolver")]
5547
[HttpPost]
5648
public void addMutationResolver(MutationResolver resolver)
5749
{
5850
_mutationEngine.registerResolver(resolver);
59-
_schemaManager.attachMutationResolverToSchema(resolver.id);
6051
}
6152

62-
6353
[Route("schema")]
6454
[HttpPost]
6555
public async void Schema()
@@ -72,7 +62,6 @@ public async void Schema()
7262
if (!String.IsNullOrEmpty(data))
7363
{
7464
this._schemaManager.parseAsync(data);
75-
7665
return;
7766
}
7867

Cosmos.GraphQL.Service/Cosmos.GraphQL.Service/Cosmos.GraphQL.Service.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
</ItemGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="GraphQL" Version="4.5.0" />
19-
<PackageReference Include="GraphQL.Server.Ui.GraphiQL" Version="5.0.2" />
20-
<PackageReference Include="GraphQL.SystemTextJson" Version="4.5.0" />
18+
19+
<PackageReference Include="HotChocolate" Version="12.0.0-rc.8" />
20+
<PackageReference Include="HotChocolate.AspNetCore" Version="12.0.0-rc.8" />
2121
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.18.0" />
2222
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Scripting" Version="3.10.0-3.final" />
2323
<PackageReference Include="Microsoft.Data.SqlClient" Version="3.0.0" />

Cosmos.GraphQL.Service/Cosmos.GraphQL.Service/Models/CosmosIdentifier.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

Cosmos.GraphQL.Service/Cosmos.GraphQL.Service/Models/GraphQLRequest.cs

Lines changed: 0 additions & 15 deletions
This file was deleted.

Cosmos.GraphQL.Service/Cosmos.GraphQL.Service/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"commandName": "Project",
2222
"dotnetRunMessages": "true",
2323
"launchBrowser": true,
24-
"launchUrl": "ui/graphiql",
24+
"launchUrl": "graphql",
2525
"applicationUrl": "https://localhost:5001;http://localhost:5000",
2626
"environmentVariables": {
2727
"ASPNETCORE_ENVIRONMENT": "Development"

Cosmos.GraphQL.Service/Cosmos.GraphQL.Service/Resolvers/CosmosClientProvider.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,4 @@ public CosmosClient getCosmosClient()
3737
return _cosmosClient;
3838
}
3939
}
40-
4140
}

Cosmos.GraphQL.Service/Cosmos.GraphQL.Service/Resolvers/Globals.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 15 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Reflection;
43
using System.Text.Json;
54
using System.Threading.Tasks;
6-
using Cosmos.GraphQL.Service.configurations;
75
using Cosmos.GraphQL.Service.Models;
8-
using Cosmos.GraphQL.Service.Resolvers;
96
using Cosmos.GraphQL.Services;
10-
using GraphQL.Execution;
11-
using GraphQL.Language.AST;
12-
using Microsoft.Azure.Cosmos;
13-
using Microsoft.CodeAnalysis.CSharp.Scripting;
14-
using Microsoft.CodeAnalysis.Scripting;
157
using Newtonsoft.Json;
168
using Newtonsoft.Json.Linq;
179

@@ -20,11 +12,8 @@ namespace Cosmos.GraphQL.Service.Resolvers
2012
public class MutationEngine
2113
{
2214
private readonly CosmosClientProvider _clientProvider;
23-
2415
private readonly IMetadataStoreProvider _metadataStoreProvider;
25-
26-
private ScriptOptions scriptOptions;
27-
16+
2817
public MutationEngine(CosmosClientProvider clientProvider, IMetadataStoreProvider metadataStoreProvider)
2918
{
3019
this._clientProvider = clientProvider;
@@ -34,24 +23,26 @@ public MutationEngine(CosmosClientProvider clientProvider, IMetadataStoreProvide
3423
public void registerResolver(MutationResolver resolver)
3524
{
3625
// TODO: add into system container/rp
37-
3826
this._metadataStoreProvider.StoreMutationResolver(resolver);
3927
}
4028

41-
private JObject execute(IDictionary<string, ArgumentValue> parameters, MutationResolver resolver)
29+
private JObject execute(IDictionary<string, object> inputDict, MutationResolver resolver)
4230
{
43-
JObject jObject = new JObject();
31+
// TODO: add support for all mutation types
32+
// we only support CreateOrUpdate (Upsert) for now
33+
34+
JObject jObject;
4435

45-
if (parameters != null)
36+
if (inputDict != null)
4637
{
47-
foreach (var prop in parameters)
48-
{
49-
jObject.Add(prop.Key, prop.Value.Value.ToString());
50-
}
38+
// TODO: optimize this multiple round of serialization/deserialization
39+
var json = JsonConvert.SerializeObject(inputDict);
40+
jObject = JObject.Parse(json);
5141
}
5242
else
5343
{
54-
jObject.Add("id", Guid.NewGuid().ToString());
44+
// TODO: in which scenario the inputDict is empty
45+
throw new NotSupportedException("inputDict is missing");
5546
}
5647

5748
var container = _clientProvider.getCosmosClient().GetDatabase(resolver.databaseName)
@@ -63,68 +54,14 @@ private JObject execute(IDictionary<string, ArgumentValue> parameters, MutationR
6354
}
6455

6556
public async Task<JsonDocument> execute(string graphQLMutationName,
66-
IDictionary<string, ArgumentValue> parameters)
57+
IDictionary<string, object> parameters)
6758
{
68-
6959
var resolver = _metadataStoreProvider.GetMutationResolver(graphQLMutationName);
7060

61+
// TODO: we are doing multiple round of serialization/deserialization
62+
// fixme
7163
JObject jObject = execute(parameters, resolver);
7264
return JsonDocument.Parse(jObject.ToString());
73-
74-
// switch (resolver.Operation)
75-
// {
76-
// case Operation.Upsert.ToString():
77-
// {
78-
// JObject jObject = toJObject(parameters);
79-
//
80-
// await container.UpsertItemAsync(jObject);
81-
//
82-
// break;
83-
//
84-
// }
85-
// default:
86-
// {
87-
// throw new NotImplementedException("not implemented");
88-
// }
89-
// }
90-
91-
92-
// ScriptState<object> scriptState = await runAndInitializedScript();
93-
// scriptState = await scriptState.ContinueWithAsync(resolver.dotNetCodeRequestHandler);
94-
// scriptState = await scriptState.ContinueWithAsync(resolver.dotNetCodeResponseHandler);
95-
// return scriptState.ReturnValue.ToString();
96-
97-
// // assert resolver != null
98-
// int result = await CSharpScript.EvaluateAsync<int>(resolver.dotNetCodeRequestHandler);
99-
// return result.ToString();
100-
}
101-
102-
// private async Task<string> execute()
103-
// {
104-
// CosmosCSharpScriptResponse response = await CosmosCSharpScript.ExecuteAsync(this.scriptState, code, this.scriptOptions);
105-
// this.scriptState = response.ScriptState;
106-
// this.scriptOptions = response.ScriptOption;
107-
//
108-
// object returnValue = this.scriptState?.ReturnValue;
109-
// Dictionary<string, object> mimeBundle = ToRichOutputMIMEBundle(returnValue);
110-
//
111-
// result.Data = mimeBundle;
112-
// }
113-
114-
115-
private async void executeInit()
116-
{
117-
Assembly netStandardAssembly = Assembly.Load("netstandard");
118-
this.scriptOptions = ScriptOptions.Default
119-
.AddReferences(
120-
Assembly.GetAssembly(typeof(Microsoft.Azure.Cosmos.CosmosClient)),
121-
Assembly.GetAssembly(typeof(JsonObjectAttribute)),
122-
Assembly.GetCallingAssembly(),
123-
netStandardAssembly)
124-
.WithImports(
125-
"Microsoft.Azure.Cosmos",
126-
"Newtonsoft.Json",
127-
"Newtonsoft.Json.Linq");
12865
}
12966
}
13067
}
Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,16 @@
1-
using System;
21
using System.Collections.Generic;
3-
using System.Configuration;
4-
using System.Reflection;
52
using System.Text.Json;
6-
using System.Threading.Tasks;
7-
using Cosmos.GraphQL.Service.configurations;
83
using Cosmos.GraphQL.Service.Models;
94
using Cosmos.GraphQL.Service.Resolvers;
10-
using GraphQL.Execution;
115
using Microsoft.Azure.Cosmos;
12-
using Microsoft.CodeAnalysis.CSharp.Scripting;
13-
using Microsoft.CodeAnalysis.Scripting;
14-
using Newtonsoft.Json;
156
using Newtonsoft.Json.Linq;
167

178
namespace Cosmos.GraphQL.Services
189
{
1910
public class QueryEngine
2011
{
2112
private readonly CosmosClientProvider _clientProvider;
22-
23-
private ScriptOptions scriptOptions;
24-
private IMetadataStoreProvider _metadataStoreProvider;
13+
private readonly IMetadataStoreProvider _metadataStoreProvider;
2514

2615
public QueryEngine(CosmosClientProvider clientProvider, IMetadataStoreProvider metadataStoreProvider)
2716
{
@@ -34,21 +23,22 @@ public void registerResolver(GraphQLQueryResolver resolver)
3423
this._metadataStoreProvider.StoreQueryResolver(resolver);
3524
}
3625

37-
public JsonDocument execute(string graphQLQueryName, IDictionary<string, ArgumentValue> parameters)
26+
public JsonDocument execute(string graphQLQueryName, IDictionary<string, object> parameters)
3827
{
28+
// TODO: fixme we have multiple rounds of serialization/deserialization JsomDocument/JObject
3929
// TODO: add support for nesting
4030
// TODO: add support for join query against another container
4131
// TODO: add support for TOP and Order-by push-down
4232

43-
var resolver = _metadataStoreProvider.GetQueryResolver(graphQLQueryName);
33+
var resolver = this._metadataStoreProvider.GetQueryResolver(graphQLQueryName);
4434
var container = this._clientProvider.getCosmosClient().GetDatabase(resolver.databaseName).GetContainer(resolver.containerName);
4535
var querySpec = new QueryDefinition(resolver.parametrizedQuery);
4636

4737
if (parameters != null)
4838
{
4939
foreach (var parameterEntry in parameters)
5040
{
51-
querySpec.WithParameter("@" + parameterEntry.Key, parameterEntry.Value.Value);
41+
querySpec.WithParameter("@" + parameterEntry.Key, parameterEntry.Value);
5242
}
5343
}
5444

@@ -67,43 +57,40 @@ public JsonDocument execute(string graphQLQueryName, IDictionary<string, Argumen
6757
return jsonDocument;
6858
}
6959

70-
public IEnumerable<JsonDocument> executeList(string graphQLQueryName, IDictionary<string, ArgumentValue> parameters)
60+
public IEnumerable<JsonDocument> executeList(string graphQLQueryName, IDictionary<string, object> parameters)
7161
{
62+
// TODO: fixme we have multiple rounds of serialization/deserialization JsomDocument/JObject
7263
// TODO: add support for nesting
7364
// TODO: add support for join query against another container
7465
// TODO: add support for TOP and Order-by push-down
7566

76-
var resolver = _metadataStoreProvider.GetQueryResolver(graphQLQueryName);
67+
var resolver = this._metadataStoreProvider.GetQueryResolver(graphQLQueryName);
7768
var container = this._clientProvider.getCosmosClient().GetDatabase(resolver.databaseName).GetContainer(resolver.containerName);
7869
var querySpec = new QueryDefinition(resolver.parametrizedQuery);
7970

8071
if (parameters != null)
8172
{
8273
foreach (var parameterEntry in parameters)
8374
{
84-
querySpec.WithParameter("@" + parameterEntry.Key, parameterEntry.Value.Value);
75+
querySpec.WithParameter("@" + parameterEntry.Key, parameterEntry.Value);
8576
}
8677
}
8778

88-
var firstPage = container.GetItemQueryIterator<JObject>(querySpec).ReadNextAsync().Result;
89-
90-
JObject firstItem = null;
91-
92-
var iterator = firstPage.GetEnumerator();
79+
FeedIterator<JObject> resultSetIterator = container.GetItemQueryIterator<JObject>(querySpec);
9380

9481
List<JsonDocument> resultsAsList = new List<JsonDocument>();
95-
while (iterator.MoveNext())
82+
while (resultSetIterator.HasMoreResults)
9683
{
97-
firstItem = iterator.Current;
98-
resultsAsList.Add(JsonDocument.Parse(firstItem.ToString()));
84+
var nextPage = resultSetIterator.ReadNextAsync().Result;
85+
IEnumerator<JObject> enumerator = nextPage.GetEnumerator();
86+
while (enumerator.MoveNext())
87+
{
88+
JObject item = enumerator.Current;
89+
resultsAsList.Add(JsonDocument.Parse(item.ToString()));
90+
}
9991
}
10092

10193
return resultsAsList;
10294
}
103-
104-
internal bool isListQuery(string queryName)
105-
{
106-
return _metadataStoreProvider.GetQueryResolver(queryName).isList;
107-
}
10895
}
10996
}

Cosmos.GraphQL.Service/Cosmos.GraphQL.Service/Services/CachedMetadataStoreProvider.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Concurrent;
33
using Cosmos.GraphQL.Service.Models;
4-
using GraphQL.Types;
54

65
namespace Cosmos.GraphQL.Services
76
{

0 commit comments

Comments
 (0)