Conditional schema types in Graphql (Gatsby.js)

Viewed 95

I am using an external API that injects data into graphql on gatsby build-time, our new system has a property called 'value' which depending on a relative property called 'type' the 'value' property will differ in it's schema type.

type > value

  1. 'text' -> String
  2. 'paragraph' -> String
  3. 'HTML' -> String
  4. 'image' -> File @fileByCustomDirective
  5. 'radio' -> String
  6. 'select' -> String
  7. 'checkbox' -> [String]

The above displays all the possible type strings and what the schema type for the 'value' property should be.

I'm unsure if this is possible, but within the createSchemaCustomization function how would I go about defining a type like this, and how would it then be queried in the frontend?

This is what my schema would look like, excluding the dynamic logic:

type customFields {
        _id: String
        value: String // this should be a array or File depending on the type value!
        type: String
        label: String
        id: String
},

This is a workaround I am thinking of applying, if I can't find a solution:

type customFields {
        _id: String
        value: String
        image: File @fileByCustomDirective // possible hack
        values: [String] // possible hack
        type: String
        label: String
        id: String
},

The only thing I can think of that I know would work right now is to force the API to send me a duplicate value for when type is 'image' by adding another property called 'image', same for 'values' when the type is 'checkbox', although this feels really hacky and I'd like everything to come through the same property if possible.

0 Answers
Related