I'm fairly new to GraphQL. Previously I've used Firebase, and when I started learning, I've realised that there's no support for arrays. I couldn't understand why, but after reading this explanation of it, and started to think in that way, and to use it in practice, everything became easier and faster.
Check the numbers. Numbers of speed, and characters of code of each approach:
const obj = {
id: {
data: "foo"
}
};
const arr = [{
id: "id",
data: "foo"
}];
const required = "id";
console.time("Get data by key from object");
(() => obj[required])();
console.timeEnd("Get data by key from object");
console.time("Get data by finding key's value from array");
(() => arr.find(({ id }) => id === required))();
console.timeEnd("Get data by finding key's value from array");
console.time("Set data of object by key from object");
(() => obj[required].data = "bar")();
console.timeEnd("Set data of object by key from object");
console.time("Set data of object by finding key's value from array");
(() => arr.find(({ id }) => id === required).data = "bar")();
console.timeEnd("Set data of object by finding key's value from array");
Long story short, I'm learning GraphQL, and I haven't found a method which can be used with this approach.
For example in this data structure:
users: {
"firstUser": {
name: "first user's name"
},
"secondUser": {
name: "second user's name"
}
}
I'd need something like:
union Ids = String | [String!]
type User {
name: String
}
type Query {
users(id: Ids): {
# the [id] would mean that it's returning unknown, even multiple fields
[id]: User
}
}
# querying like
query Users (id: ["firstUser", "secondUser"]) {
name
}
So basically what I want to pass an array of ids, and the query would return exactly the same data structure as shown in the second snippet.
I'm very new to GraphQL, so every tip would be useful if I've made something wrong, or anything.