'GraphQL Dynamic Subfields
We have the following GraphQL schema type
type Values {
somestring: String
someparent(id: ID!): ChildType
}
type ChildType {
dataset: JSON! #custom generic json scalar
}
and when we query for the data from the above type (lets say we passed ID of 8), we get the following.
{
"somestring": "string valueees",
"someparent": {
"dataset": {
"dynamicValue1": {
"staticKeyA": "some value",
"staticKeyE": "there value"
},
"dynamicValue2": {
"staticKeyA": "new value",
"staticKeyE": "abcd value"
}
}
}
}
In the above result only the keys dynamicValue1 and dynamicValue2 are dynamic, however everything is static. The dynamic keys changed based on the id passed to someparent.
In terms of typescript we would define the interface as
export interface DatasetInterface {
[dynamicValueId: string]: DynamicChildInterface;
}
export interface DynamicChildInterface {
staticKeyA: string
staticKeyE: string
}
What we want to do is without using JSON allow a user to query based on the subfields of the dynamicvalue key objects. However, we don't care about the name of dynamicValue1 and dynamicValue2, (its ok if it does not show up on autocomplete, or the schema). So we do not want to worry about
query {
Values {
someparent {
dataset {
# we don't care about dynamic value 1 and we don't need to autocomplete the user even
dynamicValue1 {
staticKeyA
}
}
}
}
}
Is the following assumption correct? From our understanding, the schema needs to be static, and since the dynamic key changes based on the id, there is no easy way to provide the schema definition whenever the id changes. Next grapqhl is strongly typed and every graphql type needs to have an explicit "name" defined in the schema. We also looked at custom scalars, but we need to be able to have subfields as well.
Is this even possible in graphql, if so how or what path do we need to take?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
