I am new to AWS AppSync and am trying to use an HTTP endpoint to fetch data from an external API. Here is an example of what that API returns.
{
"status": 1,
"size": 3,
"result": [
{
"number": "123",
"program": "program name",
"team_name": "team name",
"robot_name": "robot name",
"organisation": "team organization",
"city": "team city",
"region": "team state",
"country": "team country",
"grade": "team grade",
"is_registered": 0
},
{
"number": "456",
"program": "program name",
"team_name": "team name",
"robot_name": "robot name",
"organisation": "team organization",
"city": "team city",
"region": "team state",
"country": "team country",
"grade": "team grade",
"is_registered": 0
},
{
"number": "789",
"program": "program name",
"team_name": "team name",
"robot_name": "robot name",
"organisation": "team organization",
"city": "team city",
"region": "team state",
"country": "team country",
"grade": "team grade",
"is_registered": 0
}
]
}
Here is my GraphQL Schema
type Query {
getTeams(number: String!): Team
}
type Team {
number: String
program: String
teamName: String
robotName: String
organization: String
city: String
region: String
country: String
grade: String
isRegistered: Int
}
schema {
query: Query
}
Here is my request mapping template
{
"version": "2018-05-29",
"method": "GET",
"resourcePath": "/v1/get_teams",
"params":{
"query": {
"APIKEY": "API_KEY_GOES_HERE"
},
"headers": {
"Content-Type": "application/json"
}
}
}
And here is my response mapping template
#if($context.result.statusCode == 200)
## Success - decode the body and reconstruct the response with the schema in mind
#set($response = $util.parseJson($context.result.body))
#set($result = {
"number": $response.result[0].number,
"program": $response.result[0].program,
"teamName": $response.result[0].team_name,
"robotName": $response.result[0].robot_name,
"organization": $response.result[0].organisation,
"city": $response.result[0].city,
"region": $response.result[0].region,
"country": $response.result[0].country,
"grade": $response.result[0].grade,
"isRegistered": $response.result[0].is_registered
})
$util.toJson($result)
#else
## Error - send the proper error message
$utils.appendError($ctx.result.body, $ctx.result.statusCode)
#end
What I have currently worked but only returns one team. My question is how can I get the GraphQL query to return an array of teams by getting all of the items from the result array in the JSON file?
I'm not sure if I really understood what you are trying to achieve but here it goes:
If you want to return an array of teams instead of a single team, you have to modify your query in the schema as follow:
type Query {
getTeams: [Team]
}
Now on the response mapping you can map directly the response to your array:
#if($ctx.result.statusCode == 200)
## If response is 200, return the body.
$util.toJson($util.parseJson($ctx.result.body).result)
#else
## If response is not 200, append the response to error block.
$utils.appendError($ctx.result.body, "$ctx.result.statusCode")
#end
At this point I noticed that you renamed the fields in your schema so it does not match anymore; for example your json is returning team_name but your graphQL schema is expecting teamName.
What I would do is to modify the schema to match with the JSON as follow:
type Team {
number: String
program: String
team_name: String
robot_name: String
organisation: String
city: String
region: String
country: String
grade: String
is_registered: Int
}
And then use alias in the query to return the fields with the expected name, for example:
query{
getTeams{
number: number
teamName:team_name
robotName: robot_name
organization: organisation
}
}
That will produce the output that I think that you are expecting:
{
"data": {
"getTeams": [
{
"number": "123",
"teamName": "team name",
"robotName": "robot name",
"organization": "team organization"
},
....
Related
I am using Magento GraphQL api in my project. To create a customer address I used createCustomerAddress mutation(createCustomerAddress).
Below is the mutation that I have called to create the customer address :
mutation createAddress {
createCustomerAddress(
input: {
firstname: "test"
lastname: "name"
company: "networld"
telephone: "1231231231"
street: ["test address line 1", "test address line 2"]
city: "Rajkot"
region: { region:"Gujarat", region_code: "GJ" }
postcode: "360001"
country_code: IN
}
) {
id
prefix
firstname
lastname
middlename
city
company
country_code
default_billing
default_shipping
postcode
region {
region
region_code
}
street
suffix
telephone
vat_id
}
}
This is working properly and returning me the result as below :
{
"data": {
"createCustomerAddress": {
"id": 44,
"prefix": null,
"firstname": "test",
"lastname": "name",
"middlename": null,
"city": "Rajkot",
"company": "networld",
"country_code": "IN",
"default_billing": false,
"default_shipping": false,
"postcode": "360001",
"region": {
"region": "Gujarat",
"region_code": "GJ"
},
"street": [
"test address line 1",
"test address line 2"
],
"suffix": null,
"telephone": "1231231231",
"vat_id": null
}
}
}
But, now when I query to get the customer address, it returning wrong region_code.
Here is the query I written to get the customer address :
query{
customer{
addresses{
id
firstname
lastname
street
city
region{
region
region_code
}
country_code
postcode
telephone
}
}
}
Result :
{
"data": {
"customer": {
"addresses": [
{
"id": 44,
"firstname": "test",
"lastname": "name",
"street": [
"test address line 1",
"test address line 2"
],
"city": "Rajkot",
"region": {
"region": "Gujarat",
"region_code": "Gujarat"
},
"country_code": "IN",
"postcode": "360001",
"telephone": "1231231231"
}
]
}
}
}
As you can see, region_code in this query result and region_code in mutation result was different. Query not returning region_code that generated from the mutation. Mutation generated region_code was GJ and query returned region_code was Gujarat.
Can anyone help me why this is happening ? How to solve it ?
I just stumbled upon this bug myself in Magento 2.3.4 and it looks like it's buggy with the region_code. There's a workaround for this, try to send the region_id instead of region_code, like this:
mutation {
createCustomerAddress(input: {
region: {
region: "Vendeé"
region_id: 799
}
country_code: FR
street: ["123 Main Street"]
telephone: "7777777777"
postcode: "77777"
city: "Phoenix"
firstname: "Bob"
lastname: "Loblaw"
default_shipping: true
default_billing: false
}) {
id
region {
region
region_code
}
country_code
street
telephone
postcode
city
default_shipping
default_billing
}
}
After this, if you retrieve the region_code, it will show fine. It looks like it has problems identifying the region by the region_code.
I have a problem where I am unable to retrieve values from the message card text input in Microsoft Teams, but the same JSON template actually works on Message Card Playground.
A brief overview of my Microsoft Card implementation. I have to use MessageCard as I am using connectors (incoming webhook) to send a card to Microsoft Teams. Thus, the input value substitution syntax is referred from Message Card Reference - {{<id of input>.value}}. I am not getting any value using this syntax in my Message Card when I am in TEAMS. E.g. User filled in a textbox, and the value is not being captured or cannot be retrieved with this syntax.
The card that I used is as followed:
{
"#type": "MessageCard",
"#context": "http://schema.org/extensions",
"themeColor": "0076D7",
"summary": "{{ctx.monitor.name}}",
"sections": [
{
"activityTitle": "![TestImage](https://47a92947.ngrok.io/Content/Images/default.png){{ctx.monitor.name}}",
"activitySubtitle": "Alert",
"activityImage": "https://teamsnodesample.azurewebsites.net/static/img/image5.png",
"facts": [
{
"name": "Assigned to",
"value": "Sam"
}
],
"markdown": true
}
],
"potentialAction": [
{
"#type": "ActionCard",
"name": "Add a comment",
"inputs": [
{
"#type": "TextInput",
"id": "comment",
"title": "Enter your comment",
"isMultiline": true
}
],
"actions": [
{
"#type": "HttpPOST",
"name": "OK",
"target": "https://webhook.site/ab592c11-4590-438d-90c2-57bc4bb4aa8a?serviceToken=d2l0cy1zYW06MXFhekBXU1g%3D",
"body": "{{comment.value}}"
}
]
}
]
}
Note: You can see there is "summary": "{{ctx.monitor.name}}", it is a property from Kibana (a data visualization tool). This value works, but it is not our focus right here. My problem is I cannot get any value from {{comment.value}}, it is an empty string.
My questions are:
Is this the limitation coming from MSFT Teams itself?
#csamleong could you please replace the "body": "comment ={{comment.value}}" so you will receive the comment value:
card json:
{
"summary": "Card \"Test card\"",
"themeColor": "0078D7",
"#type": "MessageCard",
"#context": "http://schema.org/extensions",
"themeColor": "0076D7",
"summary": "{{ctx.monitor.name}}",
"sections": [
{
"activityTitle": "![TestImage](https://47a92947.ngrok.io/Content/Images/default.png){{ctx.monitor.name}}",
"activitySubtitle": "Alert",
"activityImage": "https://teamsnodesample.azurewebsites.net/static/img/image5.png",
"facts": [
{
"name": "Assigned to",
"value": "Sam"
}
],
"markdown": true
}
],
"potentialAction": [
{
"#type": "ActionCard",
"name": "Add a comment",
"inputs": [
{
"#type": "TextInput",
"id": "comment",
"title": "Enter your comment",
"isMultiline": true
}
],
"actions": [
{
"#type": "HttpPOST",
"name": "OK",
"target": "https://daf47bb241c6.ngrok.io//weatherforecast/Configure/Comment",
"body": "comment={{comment.value}}"
}
]
}
]
}
Post Method:
[HttpPost]
[Route("Configure/Comment")]
public async Task<ActionResult> Comment()
{
string bodyStr;
using (var reader = new StreamReader(this.Request.Body, Encoding.UTF8, true, 1024, true))
{
bodyStr = await reader.ReadToEndAsync();
}
string comment = string.IsNullOrWhiteSpace(bodyStr) ? string.Empty : bodyStr.Split('=')[1];
Response.Headers.Add("CARD-UPDATE-IN-BODY", "true");
return null;
}
You will receive value in the comment varable.
I have a column name payment_data in my database table. data was stored as JSON array using json_encode().
I want to parse the JSON array in my datatable. But I fail.
this is the data that I want to access
payment_data column
I updated my data structure to this.
Array[2][
{
"po_id": 43,
"full_name": "Dawn Zulita",
"level": "organization",
"payment_data": {
"product_id": "184",
"product_name": "Grading Org Product",
"student_name": {
"0": "Eloise Phillips",
"1": "Lara vel"
}
},
"date_purchase": "2018-08-10 10:38:08"
},
{
"po_id": 42,
"full_name": "QWerty You",
"level": "school",
"payment_data": {
"product_id": 185,
"product_name": "School Owner Manual Payment School Owner Manual Payment",
"student_name": {
"0": "Jai Who",
"1": "Charlie Putt",
"2": "Kevin Young"
}
},
"date_purchase": "2018-08-09 14:53:35"
}
]
I can now access the payment_data.product_name,
{
data: 'payment_data.product_id'
},
{
data: 'payment_data.product_name'
},
but the problem is I cannot access payment_data.student_name
error return Undefined index
{
data: 'payment_data.student_name'
},
So I used an account and message my skype bot.
I get this json response on my endpoint url. I sent a text "add"
{"text":"add"
,"type":"message"
,"timestamp":"2017-01-13T15:38:32.242Z"
,"id":"1234567"
,"channelId":"skype"
,"serviceUrl":"https:\/\/smba.trafficmanager.net\/apis\/"
,"from":{"id":"29:yyyy","name":"Real Person"}
,"conversation":{"id":"29:yyyy"}
,"recipient":{"id":"28:xxxx","name":"Skype Test"}
}
When I return a reponse to this url using yyyy as conversationId
POST /v3/directline/conversations/{conversationId}/activities
https://directline.botframework.com/v3/directline/conversations/yyyy/activities
with this parameter:
{
"type": "message",
"text": "registration",
"from": {
"id":"xxxx","name":"Skype Test"
},
"recipient": {"id":"yyyy","name":"Real Person"}
}
I get an unknown conversation response. Not sure what is wrong.
Your Response should look like:
{
"type": "message",
"from": {
"id": "recipient_id",
"name": "recipient_name"
},
"conversation": {
"id": "conversation_id",
"name": "conversation_name(if available)"
},
"recipient": {
"id": "from_id",
"name": "from_name"
},
"text": "response_text",
"replyToId": "activity_id"
}
Hope that Help :)
I am developing a platform with JSON API using Python Flask. In some cases I need to join three tables. How to join tables with a array of IDs gave me some guidance but I need a solution beyond it.
Let's assume we have three tables for a messaging app.
Accounts
Conversations
Messages
Message Readers
Accounts table snippet
{
"id": "account111",
"name": "John Doe",
},
Conversations table snippet
{
"id": "conversation111",
"to": ["account111", "account222", "account333"], // accounts who are participating the conversation
"subject": "RethinkDB",
}
Messages table snippet
{
"id": "message111",
"text": "I love how RethinkDB does joins.",
"from": "account111", // accounts who is the author of the message
"conversation": "conversation111"
}
Message Readers table snippet
{
"id": "messagereader111",
"message": "message111",
"reader": "account111",
}
My question is "What's the magic query to get the document below when I receive a get request on an account document with id="account111"?"
{
"id": "account111",
"name": John Doe,
"conversations": [ // 2) Join account table with conversations
{
"id": "conversation111",
"name": "RethinkDB",
"to": [ // 3) Join conversations table with accounts
{
"id": "account111",
"name": "John Doe",
},
{
"id": "account222",
"name": "Bobby Zoya",
},
{
"id": "account333",
"name": "Maya Bee",
},
]
"messages": [ // 4) Join conversations with messages
{
"id": "message111",
"text": "I love how RethinkDB does joins.",
"from": { // 5) Join messages with accounts
"id": "account111",
"name": "John Doe",
},
"message_readers": [
{
"name": "John Doe",
"id": "account111",
}
],
},
],
},
],
}
Any guidance or advice would be fantastic. JavaScript or Python code would be awesome.
I had a hard time understanding what you want (you have multiple documents with the id 111), but I think this is the query you are looking for
Python query:
r.table("accounts").map(lambda account:
account.merge({
"conversations": r.table("conversations").filter(lambda conversation:
conversation["to"].contains(account["id"])).coerce_to("array").map(lambda conversation:
conversation.merge({
"to": conversation["to"].map(lambda account:
r.table("accounts").get(account)).pluck(["id", "name",]).coerce_to("array"),
"messages": r.table("messages").filter(lambda message:
message["conversation"] == conversation["id"]).coerce_to("array").map(lambda message:
message.merge({
"from": r.table("accounts").get(message["from"]).pluck(["id", "name",]),
"readers": r.table("message_readers").filter(lambda message_reader:
message["id"] == message_reader["message"]).coerce_to("array").order_by(r.desc("created_on")),
})).order_by(r.desc("created_on"))
})).order_by(r.desc("modified_on"))
})).order_by("id").run(db_connection)