d3 nesting two value as key value pairs - d3.js

I have a csv file which I converted to an object:
[object Object] {
key: "2020-07-09",
values: [[object Object] {
cases: "49174",
date: "2020-07-09",
deaths: "1068",
fips: "01",
state: "Alabama"
}
}
I want an output of this object as a nest, something like this:
key: "2020-07-09",
perstate: {
"Alabama" : "49174"
}
I am not aware how to convert two values into a key value pair

Say the object is in an array, called data.
data.map(d => ({
key: d.key,
perstate: {
[d.values.state]: d.values.cases
}
}))
We use an ES6 arrow function for conciseness, putting the object literal in paranteses to indicate that the function returns an object.
To compute the key, we use ES6 computed property names. This allows us to get the value at values.state and use it as the key.

Related

AWS CDK - Can I Pass LambdaInvoke's InputPath Prop Multiple JSON Paths?

I find new LambdaInvoke requires the inputPath to be a string, so whether I need to pass the event an object from multiple places, I need a Pass state to make it happen.
this.organiseTheArrayAndId = new Pass(this, "Organise the Array and Id Together In One Object", {
parameters: {
"array": JsonPath.stringAt("$.productIdsArray"),
"value": JsonPath.numberAt("$.productId.id")
},
resultPath: "$.organiseTheArrayAndId",
});
this.augmentProductIdArray = new LambdaInvoke(this, "Add the Product ID To The Array", {
lambdaFunction: lambdaFunctionLocation,
inputPath: "$.organiseTheArrayAndId",
});
Is there a more efficient way?
The LambdaInvoke task construct's payload property lets you customize the input your function receives when invoked:
payload: sfn.TaskInput.fromObject({
array: JsonPath.stringAt("$.productIdsArray"),
value: JsonPath.numberAt("$.productId.id")
}),

How can I add 2 types either an array of strings or an object to the data?

I have this mutation function where I can add either array of String or Object in the data arguments.
This is a request with data type value Object.
mutation {
reading(id: 257, data: [{ id: 1, timestamp: "1654831128"}]){
id
name
}
}
This is a request with data type value String
mutation {
reading(id: 257, data: [["ace", "red"], ["rock", "stone"]]){
id
name
}
}
In one mutation, there are 2 different types that I will send to the server.
In my graphQL file code
type Mutation {
reading(
id: Int
data: [Time | String] <----
): [Device!]!
}
In my code with an arrow above. How can I add 2 types either an array of strings or an object to the data?
In the documentation, I try union but I think it only applies to the results.

GraphQL: how to have it return a flexible, dynamic array, depending on what the marketeer filled in? [duplicate]

We are in the situation that the response of our GraphQL Query has to return some dynamic properties of an object. In our case we are not able to predefine all possible properties - so it has to be dynamic.
As we think there are two options to solve it.
const MyType = new GraphQLObjectType({
name: 'SomeType',
fields: {
name: {
type: GraphQLString,
},
elements: {
/*
THIS is our special field which needs to return a dynamic object
*/
},
// ...
},
});
As you can see in the example code is element the property which has to return an object. A response when resolve this could be:
{
name: 'some name',
elements: {
an_unkonwn_key: {
some_nested_field: {
some_other: true,
},
},
another_unknown_prop: 'foo',
},
}
1) Return a "Any-Object"
We could just return any object - so GraphQL do not need to know which fields the Object has. When we tell GraphQL that the field is the type GraphQlObjectType it needs to define fields. Because of this it seems not to be possible to tell GraphQL that someone is just an Object.
Fo this we have changed it like this:
elements: {
type: new GraphQLObjectType({ name: 'elements' });
},
2) We could define dynamic field properties because its in an function
When we define fields as an function we could define our object dynamically. But the field function would need some information (in our case information which would be passed to elements) and we would need to access them to build the field object.
Example:
const MyType = new GraphQLObjectType({
name: 'SomeType',
fields: {
name: {
type: GraphQLString,
},
elements: {
type: new GraphQLObjectType({
name: 'elements',
fields: (argsFromElements) => {
// here we can now access keys from "args"
const fields = {};
argsFromElements.keys.forEach((key) => {
// some logic here ..
fields[someGeneratedProperty] = someGeneratedGraphQLType;
});
return fields;
},
}),
args: {
keys: {
type: new GraphQLList(GraphQLString),
},
},
},
// ...
},
});
This could work but the question would be if there is a way to pass the args and/or resolve object to the fields.
Question
So our question is now: Which way would be recommended in our case in GraphQL and is solution 1 or 2 possible ? Maybe there is another solution ?
Edit
Solution 1 would work when using the ScalarType. Example:
type: new GraphQLScalarType({
name: 'elements',
serialize(value) {
return value;
},
}),
I am not sure if this is a recommended way to solve our situation.
Neither option is really viable:
GraphQL is strongly typed. GraphQL.js doesn't support some kind of any field, and all types defined in your schema must have fields defined. If you look in the docs, fields is a required -- if you try to leave it out, you'll hit an error.
Args are used to resolve queries on a per-request basis. There's no way you can pass them back to your schema. You schema is supposed to be static.
As you suggest, it's possible to accomplish what you're trying to do by rolling your own customer Scalar. I think a simpler solution would be to just use JSON -- you can import a custom scalar for it like this one. Then just have your elements field resolve to a JSON object or array containing the dynamic fields. You could also manipulate the JSON object inside the resolver based on arguments if necessary (if you wanted to limit the fields returned to a subset as defined in the args, for example).
Word of warning: The issue with utilizing JSON, or any custom scalar that includes nested data, is that you're limiting the client's flexibility in requesting what it actually needs. It also results in less helpful errors on the client side -- I'd much rather be told that the field I requested doesn't exist or returned null when I make the request than to find out later down the line the JSON blob I got didn't include a field I expected it to.
One more possible solution could be to declare any such dynamic object as a string. And then pass a stringified version of the object as value to that object from your resolver functions. And then eventually you can parse that string to JSON again to make it again an object on the client side.
I'm not sure if its recommended way or not but I tried to make it work with this approach and it did work smoothly, so I'm sharing it here.

