Skip to content

Union input types #202

Closed
Closed
@calebmer

Description

@calebmer

Could we make union types of strictly input types an input type? Often I find places where having a union type as the argument to a GraphQL field could be useful. Especially for complex querying. For example an orderBy field could take a single ordering enum or an array of enums: orderBy: ID or orderBy: [FIRST_NAME, ID] with union input types.

Another example could be complex conditions. So for instance we could have the following (rough) type definition:

input PersonCondition {
  id: IntCondition
  firstName: StringCondition
  lastName: StringCondition
}

union IntCondition = Int | IntOperation | [IntOperation]

input IntOperation {
  operation: IntOperator
  value: Int!
}

enum IntOperator {
  EQUALS
  LESS_THAN
  GREATER_THAN
}

union StringCondition = String | StringOperation | [StringOperation]

input StringOperation {
  operation: StringOperator
  value: String!
}

enum StringOperator {
  EQUALS
  LESS_THAN
  GREATER_THAN
  SEARCH
}

Where here say the literal Int would be the same as an IntOperation with an operator of EQUALS. And the array of StringOperation could be joined together with ands when querying the database.


I don’t think union input types are useful for people who are handcrafting their schemas (although it may be), but union input types are incredibly useful for GraphQL as a service type products. That provide the user with a GraphQL schema and then allow that user to perform arbitrary queries against that schema. And here I don’t mean “arbitrary queries” in the sense that GraphQL allows the client to define the exact data it wants from the schema, but rather arbitrary queries to the backend. Whether that backend be a SQL database, NoSQL database, or something proprietary.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions