I am trying to report the status of a couple of devices via API and I am getting this error
Request payload:
{
"requestId":"3310672920401175639",
"agentUserId":"5d8f3dd42ce05140dc1c6a20",
"payload":{
"devices":{
"states":[
{
"5de28e041729ec0cb40ba906":{
"on":true
}
},
{
"5df49862f53ffa4c1452a448":{
"on":false,
"brightness":100
}
}
]
}
}
}
Response:
{
"error":{
"code":400,
"message":"Invalid JSON payload received. Unknown name "states" at 'payload.devices': Proto field is not repeating, cannot start list.",
"status":"INVALID_ARGUMENT",
"details":[
{
"#type":"type.googleapis.com/google.rpc.BadRequest",
"fieldViolations":[
{
"field":"payload.devices",
"description":"Invalid JSON payload received. Unknown name "states" at 'payload.devices': Proto field is not repeating, cannot start list."
}
]
}
]
}
}
can "states" hold more than one status of the device? or am I doing something wrong with this?
The states value in the payload should be an object with each unique device id as a key. It should not be returned with these wrapped into an array. So your request payload should look more like this:
{
"requestId":"3310672920401175639",
"agentUserId":"5d8f3dd42ce05140dc1c6a20",
"payload": {
"devices": {
"states": {
"5de28e041729ec0cb40ba906": {
"on": true
},
"5df49862f53ffa4c1452a448": {
"on": false,
"brightness": 100
}
}
}
}
}
Related
In my case, I have an app that users can subscript to some in-app events.
I want to call a mutation from one of my microservices and send several user ids as a list to the mutation, and then all clients that subscript to that mutation receive '[1]'.
schema
type Mutation {
setUsersAlarm(user_id: [Int]): UserIDList
}
type Subscription {
subscripesetUsersAlarm: UserIDList
#aws_subscribe(mutations: ["setUsersAlarm"])
}
type UserIDList {
id_list: [Int]
}
schema {
query: Query
mutation: Mutation
subscription: Subscription
}
Mutation Resolver
request template
{
"version": "2017-02-28",
"payload":$util.toJson($context.args["user_id"])
}
response template
{
"id_list":$util.toJson($context.result)
}
Subscription Resolver
request template
{
"version": "2017-02-28",
"payload": {
"hello": "local",
}
}
response template
$extensions.setSubscriptionFilter({
"filterGroup": [
{
"filters" : [
{
"fieldName" : "id_list",
"operator" : "contains",
#* I can get the value from cognito or from
user input arguments*#
"value" : 10
}
]
}
]
})
#set ($myList = [1])
#set( $ctx.result.id_list =$myList)
$util.toJson($ctx.result)
Query
subscription MySubscription {
subscripesetUsersAlarm {
id_list
}
}
mutation MyMutation {
setUserRefreshToken(user_id: [10, 12]) {
id_list
flg
}
}
Output of mutation
{
"data": {
"setUsersAlarm": {
"id_list": [
10,
12
]
}
}
}
Output of subscription
I want to receive the below result in subscription:
{
"data": {
"subscripesetUsersAlarm": {
"id_list": [1]
}
}
}
but I receive this:
{
"data": {
"subscripesetUsersAlarm": {
"id_list": [
10,
12
]
}
}
}
I want to customize the subscription response depending on my clients
I'm trying to query data within a Strapi Dynamic Zone in Gatsby. In the Graphql Playground I can get this to work, but using the same query in Gatsby I receive the following error in the terminal:
error Unknown type "ComponentTextArticleCopy" graphql/template-strings
And my query in article.js
export const query = graphql`
query ArticleTemplate($id: String!) {
strapiArticle(id: { eq: $id }) {
articleHeader {
articleTitle
articleSnippet
}
articleContent {
__typename
... on ComponentTextArticleCopy {
contentCopy
}
... on ComponentImageContentImg {
imgCaption
}
... on ComponentTextArticleQuote {
contentQuote
}
}
}
}
`
According to the Graphql docs, Inline Fragment would seem to be the right approach but clearly I've got something wrong somewhere.
The following query 'works' on Gatsby but tries to resolve for all components:
query MyQuery {
allStrapiArticle {
edges {
node {
__typename
articleContent {
contentCopy
contentQuote
}
}
}
}
}
{
"data": {
"allStrapiArticle": {
"edges": [
{
"node": {
"__typename": "StrapiArticle",
"articleContent": [
{
"contentCopy": null,
"contentQuote": null
},
{
"contentCopy": "What a great city Gothenburg is. We even took a trip out to the archipelago. ",
"contentQuote": null
},
{
"contentCopy": null,
"contentQuote": null
},
{
"contentCopy": null,
"contentQuote": "You must visit at have fika"
}
]
}
}
]
}
},
Deleting Cache folder and running again worked for me.
I want to convert the following nested object into hierarchical data structure
{
"AP":{
"districts":{
"Anantapur":{
"total":{
"confirmed":66593,
"deceased":587,
"recovered":65697
}
},
"Chittoor":{
...
}
}
},
"AR":{
"districts":{....}
}...so on
}
to
[
{
"name":"AP",
"children":[
{
"name":"Anantapur",
"children":[
{
"name":"confirmed",
"value":66593
},
{
"name":"deceased",
"value":587
},
{
"name":"recovered",
"value":65697
}
]
},
{
...
}
]
},...so on
]
How can this be done?...I tried using d3 nest but there is no common key value like
"state":"AP", "state":"AR".
Here "AP" and "AR" are keys themselves. Is there any other method of doing this?
You can use Object.keys(data).map(function(key) { to create an array with an item for every property in your objects. You can nest this approach to get to the desired outcome. Here's a manual solution for your data structure (you could use a recursive function for arbitrarily nested data with a slightly different output, but given the depth of the object and the structure of the output, I have not done that):
var data = { "State One":{
"districts":{
"Region A":{
"total":{
"confirmed":1,
"deceased":2,
"recovered":3
}
},
"Region B":{
"total":{
"confirmed":4,
"deceased":5,
"recovered":6
}
}
}
},
"State Two":{
"districts":{
"Region C":{
"total":{
"confirmed":7,
"deceased":8,
"recovered":9
}
}
}
}
}
var output = Object.keys(data).map(function(key) {
return {
name:key,
children: Object.keys(data[key]["districts"]).map(function(district) {
return {
name:district,
children: Object.keys(data[key]["districts"][district]["total"]).map(function(d) {
return { name: d, value:data[key]["districts"][district]["total"][d] }
})
}
})
}
})
console.log(output);
Are there any data types in GraphQL that can be used to describe a JSON Patch operation?
The structure of a JSON Patch operation is as follows.
{ "op": "add|replace|remove", "path": "/hello", "value": ["world"] }
Where value can be any valid JSON literal or object, such as.
"value": { "name": "michael" }
"value": "hello, world"
"value": 42
"value": ["a", "b", "c"]
op and path are always simple strings, value can be anything.
If you need to return JSON type then graphql have scalar JSON
which return any JSON type where you want to return it.
Here is schema
`
scalar JSON
type Response {
status: Boolean
message: String
data: JSON
}
type Test {
value: JSON
}
type Query {
getTest: Test
}
type Mutation {
//If you want to mutation then pass data as `JSON.stringify` or json formate
updateTest(value: JSON): Response
}
`
In resolver you can return anything in json format with key name "value"
//Query resolver
getTest: async (_, {}, { context }) => {
// return { "value": "hello, world" }
// return { "value": 42 }
// return { "value": ["a", "b", "c"] }
// return anything in json or string
return { "value": { "name": "michael" } }
},
// Mutation resolver
async updateTest(_, { value }, { }) {
// Pass data in JSON.stringify
// value : "\"hello, world\""
// value : "132456"
// value : "[\"a\", \"b\", \"c\"]"
// value : "{ \"name\": \"michael\" }"
console.log( JSON.parse(value) )
//JSON.parse return formated required data
return { status: true,
message: 'Test updated successfully!',
data: JSON.parse(value)
}
},
the only thing you need to specifically return "value" key to identify to get in query and mutation
Query
{
getTest {
value
}
}
// Which return
{
"data": {
"getTest": {
"value": {
"name": "michael"
}
}
}
}
Mutation
mutation {
updateTest(value: "{ \"name\": \"michael\" }") {
data
status
message
}
}
// Which return
{
"data": {
"updateTest": {
"data": null,
"status": true,
"message": "success"
}
}
}
I am validating model from another model like below
Model.addFavorite = function (data, callbackFn) {
if (data) {
var faviroteModel = this.app.models.Favorite;
var objFavorite = new faviroteModel(data);
objFavorite.isValid(function (isValid) {
if (isValid) {
callbackFn(null, objFavorite);
}
else {
callbackFn(objFavorite.errors);
}
});
}
else callbackFn("Post data required", {});
}
If i do this then I am getting error like below
{
"error": {
"statusCode": 500,
"t": [
"is not a valid date"
]
}
}
It should be with error message like below
{
"error": {
"statusCode": 422,
"name": "ValidationError",
"message": "The `Favorite` instance is not valid. Details: `t` is not a valid date (value: Invalid Date).",
"details": {
"context": "Favorite",
"codes": {
"t": [
"date"
]
},
"messages": {
"t": [
"is not a valid date"
]
}
}
}
}
Can anyone tell me what am i missing here.
How can i achieve this.
https://github.com/strongloop/loopback-datasource-juggler/blob/master/lib/validations.js#L843
You might run into situations where you need to raise a validation
error yourself, for example in a "before" hook or a custom model
method.
if (model.isValid()) {
return callback(null, { success: true });
}
// This line shows how to create a ValidationError
var err = new MyModel.ValidationError(model);
callback(err);
}