I tried to write mutation but it gives me error.
as {"errors":[{"message":"Syntax Error: Expected $, found Name \"objects\"","locations":[{"line":2,"column":27}],"extensions":{"code":"GRAPHQL_PARSE_FAILED"}}]}
The code I wrote is this.
[HttpGet("GraphDataCreate")]
public async Task<dynamic> ReturnGraphDataCreate()
{
using var graphQLClient = new GraphQLHttpClient("https://api.spacex.land/graphql/", new NewtonsoftJsonSerializer());
var trial = new GraphQLRequest
{
Query = #"
query insert_users(objects: { name: $name, rocket: $rocket }) {
returning {
id
name
rocket
timestamp
twitter
}
}",
OperationName = "insert_users",
Variables = new
{
name = "AdiAntNam",
rocket = "SPUTNIK5V"
}
};
var graphQLResponse = (object)(null);
try
{
graphQLResponse = await graphQLClient.SendQueryAsync<dynamic>(trial);
}
catch (Exception ex)
{
var err = ex;
string err1 = ex.Message;
string err2 = ex.InnerException.Message;
}
return Task.FromResult(graphQLResponse);
}
Now what is it I'm missing in this part ?
The reference I've taken is from here.
https://github.com/graphql-dotnet/graphql-client
The problem with the example is the data type which is written that is hard to follow PersonAndFilms($id: ID) now ID is a data type so I was assuming that it was just a variable name declared that's why I was in confusion.
So I had written it as query insert_users(objects: { name: $name, rocket: $rocket }) which was not understandable for GraphQL as it requires Data Type, so I re-writed my query as below.
var trial = new GraphQLRequest
{
Query = #"
mutation xyz($nameU: String, $rocketU: String)
{
insert_users(objects: { name: $nameU , rocket: $rocketU })
{
returning
{
id
name
rocket
timestamp
twitter
}
}
}",
OperationName = "xyz",
Variables = new
{
nameU = "AdiAntNam2",
rocketU = "SPUTNIK5V"
}
};
Now the mutation xyz($nameU: String, $rocketU: String) it reads correct data type. The example is correct some what in the project but confusing to end user as $id: ID is not much understandable so its better to use example as mine.
Related
This feels like a simple question and I feel like I am overthinking it. I am doing an AWS project that will compare face(s) on an image to a database (s3bucket) of other faces. So far, I have a lambda function for the comparefacerequest, a class library which invokes the function, and an UWP that inputs the image file and outputs a result. It has worked so far being based on boolean (true or false) functions, but now I want it to instead return what face(s) are recognized via an array. I struggling at implementing this.
Below is my lambda function. I have adjusted the task to be an Array instead of a bool and changed the return to be an array. At the bottom, I have created a global variable class with a testing array so I could attempt to reference the array elsewhere.
public class Function
{
//Function
public async Task<Array> FunctionHandler(string input, ILambdaContext context)
{
//number of matched faces
int matched = 0;
//Client setup
var rekognitionclient = new AmazonRekognitionClient();
var s3client = new AmazonS3Client();
//Create list of target images
ListObjectsRequest list = new ListObjectsRequest
{
BucketName = "bucket2"
};
ListObjectsResponse listre = await s3client.ListObjectsAsync(list);
//loop of list
foreach (Amazon.S3.Model.S3Object obj in listre.S3Objects)
{
//face request with input and obj.key images
var comparefacesrequest = new CompareFacesRequest
{
SourceImage = new Image
{
S3Object = new S3Objects
{
Bucket = "bucket1",
Name = input
}
},
TargetImage = new Image
{
S3Object = new S3Objects
{
Bucket = "bucket2",
Name = obj.Key
}
},
};
//compare with confidence of 95 (subject to change) to current target image
var detectresponse = await rekognitionclient.CompareFacesAsync(comparefacesrequest);
detectresponse.FaceMatches.ForEach(match =>
{
ComparedFace face = match.Face;
if (match.Similarity > 95)
{
//if face detected, raise matched
matched++;
for(int i = 0; i < Globaltest.testingarray.Length; i++)
{
if (Globaltest.testingarray[i] == "test")
{
Globaltest.testingarray[i] = obj.Key;
}
}
}
});
}
//Return true or false depending on if it is matched
if (matched > 0)
{
return Globaltest.testingarray;
}
return Globaltest.testingarray;
}
}
public static class Globaltest
{
public static string[] testingarray = { "test", "test", "test" };
}
Next, is my invoke request in my class library. It has so far been based on the lambda outputting a boolean result, but I thought, "hey, it is parsing the result, it should be fine, right"? I do convert the result to a string, as there is no GetArray, from what I know.
public async Task<bool> IsFace(string filePath, string fileName)
{
await UploadS3(filePath, fileName);
AmazonLambdaClient client = new AmazonLambdaClient(accessKey, secretKey, Amazon.RegionEndpoint.USWest2);
InvokeRequest ir = new InvokeRequest();
ir.InvocationType = InvocationType.RequestResponse;
ir.FunctionName = "ImageTesting";
ir.Payload = "\"" + fileName + "\"";
var result = await client.InvokeAsync(ir);
var strResponse = Encoding.ASCII.GetString(result.Payload.ToArray());
if (bool.TryParse(strResponse, out bool result2))
{
return result2;
}
return false;
}
Finally, here is the section of my UWP where I perform the function. I am referencing the lambda client via "using Lambdaclienttest" (name of lamda project, and this is its only instance I use the reference though). When I run my project, I do still get a face detected when it should, but the Globaltest.testingarray[0] is still equal to "test".
var Facedetector = new FaceDetector(Credentials.accesskey, Credentials.secretkey);
try
{
var result = await Facedetector.IsFace(filepath, filename);
if (result)
{
textBox1.Text = "There is a face detected";
textBox2.Text = Globaltest.testingarray[0];
}
else
{
textBox1.Text = "Try Again";
}
}
catch
{
textBox1.Text = "Please use a photo";
}
Does anyone have any suggestions?
I have a lot data stored in an azure table storage table (string datatype).
I want to post only certain of these fields to an API every x minute in a .json format using an azure function with timer trigger. Everything is good but:
The format of the json is currently including square brackets, which I'm trying to remove without any sucess:
The most important is to remove the brackets, so maybe I can "hardcode" something to remove the brackets? I have used a lot of time to get it in correct "grouping" format. Can it be done in a much easier way?
{
"Test":[
{
"name":"Tom"
}
],
"Info":[
{
"Weight":"122",
"Age":"22"
}
],
"License":[
{
"number":"2222312"
}
]
}
First:
I'm querying the data in the azure table storage:
public async Task Run(
[TimerTrigger("0 */5 * * * *", RunOnStartup = true)] TimerInfo myTimer,
[Table("TestTable", Connection = "STORAGE-ConnectionString")] CloudTable TestTable,
ICollector<string> events,
ILogger log)
{
try
{
TableContinuationToken token = null;
List<PersonEntity> Entities = new List<PersonEntity>();
do
{
var query = new TableQuery<PersonEntity>().Where(TableQuery.GenerateFilterCondition("Status"
, QueryComparisons.Equal, "NEW"));
var result = await TestTable.ExecuteQuerySegmentedAsync(query, token);
Entities.AddRange(result.Results);
token = result.ContinuationToken;
} while (token != null);
Secondly: I'm trying to collect only certain of the fields in the Azure table with the following code.
IEnumerable<IGrouping<string, PersonEntity>> groupedEntities = Entities.GroupBy(x => x.PartitionKey);
foreach (IGrouping<string, PersonEntity> group in groupedEntities)
{
PersonEntity baseEntity = group.First();
var personBooking = new PersonBookingModel
{
Test = group.Select(x => new PersonBookingRouteModel
{
name=x.PersonName.ToString()
}).ToList(),
Info = group.Select(x => new PersonBookingGoodsModel
{
Weight= x.PersonWeight.ToString(),
Age = x.PersonAge.ToString()
}).ToList(),
License = group.Select(x => new PersonBookingDriverModel
{
number = x.LicenceNumber.ToString(),
}).ToList()
};
Furthermore:
try
{
var payload = JsonConvert.SerializeObject(personBooking);
using (HttpContent content = new StringContent(payload, Encoding.UTF8, "application/json"))
{
if (payload == "")
{
log.LogWarning("No payload created sucessfully");
}
else {
log.LogWarning(payload);
}
latestResponse = await httpClient.PostAsync(Environment.GetEnvironmentVariable("API-Endpoint"), content).ConfigureAwait(false);
if (latestResponse.IsSuccessStatusCode)
{
log.LogWarning("Response with endpoint");
}
string data = await latestResponse.Content.ReadAsStringAsync().ConfigureAwait(false);
Desired output without brackets need to be:
{
"Test":
{
"name":"Tom"
}
,
"Info":
{
"Weight":"122",
"Age":"22"
}
,
"License":
{
"number":"2222312"
}
}
As per the above comments, I'm converting your comment as answer so that may help other community members:
So after changing the code to this-
var res = payload.Replace ("[","").Replace ("]","");
Hopefully resolved your issue.
So I finally figured out a way to successfully make detect intent calls and provide an input context. My question is whether or not this is the CORRECT (or best) way to do it:
(And yes, I know you can just call DetectIntent(agent, session, query) but I have to provide a input context(s) depending on the request)
var query = new QueryInput
{
Text = new TextInput
{
Text = model.Content,
LanguageCode = string.IsNullOrEmpty(model.Language) ? "en-us" : model.Language,
}
};
var commonContext = new global::Google.Cloud.Dialogflow.V2.Context
{
ContextName = new ContextName(agent, model.sessionId, "my-input-context-data"),
LifespanCount = 3,
Parameters = new Struct
{
Fields = {
{ "Source", Value.ForString(model.Source) },
{ "UserId" , Value.ForString(model.UserId.ToString())},
{ "Name" , Value.ForString(model.FirstName)}
}
}
};
var request = new DetectIntentRequest
{
SessionAsSessionName = new SessionName(agent, model.sessionId),
QueryParams = new QueryParameters
{
GeoLocation = new LatLng {Latitude = model.Latitude, Longitude = model.Longitude},
TimeZone = model.TimeZone ?? "MST"
},
QueryInput = query
};
request.QueryParams.Contexts.Add(commonContext);
// ------------
var creds = GetGoogleCredentials("myCredentials.json");
var channel = new Grpc.Core.Channel(SessionsClient.DefaultEndpoint.Host, creds.ToChannelCredentials());
var client = SessionsClient.Create(channel);
var response = client.DetectIntent(request);
channel.ShutdownAsync();
return response;
Note: I included the explicit ShutDownAsync (it's not in an async call) because I was getting some file locking issues when attempting to re-deploy the WebAPI project (and only after having executed this code).
Thanks
Chris
Updated 4/25: The most basic way I use this is to integrate the user's name into intent responses:
It can also be read from within the webhook/inline fulfillment index.js:
const name = request.body.queryResult && request.body.queryResult.outputContexts && request.body.queryResult.outputContexts[0].parameters.Name
StackOverflow Community!
I have a chatbot, and integrated LUIS.ai to make it smarter. One of the dialogues is about to Book an appointment with a Supervisor (Teacher)
Everything has been working fine, with literally the same code. A couple of hours ago I am experiencing some strange errors.
Exception: Type 'Newtonsoft.Json.Linq.JArray' in Assembly 'Newtonsoft.Json, Version=10.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' is not marked as serializable.
How to reproduce the error?
If the both Entity (Teacher and Date) is missing from the user's input it works FINE, the bot builds the Form, asking for missing inputs and presents the suggested meeting times.
If one of the Entity is missing it from input it will build a Form and asks for the missing Date or Teacher Entity and presents the suggested meeting times.
BUT
If the user's input contains both Entity: the Teacher and the Date too, then I am getting the error.
Here is my WebApiConfig class:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Json settings
config.Formatters.JsonFormatter.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
config.Formatters.JsonFormatter.SerializerSettings.Formatting = Formatting.Indented;
JsonConvert.DefaultSettings = () => new JsonSerializerSettings()
{
ContractResolver = new CamelCasePropertyNamesContractResolver(),
Formatting = Newtonsoft.Json.Formatting.Indented,
NullValueHandling = NullValueHandling.Ignore,
};
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
I am only experiencing this error when I am trying to get an Entity from the user utterance, which is a type of builtin.dateTimeV2.
This async method is called:
//From the LUIS.AI language model the entities
private const string EntityMeetingDate = "MeetingDate";
private const string EntityTeacher = "Teacher";
[LuisIntent("BookSupervision")]
public async Task BookAppointment(IDialogContext context, IAwaitable<IMessageActivity> activity, LuisResult result)
{
var message = await activity;
await context.PostAsync($"I am analysing your message: '{message.Text}'...");
var meetingsQuery = new MeetingsQuery();
EntityRecommendation teacherEntityRecommendation;
EntityRecommendation dateEntityRecommendation;
if (result.TryFindEntity(EntityTeacher, out teacherEntityRecommendation))
{
teacherEntityRecommendation.Type = "Name";
}
if (result.TryFindEntity(EntityMeetingDate, out dateEntityRecommendation))
{
dateEntityRecommendation.Type = "Date";
}
var meetingsFormDialog = new FormDialog<MeetingsQuery>(meetingsQuery, this.BuildMeetingsForm, FormOptions.PromptInStart, result.Entities);
context.Call(meetingsFormDialog, this.ResumeAfterMeetingsFormDialog);
}
Further methods for building a form:
private IForm<MeetingsQuery> BuildMeetingsForm()
{
OnCompletionAsyncDelegate<MeetingsQuery> processMeetingsSearch = async (context, state) =>
{
var message = "Searching for supervision slots";
if (!string.IsNullOrEmpty(state.Date))
{
message += $" at {state.Date}...";
}
else if (!string.IsNullOrEmpty(state.Name))
{
message += $" with professor {state.Name}...";
}
await context.PostAsync(message);
};
return new FormBuilder<MeetingsQuery>()
.Field(nameof(MeetingsQuery.Date), (state) => string.IsNullOrEmpty(state.Date))
.Field(nameof(MeetingsQuery.Name), (state) => string.IsNullOrEmpty(state.Name))
.OnCompletion(processMeetingsSearch)
.Build();
}
private async Task ResumeAfterMeetingsFormDialog(IDialogContext context, IAwaitable<MeetingsQuery> result)
{
try
{
var searchQuery = await result;
var meetings = await this.GetMeetingsAsync(searchQuery);
await context.PostAsync($"I found {meetings.Count()} available slots:");
var resultMessage = context.MakeMessage();
resultMessage.AttachmentLayout = AttachmentLayoutTypes.Carousel;
resultMessage.Attachments = new List<Attachment>();
foreach (var meeting in meetings)
{
HeroCard heroCard = new HeroCard()
{
Title = meeting.Teacher,
Subtitle = meeting.Location,
Text = meeting.DateTime,
Images = new List<CardImage>()
{
new CardImage() {Url = meeting.Image}
},
Buttons = new List<CardAction>()
{
new CardAction()
{
Title = "Book Appointment",
Type = ActionTypes.OpenUrl,
Value = $"https://www.bing.com/search?q=easj+roskilde+" + HttpUtility.UrlEncode(meeting.Location)
}
}
};
resultMessage.Attachments.Add(heroCard.ToAttachment());
}
await context.PostAsync(resultMessage);
}
catch (FormCanceledException ex)
{
string reply;
if (ex.InnerException == null)
{
reply = "You have canceled the operation.";
}
else
{
reply = $"Oops! Something went wrong :( Technical Details: {ex.InnerException.Message}";
}
await context.PostAsync(reply);
}
finally
{
context.Wait(DeconstructionOfDialog);
}
}
private async Task<IEnumerable<Meeting>> GetMeetingsAsync(MeetingsQuery searchQuery)
{
var meetings = new List<Meeting>();
//some random result manually for demo purposes
for (int i = 1; i <= 5; i++)
{
var random = new Random(i);
Meeting meeting = new Meeting()
{
DateTime = $" Available time: {searchQuery.Date} At building {i}",
Teacher = $" Professor {searchQuery.Name}",
Location = $" Elisagårdsvej 3, Room {random.Next(1, 300)}",
Image = $"https://placeholdit.imgix.net/~text?txtsize=35&txt=Supervision+{i}&w=500&h=260"
};
meetings.Add(meeting);
}
return meetings;
}
The strangest thing that this code has worked, and my shoutout and respect goes for the community on GitHub, because I think this is an awesome platform with a tons of documentation and samples.
This is known issue (also reported here and here).
Long story short, since the builtin.datetimeV2.* entities are not yet supported in BotBuilder, the Resolution dictionary of the EntityRecommendation ends up with an entry with a value of type JArray. The problem arises when you pass those entities to a FormDialog. Since the entities are a private field in the dialog and of course, as any other dialog, is being serialized, an exception is being thrown because the JArray class from Newtonsoft is not marked as serializable.
The request for adding support for datetimeV2 entities is here.
The workaround I can think of right now is to extract the value of the DateTime entity manually and assign it your Date field of your MeetingsQuery instance you are passing to the FormDialog and also to remove the DateTime entity from the result.Entities collection that you are passing to the FormDialog.
Update
This is already fixed in the SDK as you can see in this Pull Request.
I have a ASP MVC project with some Ajax Calls, where I am trying to pass from jquery/Ajax to my AjaxController (see bellow code) where in the controller sting item is receiving a json string like this
"[\n \"1002\",\n \"1003\"\n]"
And I get this error in my controller (See bellow code where the comment identifies the error)
Newtonsoft.Json.JsonSerializationException: Error converting value "1002" to type 'System.String[]'. Path '[0]', line 2, position 9. ---> System.ArgumentException: Could not cast or convert from System.String to System.String[]. at Newtonsoft.Json.Utilities.ConvertUtils.EnsureTypeAssignable(Object value, Type initialType, Type targetType) at Newtonsoft.Json.Utilities.ConvertUtils.ConvertOrCast(Object initialValue, CultureInfo culture, Type targetType) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType) ...(continue)
This is my Json/Jquery creation function
function SaveObservations() {
var SerSel = $("#ServiceIdSelect2").val();
var obs = $("#observation").val();
var itemsX = [];
if (obs == "") {
mostrar_alert_ui("Validation message", "Observation cannot be null", 350);
} else {
//Start json creation
$("#ItemsPieces > input:checked").each(function () {
var id = $(this).val();
itemsX.push(id);
});
var itemsY = JSON.stringify(itemsX, null, 2);
//End json creation
Funciones_Ajax_conAlert("/Ajax/SendEmailItemsToClient/", { proyecto: SerSel, items: itemsY, observation: obs }, CloseObservations);
}
}
And here is my controller where the error is given
public JsonResult SendEmailItemsToClient(int proyecto, string items, string observation)
{
object data = null;
try
{
List<string[]> datax1 = JsonConvert.DeserializeObject<List<string[]>>(items); //Here is the issue Aqui tengo el problema
foreach (var item in datax1)
{
string test = item.ToString();
}
string mensaje = "";
int ProjectIdx = proyecto;
bool resp = CorreccionesController.SendItemsDifferentsToClient(ProjectIdx, mensaje);
if (resp) {
data = new
{
success = true,
titulo = "Notification",
mensaje = "A message explaining why the different items selected are used had been sent"
};
}
else
{
data = new
{
success = false,
titulo = "Notification",
mensaje = "The observation is Saved but the email couldn't be send, please contact support"
};
}
}
catch (Exception ex)
{
data = new
{
success = false,
titulo = "ERROR",
mensaje = ex.ToString()
};
}
return Json(data, JsonRequestBehavior.AllowGet);
}
The question would be how can I itterate that json string without receiving an error?
Your code need a little bit of refactoring; basically you have a json structure like this one:
[
"1002",
"1003"
]
That is basically an Array of Strings.
In your Controller you have the following line:
List<string[]> datax1 = JsonConvert.DeserializeObject<List<string[]>>(items);
Now, what this little chunk of code List<string[]> means? With that line you are trying to create a List of arrays of string, something like this:
[
["1002","1003"],
["1002","1003"]
]
So your deserializing methods fails with the following message: Could not cast or convert from System.String to System.String[]. Now makes sense.
So if you want to deserialize an json array of string you just needs:
List<string> datax1 = JsonConvert.DeserializeObject<List<string>>(items);
List<string> is just like and array of string(internally a list is constructed on an array basis, check this answer for more information about array and list: Array versus List<T>: When to use which?
Based on that info you can write your code this way too:
string[] datax1 = JsonConvert.DeserializeObject<string[]>(items); //Not tested, but should works.
Don't double Json encode, and let the WebAPI do all the work:
function SaveObservations() {
var SerSel = $("#ServiceIdSelect2").val();
var obs = $("#observation").val();
var itemsX = [];
if (obs == "") {
mostrar_alert_ui("Validation message", "Observation cannot be null", 350);
} else {
//Start json creation
$("#ItemsPieces > input:checked").each(function () {
var id = $(this).val();
itemsX.push(id);
});
//End json creation
Funciones_Ajax_conAlert("/Ajax/SendEmailItemsToClient/", { proyecto: SerSel, items: itemsX, observation: obs }, CloseObservations);
}
}
and
public JsonResult SendEmailItemsToClient(int proyecto, string[] items, string observation)
{
object data = null;
try
{
foreach (var item in items)
{
string test = item.ToString();
}
string mensaje = "";
int ProjectIdx = proyecto;
bool resp = CorreccionesController.SendItemsDifferentsToClient(ProjectIdx, mensaje);
if (resp) {
data = new
{
success = true,
titulo = "Notification",
mensaje = "A message explaining why the different items selected are used had been sent"
};
}
else
{
data = new
{
success = false,
titulo = "Notification",
mensaje = "The observation is Saved but the email couldn't be send, please contact support"
};
}
}
catch (Exception ex)
{
data = new
{
success = false,
titulo = "ERROR",
mensaje = ex.ToString()
};
}
return Json(data, JsonRequestBehavior.AllowGet);
}