Skip to content

Instantly share code, notes, and snippets.

@rquackenbush
Created May 28, 2024 11:35
Show Gist options
  • Save rquackenbush/4b1047b83719463ea171012ad1342f98 to your computer and use it in GitHub Desktop.
Save rquackenbush/4b1047b83719463ea171012ad1342f98 to your computer and use it in GitHub Desktop.
CosmosDB / Gremlin Snippets
[Fact]
public async Task Explicit()
{
var twinId = Guid.NewGuid().ToString();
var requestScript = $"g.addV('twin').property('id', '{twinId}').property('partitionKey', '{twinId}').property('Prop1', 'SomeValue').property('Prop2', 42)";
var results = await _client.SubmitAsync<IDictionary<string, object>>(requestScript: requestScript);
var result = results.SingleOrDefault();
if (result != null)
{
if (!result.TryGetTypedValue<string>("id", out var id))
throw new InvalidOperationException("Unable to find 'id'");
if (!result.TryGetTypedValue<string>("label", out var label))
throw new InvalidOperationException("Unable to find 'label'");
if (!result.TryGetTypedValue<string>("type", out var @type))
throw new InvalidOperationException("Unable to find 'type'");
if (!result.TryGetTypedValue<IDictionary<string, object>>("properties", out var properties))
throw new InvalidOperationException("Unable to find 'properties'.");
if (properties != null)
{
foreach (var property in properties)
{
output.WriteLine($"{property.Key}");
var enumerable = property.Value as IEnumerable<object>;
if (enumerable != null)
{
foreach (var item in enumerable)
{
var dict = item as IDictionary<string, object>;
if (dict != null)
{
foreach (var kvp in dict)
{
output.WriteLine($" {kvp.Key}: {kvp.Value}");
}
}
}
}
}
}
}
}
//Note - need to construct the bloody reader using this hack https://stackoverflow.com/questions/68092798/gremlin-net-deserialize-number-property/72316108#72316108
[Fact]
public async Task Serialize()
{
var twinId = Guid.NewGuid().ToString();
var requestScript = $"g.addV('twin').property('id', '{twinId}').property('partitionKey', '{twinId}').property('Prop1', 'SomeValue').property('Prop2', 42)";
var results = await _client.SubmitAsync<dynamic>(requestScript: requestScript);
var result = results.SingleOrDefault();
if (result != null)
{
var json = JsonSerializer.Serialize(result, new JsonSerializerOptions
{
WriteIndented = true,
});
output.WriteLine(json);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment