Title mostly says it all: I'm building a react / relay application which will allow the user to dynamically create charts at runtime displaying their various income streams over a specified time range. One feature of this chart is the ability of the user to specify the sampling interval of each income stream (e.g. YEAR
, QUARTER
, MONTH
, WEEK
, etc.) as a parameter of each stream.
These values are defined in the schema as a GraphQLInputObjectType
instance as follows:
enum timeSeriesIntervalEnum {
YEAR
QUARTER
MONTH
WEEK
}
On the client-side, I have react-relay
fragments defined of the following form:
fragment on BalanceSheet {
income {
# some income stream
afterTax {
values(interval: $samplingInterval)
dates(interval: $samplingInterval)
}
}
}
This variable value will be populated as part of dropdown menu in a separate component where each value in the dropdown should correspond to a valid timeSeriesIntervalEnum
value.
Although it would certainly be possible to simply hardcode these values in, the underlying API is still being changed quite often and I would like to reduce coupling and instead populate these fields dynamically by specifying the variable type for a given dropdown (e.g. timeSeriesIntervalEnum
) and then use the graphql client schema to parse the values and populate either a config json file (pre-runtime) or assign the values dynamically at runtime.
NOTE: I already do a bit of query string and fragment transpilation pre-start, so I'm not averse to creating json config files as part of this process if that is required.
Use GraphQL variables for type safety and self-documenting queries. GraphQL variables offer an extra layer of protection in your queries, namely type safety — meaning that a query will only accept dynamic variables of certain data types, such as String, Int (number), DateTime and so on.
The GraphQL schema language supports the scalar types of String , Int , Float , Boolean , and ID , so you can use these directly in the schema you pass to buildSchema . By default, every type is nullable - it's legitimate to return null as any of the scalar types.
No, the spec does not allow input types to implement interfaces. And GraphQL type system in general does not define any form of inheritance (the extends keyword adds fields to an existing type, and isn't for inheritance).
GraphQL comes with a set of default scalar types out of the box: Int : A signed 32‐bit integer. Float : A signed double-precision floating-point value. String : A UTF‐8 character sequence.
This can be done using an introspection query.
In your case this is one possible solution:
{
__type(name: "timeSeriesIntervalEnum") {
name
enumValues {
name
}
}
}
The response contains a list of all possible types:
{
"data": {
"__type": {
"name": "timeSeriesIntervalEnum",
"enumValues": [
{
"name": "YEAR"
},
{
"name": "QUARTER"
},
{
"name": "MONTH"
},
{
"name": "WEEK"
}
]
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With