Using Observable map operator to flatted an array

So let's say I have a function that returns an Observable of the object ObjectReturned below:
interface ObjectReturned {
id: string,
information: info[],
anotherObj: AnotherObj[]
}
interface AnotherObj {
information: info[]
}
interface info {
name: string
}
Given that ObjectReturned.info[] and AnotherObj.info[] both always contain only one element each, how can I use the map operator of Observable to "flatten" the info array above so that I can access the returned result (Observable<ObjectReturned>) using objectReturned.name and objectReturned.anotherObj.name directly?
objectReturned.map( obj => Object.assign(obj, {name: information[0].name});
this gives
{
id: string,
name: string,
anotherObj: AnotherObj[]
}
You can further apply the same to anotherObj too. let me know if this is what you are looking for.

How to pass GraphQLEnumType in mutation as a string value

I have following GraphQLEnumType
const PackagingUnitType = new GraphQLEnumType({
name: 'PackagingUnit',
description: '',
values: {
Carton: { value: 'Carton' },
Stack: { value: 'Stack' },
},
});
On a mutation query if i pass PackagingUnit value as Carton (without quotes) it works. But If i pass as string 'Carton' it throws following error
In field "packagingUnit": Expected type "PackagingUnit", found "Carton"
Is there a way to pass the enum as a string from client side?
EDIT:
I have a form in my front end, where i collect the PackagingUnit type from user along with other fields. PackagingUnit type is represented as a string in front end (not the graphQL Enum type), Since i am not using Apollo Client or Relay, i had to construct the graphQL query string by myself.
Right now i am collecting the form data as JSON and then do JSON.stringify() and then remove the double Quotes on properties to get the final graphQL compatible query.
eg. my form has two fields packagingUnitType (An GraphQLEnumType) and noOfUnits (An GraphQLFloat)
my json structure is
{
packagingUnitType: "Carton",
noOfUnits: 10
}
convert this to string using JSON.stringify()
'{"packagingUnitType":"Carton","noOfUnits":10}'
And then remove the doubleQuotes on properties
{packagingUnitType:"Carton",noOfUnits:10}
Now this can be passed to the graphQL server like
newStackMutation(input: {packagingUnitType:"Carton", noOfUnits:10}) {
...
}
This works only if the enum value does not have any quotes. like below
newStackMutation(input: {packagingUnitType:Carton, noOfUnits:10}) {
...
}
Thanks
GraphQL queries can accept variables. This will be easier for you, as you will not have to do some tricky string-concatenation.
I suppose you use GraphQLHttp - or similar. To send your variables along the query, send a JSON body with a query key and a variables key:
// JSON body
{
"query": "query MyQuery { ... }",
"variables": {
"variable1": ...,
}
}
The query syntax is:
query MyMutation($input: NewStackMutationInput) {
newStackMutation(input: $input) {
...
}
}
And then, you can pass your variable as:
{
"input": {
"packagingUnitType": "Carton",
"noOfUnits": 10
}
}
GraphQL will understand packagingUnitType is an Enum type and will do the conversion for you.

Resources