Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Given a set of GraphQL variable types, is it possible to use the client schema to create a map of all valid values for each type in the set

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.

like image 302
wvandaal Avatar asked Jan 24 '17 23:01

wvandaal


People also ask

Is it possible to use variables in a GraphQL query?

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.

What other types can be used in a GraphQL schema?

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.

Is it possible to use inheritance with GraphQL input 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).

When using GraphQL which defines a set of types?

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.


1 Answers

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"
        }
      ]
    }
  }
}
like image 141
marktani Avatar answered Oct 05 '22 13:10

marktani