Giter Site home page Giter Site logo

hai's People

Contributors

basicbrogrammer avatar markjessop avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

markjessop

hai's Issues

GQL - Allow arguments to be required

Currently, no arguments are required in a model's graphql schema. This is fine, because validation can be added to the model file which would prevent an instance of that model from being created with invalid attributes.

It would be nice however if we could parse the validations on a model and set the required attribute on the GQL argument to be required if a presence validation is found

Error when creating object that doesn't contain any attributes

There seems to be an issue when attempting to create an object through a mutation when the model was created with only the default attributes given to a model in Rails (id, created_at, updated_at).

This seems to happen because we are early returning when cycling through the model attributes when it encounters the above attributes. However if the model only has these, the attr variable becomes [] and we receive the below error

model.attribute_types.each do |attr, type|
next if %w[id created_at updated_at].include?(attr)
klass.argument(
attr,
Hai::GraphQL::TYPE_CAST[type.class] ||
Hai::GraphQL::TYPE_CAST[type.class.superclass],
required: false
)
end
klass.field(:result, ::Types.const_get("#{model}Type"))
klass.define_method(:resolve) do |args|
Hai::Create.new(model, context).execute(**args)
end
Hai::GraphQL::Types.const_set("Create#{model}", klass)
end

Below is the mutation along with the stack trace

mutation {
  createTask(input: {}) {
    result {
      id
    }
  }
}
{
  "errors": [
    {
      "message": "wrong number of arguments (given 0, expected 1)",
      "backtrace": [
        "/usr/local/rvm/gems/ruby-3.1.3/gems/hai-0.0.4/lib/hai/graphql/create_mutations.rb:29:in `block in define_resolver'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema/resolver.rb:107:in `public_send'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema/resolver.rb:107:in `block (3 levels) in resolve_with_support'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema.rb:1056:in `after_lazy'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema/resolver.rb:93:in `block (2 levels) in resolve_with_support'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema.rb:1056:in `after_lazy'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema/resolver.rb:84:in `block in resolve_with_support'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema.rb:1056:in `after_lazy'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema/resolver.rb:73:in `resolve_with_support'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema/relay_classic_mutation.rb:60:in `resolve_with_support'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema/field.rb:686:in `public_send'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema/field.rb:686:in `block (2 levels) in resolve'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema/field.rb:807:in `with_extensions'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema/field.rb:657:in `block in resolve'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema.rb:1056:in `after_lazy'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema/field.rb:655:in `resolve'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/execution/interpreter/runtime.rb:511:in `block (3 levels) in evaluate_selection_with_args'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/tracing.rb:65:in `trace'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/execution/interpreter/runtime.rb:510:in `block (2 levels) in evaluate_selection_with_args'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/execution/interpreter/runtime.rb:842:in `call_method_on_directives'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/execution/interpreter/runtime.rb:507:in `block in evaluate_selection_with_args'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/execution/interpreter/runtime.rb:942:in `after_lazy'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/execution/interpreter/runtime.rb:445:in `evaluate_selection_with_args'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/execution/interpreter/runtime.rb:438:in `block in evaluate_selection'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/schema/member/has_arguments.rb:246:in `block (3 levels) in coerce_arguments'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/dataloader.rb:181:in `block in run'",
        "/usr/local/rvm/gems/ruby-3.1.3/gems/graphql-2.0.16/lib/graphql/dataloader.rb:306:in `block in spawn_fiber'"
      ]
    }
  ],
  "data": {}
}

Restrict certain models from being exposed through the REST API

Currently all models are exposed through the REST API with no easy way of restricting what is visible. We should look into a way that allows user's to identify what models they don't want to expose.

This can also potentially be tackled by doing the inverse. Perhaps we flip things around and every model is restricted by default. We would then need some way to identify what models we want to expose.


The graphql api has more fine grained control over what is exposed.

In app/graphql/types/mutation_type.rb we can control what models can be created/updated through a mutation by adding the name of the model to the method call. ex. hai_muation(Task, User, ...). The same can be done for querying by modifying what models are passed to hai_query(Task, User, ...)

When using the installation script, all models are included in these lists

Expose public methods on a model

Currently only the db column methods are exposed. We want the ability to add a method on a model and expose that to the api.

This will need to be strongly typed as well.

Restricted attributes

You may not want to expose all attributes from your model. We need a way to hide some attributes from the api.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.