Giter Club home page Giter Club logo

Comments (6)

sunny avatar sunny commented on July 3, 2024 1

Hi there!

When using type:, it ensures that the arguments you give to the actor inherit from the classes that you give.

For example:

class UpdateUser < Actor
  input :user, type: "User"
  input :age, type: ["Integer", "Float"]

  # …
end

class User < ApplicationRecord
  # …
end

This would accept:

UpdateUser.call(user: User.new, age: 12)

but raise an error when calling:

UpdateUser.call(user: 1, age: "12")

from actor.

sunny avatar sunny commented on July 3, 2024 1

Yep, just like that 👍🏻

Here’s a full example with a custom class:

class GreetUser < Actor
  input :user, type: "User"

  def call
    puts "Hello #{user.name}!"
  end
end

class User
  def initialize(name:, email:, password:)
    @name = name
    @email = email
    @password = password
  end

  attr_reader :name, :email, :password
end

user = User.new(name: "Kylo", email: "[email protected]", password: "upsilon1%25")

GreetUser.call(user: "wrong")
# => ServiceActor::ArgumentError: Input user on GreetUser must be of type User but was String

GreetUser.call(user: user)
# => Hello Kylo!

from actor.

paulomcnally avatar paulomcnally commented on July 3, 2024 1

Ha ok, dont it's a deep validator.

You can't tell inside User which name has a specific type or which age is of type integer.

Validation is at the highest level only.

This has resolved my doubts.

Thanks.

from actor.

paulomcnally avatar paulomcnally commented on July 3, 2024

So it has to be a class that inherits from ApplicationRecord?

Doesn't work like graphql-ruby that you can create types even without inheriting from ApplicationRecord?

from actor.

sunny avatar sunny commented on July 3, 2024

No, it can be any class, in the example I’m also using Integer, Float which don’t inherit from ApplicationRecord.

from actor.

paulomcnally avatar paulomcnally commented on July 3, 2024

How would you do a class UserNotApplicationRecord that has name string, email string, and password string?

...
input :user, type: 'UserNotApplicationRecord'
...

from actor.

Related Issues (20)

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.