Skip to content

Dynamic Input Object Types #140

Closed
Closed

Description

@bringking

Is is possible in the spec to have "dynamic" object input types? The use case being something like these Sequelize where clauses -

{
  rank: {
    $or: {
      $lt: 1000,
      $eq: null
    }
  }
}

These clauses can be nested to join the logic. e.g the object can have $or, and that object can have n nested $or + n nested $and statements.

{
  rank: {
    $or: {
      $lt: 1000
    },
     $or: {
      $gt: 1500
    },
    $and: {
      $ne: 1500
    }
   ...
  }
}

Could these be modeled as an input type?

Activity

  1. sorenbs commented on Oct 7, 2016

    @sorenbs

    Fwiw, at Graphcool we generate a filter input type pretty much like you describe based on the fields in a given model.

    This allows arbitrary queries like this in a typed system (and auto-complete in GraphiQL):

    allArtists(filter:{OR:[{name:"Madonna"}, {AND:[{name_contains:"donna"}, {name_not:"Madonna"}]}]}){name}
    

    We are really happy with this approach.

  2. akre54 commented on Oct 7, 2016

    @akre54

    You could try something like https://github.com/taion/graphql-type-json. Basic gist is to create a new GraphQLScalarType and use the serialize and parse methods.

  3. jodinathan commented on Oct 17, 2019

    @jodinathan

    Fwiw, at Graphcool we generate a filter input type pretty much like you describe based on the fields in a given model.

    This allows arbitrary queries like this in a typed system (and auto-complete in GraphiQL):

    allArtists(filter:{OR:[{name:"Madonna"}, {AND:[{name_contains:"donna"}, {name_not:"Madonna"}]}]}){name}
    

    We are really happy with this approach.

    but this can make your type list increase dramatically, doesn't it?
    dunno if that is a problem thought

  4. jodinathan commented on Oct 18, 2019

    @jodinathan

    Fwiw, at Graphcool we generate a filter input type pretty much like you describe based on the fields in a given model.

    This allows arbitrary queries like this in a typed system (and auto-complete in GraphiQL):

    allArtists(filter:{OR:[{name:"Madonna"}, {AND:[{name_contains:"donna"}, {name_not:"Madonna"}]}]}){name}
    

    We are really happy with this approach.

    Also, from your example, you can't have two blocks of logic of same type, ie:

    AND (
        foo = 1,
        OR (
          bar = 2,
          // below we can have either a block where zas is 3 and yus is 4 or the opposite
          // thus having two times the AND block
          AND (
            zas = 3,
            yus = 4
          ),
          AND (
            zas = 4,
            yus = 3
          )
        )
      )
    
  5. IvanGoncharov commented on Oct 21, 2019

    @IvanGoncharov
    Member

    @jodinathan Most of the serialization formats don't support duplicating keys including JSON and input types should be serializable in order to be passed as query parameters.
    And I don't think it could be addressed without changing how input types are mapped to JSON.

  6. benjie commented on Oct 23, 2019

    @benjie
    Member

    Also, from your example, you can't have two blocks of logic of same type, ie ...

    You can, you just wrap each layer in an input object:

    {allArtists(filter: {
      AND: [
        {foo: 1},
        {OR: [
          {bar: 3},
          {
            AND: [
              {yas: 3},
              {yus: 4}
            ]
          },
          {
            AND: [
              {yas: 4},
              {yus: 3}
            ]
          }
        ]}
      ]
    })}

    (this could be expressed much simpler, but I tried to reflect your structure).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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