I am building a GraphQL layer on top of an existing REST api. In the schema, I have a key, "steps" that returns an array of differently shaped objects, of which one is an array of objects, like this
"steps": [
{
"created_at": "a string",
"created_by": "a string",
"event_name": "a string",
"reason": "failed",
},
{
"components": [
{
"condition": false,
"data": {
"address": "foo",
"address_city": "bar",
"address_state": "baz",
},
}
],
I am able to get the first object returning, but am struggling with how to type this in order to access the components array, which is coming back as null in the graphQL playground. In my typeDefs, I am typing like this
type Steps {
event_name: String
reason: String
created_at: String
created_by: String
step_date: String
step_name: String
components: [Components]
}
I understand WHY it's failing, as components is nested one extra level in it's own object, but I'm not sure what the fix is, since I can't just use a nested object in the type declaration and the individual objects in the steps array don't have keys.
Reading around the GraphQL docs, I can't see anything that explains how to type this sort of structure?
Related
Hello all,
I have a json that looks like something below ...
[
{
"Delay": 0.031247,
"Index": {
"Currency": "USD",
"Valoren": "998434",
},
"IdentifierType": "Symbol",
"Identifier": "SPX.INDCBSX",
},
{
"Delay": 0,
"Index": {
"Currency": "USD",
"Valoren": "13190963",
},
"IdentifierType": "Symbol",
"Identifier": "SPDVXT.INDCBSX",
}
]
I want to unmarshall it in such a way that I should be able to store it as map[string]interface{}, where key is value of "Identifier" value. For example if I write data["SPX.INDCBSX"] then I should get complete array element data. Of course I can unmarshall it by creating similar structure and iterating over it and creating map of this type but it is more time consuming operation.
There's no magic way to tell encoding/json to unmarshal this data for you while selecting a custom field as the key of the map. You have several options:
Implement the json.Unmarshaler interface; see the docs of encoding/json and for example this answer.
Unmarshal the data into []map[string]interface{} and then rejigger it into the format you want in a separate loop.
Use something like JSON-to-Go to create the structures representing your data and then doing (2) is much simpler, but this only works of the structure of your JSON data is rigid.
This post can also be helful. And this one.
I'm trying to avoid iterating through this array, but I imagine that is the only way to handle this. Just seeing if there is a way to directly query this value in the array from the Web API URI.
This is the URI example:
https://example.crm.dynamics.com/api/data/v9.0/GlobalOptionSetDefinitions(f4a9de67-1d00-ea11-a811-000d3a33f702)
And this is an example of the response:
{
"#odata.context": "https://example.crm.dynamics.com/api/data/v9.0/$metadata#GlobalOptionSetDefinitions/Microsoft.Dynamics.CRM.OptionSetMetadata/$entity",
"MetadataId": "f4a9de67-1d00-ea11-a811-000d3a33f702",
"Options": [
{
"Value": 799680006,
"Color": "#0000ff",
"IsManaged": false,
"ExternalValue": "",
"ParentValues": [],
"MetadataId": null,
"HasChanged": null,
"Label": {
"LocalizedLabels": [
{
"Label": "ABC123",
"LanguageCode": 1033,
"IsManaged": false,
"MetadataId": "b4eb2c69-b500-ea11-a811-000d3a33fe19",
"HasChanged": null
}
],
"UserLocalizedLabel": {
"Label": "ABC123",
"LanguageCode": 1033,
"IsManaged": false,
"MetadataId": "b4eb2c69-b500-ea11-a811-000d3a33fe19",
"HasChanged": null
}
}
}
]
}
Basically, I have the "Value": 799680006 which is what I want to somehow add to the URI query parameters, so that I can ultimately get "Label": "ABC123".
Any suggestions or is iterating through the array of objects with if Value = x really the only option?
Let me clarify two things:
Querying metadata like you are using GlobalOptionSetDefinitions to get all the localized labels if you have multiple language packs or for verifying customizations or for Devops deployment purpose is one thing
Getting the label for the selected picklist value in one of the transaction database record is another purpose
If you simply need for second purpose, you can get it by selecting the Formatted value, after adding a header in web api request. Read more in my SO answer
Another way to inspect the label is using stringmap entity.
https://crmdev.crm.dynamics.com/api/data/v9.1/stringmaps?$filter=objecttypecode eq 'account' and attributename eq 'accountclassificationcode' and attributevalue eq 1
All:
I am trying to understand the relationship between Entity Array and Object:
Are they just different format to describe diff structure of data? Or Entity is quite diff from the rest two?
The normalized data result has a structure like {result:,entities:}, are the data structures only defined with schema.Entity put inside entities or so can schema.Array and Object? When I define a schema only use Object and Array, it seems nothing put in entities, I am not sure if it is my schema def fault or this is how normalizr work?
If only schema.Entity() defined data can put into entities, then how can I put an data array into it, something like {0:.., 1:..,2:,}?
For exmaple, I have data like:
var data = [
{
id:"0",
items:[
{
id: "0",
data: {name:"data-0-0"}
},
{
id: "1",
data: {name:"data-0-1"}
}
]
},
{
id:"1",
items:[
{
id: "0",
data: {name:"data-1-0"}
},
{
id: "1",
data: {name:"data-1-1"}
}
]
}
]
const normalizedData = normalize(data, [{items:[{data:{}}]}]);
And the normalized data is like:
{
"entities": {},
"result": {
"0": {
"id": "0",
"items": [
{
"id": "0",
"data": {
"name": "data-1-0"
}
}
]
}
}
}
Thanks
Question: Are they just different format to describe diff structure of data? Or Entity is quite diff from the rest two?
Answer: Yes. An Entity is a singular object that has a unique identifier associated with it. Array and Object are more generic structures that can't be uniquely identified. In your case, it looks like you only need to use Array and Entity for the data you're describing.
Question: Are the data structures only defined with schema? Entity put inside entities?
Answer: Yes.
I was wondering if there is a way via the Ruby API doc to modify a Topic for the following class:
Google::Apis::ClassroomV1::CourseWork
Topics were introduced in August 2016 as far as I can tell as a way for teachers to organize their stream:
https://support.google.com/edu/classroom/answer/6149237?hl=en
Does anyone know of a way? I'm okay with making a REST call as well if necessary.
Thanks!
Looking at the JSON response https://developers.google.com/classroom/reference/rest/v1/courses.courseWork.
It doesnt look like they have added the functionality to update a Topic through courses.CourseWork
JSON representation
{
"courseId": string,
"id": string,
"title": string,
"description": string,
"materials": [
{
object(Material)
}
],
"state": enum(CourseWorkState),
"alternateLink": string,
"creationTime": string,
"updateTime": string,
"dueDate": {
object(Date)
},
"dueTime": {
object(TimeOfDay)
},
"maxPoints": number,
"workType": enum(CourseWorkType),
"associatedWithDeveloper": boolean,
"submissionModificationMode": enum(SubmissionModificationMode),
// Union field details can be only one of the following:
"assignment": {
object(Assignment)
},
"multipleChoiceQuestion": {
object(MultipleChoiceQuestion)
},
// End of list of possible types for union field details.
}
I'm trying to read individual value from be json array object to display in the page. I have tried with below code but couldn't make it. Please advise what am i doing wrong here.
Apperciate your help.
You can get the length of a JavaScript array via its property length. To access the array Reference in your object, you can use dot notation.
In combination, the following should do what you expect:
var obj = {
"Reference": [
{
"name": "xxxxxxxx",
"typeReference": {
"articulation": 0,
"locked": false,
"createdBy": {
"userName": "System",
},
"lastModifiedBy": {
"userName": "System",
},
"lastModified": 1391084398660,
"createdOn": 1391084398647,
"isSystem": true
},
...
},
...
]
};
console.log(obj.Reference.length);
In case you are actually dealing with a JSON string, not a JavaScript object, you will need to parse it first via JSON.parse().
You get the length of an array by simply access the length attribute.
For example [0,1,2,3].length === 4.
If you just want to loop through the array, use forEach or map instead of a for loop. It's safer, cleaner, less hassle and you don't meed to know the length.
E.g.
[0,1,2,3].forEach(num => console.log(num))