Graphql-Ruby Mapping using Active Record - ruby-on-rails

I am completely new to using Rails, I'm trying to create a basic crud application using ruby-graphql to do a simple find query with active record to a sqlite3 database.
I set up a user type
class UserType < Types::BaseObject
description "A user type "
field :id, ID, null: false
field :username, String, null: false
field :email, String, null: false
field :email_confirmed, Boolean, null:false
field :first_name, String , null:false
field :last_name, String, null:false
field :biography, String, null:true
field :avatar, String, null:true
field :created_at, GraphQL::Types::ISO8601DateTime, null:false
field :updated_at, GraphQL::Types::ISO8601DateTime, null:false
end
Then I setup the query :
field :user, UserType, null:false,
description: "Get a single user" do
argument :username, String, required: true
end
def user(username:)
User.find(username)
end
My query is :
{
user(username:"test"){
username
}
}
I get the following error :
{
"error": {
"message": "no implicit conversion of #<Class:0x000000000b975440> into Hash",
"backtrace": [
"C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/graphql-1.9.13/lib/graphql/schema/field.rb:519:in `block in resolve'",
If anyone could help me I would really appreciate it.
How do I do this conversion in Ruby / Rails?

Turns out there was an issue with Ruby gem I was using upgraded to the new gem and it solved the problem. See - https://github.com/rmosolgo/graphql-ruby/issues/2537

Related

return array of objects in graphql rails

I need write code for show users with organizations, but return next error in query, search, but with error
My code
module Types
class UserType < Types::BaseObject
field :id, ID, null: false
field :email, String, null: false
field :role, String, null: true
field :organization_ids, [Types::OrganizationType], null: false
end
end
module Types
class OrganizationType < Types::BaseObject
field :id, ID, null: false
field :name, String, null: false
end
end
But return next error
This is my query
It looks like you are trying to return an association: a User has many Organizations?
Make sure in user.rb you have the association defined with has_many :organizations ... and include any middle models required for Rails to understand the associations
Update your UserType to:
module Types
class UserType < Types::BaseObject
...
field :organizations, [Types::OrganizationType], null: false
end
end
Then you should be able to query like:
query getUsers{
users {
id
email
role
organizations {
id
}
}
}

Rails GraphQL: Query the fields of an associated entity-type via the foreign key

My data-model is this way: A vendor has many cars. A car belongs to one vendor.
So I got the ID of the respective vendor in each car-entity. What I like to have are the other fields of the vendor. Like vendor-name, country etc.
I finally have accomplished the described task:
Here is the code, which I have written.
The CarType:
module Types
class CarType < Types::BaseObject
field :id, ID, null: false
field :name, String, null: false
field :vendor_id, ID, null: false
field :vendor, [String], null: true
def vendor
vendor = Vendor.find(object.vendor_id)
[vendor[:name], vendor[:country]]
end
end
end
And the root-type definition, plus resolver-function:
field :car, CarType, null: false,
description: "A single car-entity." do
argument :id, ID, required: true
end
def car(id:)
Car.find(id)
end
It works, but I like to ask:
Is my implementation correct, so that it can be used that way?
Or has it to be done differently?
In case so: How has it to be done?

How do I return custom object in a mutation?

I'd like my GraphQL mutation to return field that is not defined in the Type Object
I am using the Ruby GraphQL gem with Rails 6. I have described a UserType with the fields username, email and password. In my login mutation, I'd like to return a jwt token as the response. Here's what I have so far;
The user type:
module Types
class UserType < BaseObject
field :id, ID, null: false
field :email, String, null: false
field :username, String, null: false
end
end
The mutation:
module Mutations
UserMutation = GraphQL::ObjectType.define do
field :login, Types::UserType do
argument :username, !types.String
argument :password, !types.String
resolve ->(_obj, args, _ctx) do
# perform the authentication and return a jwt token.
end
end
end
end
I expect to get a response like:
{
"data" : {
"token": "my_generated_token"
}
}
or an error message of the same shape.
Update: Here's the solution that worked for me.
I created a new type LoginType that defines token and user fields and made the mutation respond to it instead of the UserType
module Types
class LoginType < BaseObject
field :token, String, null: false
field :user, Types::UserType, null: false
end
end
Thanks to #rmosolgo for the help

Rails Neo4j Migration Argument Error

So I have these two classes
class Tweet
include Neo4j::ActiveNode
property :content, type: String
property :created_at, type: DateTime
property :updated_at, type: DateTime
has_one(:user, :tweeted).from(:User)
end
class User
include Neo4j::ActiveNode
property :username, type: String
property :name, type: String
property :email, type: String, default: ''
validates :email, :username, uniqueness: true
property :encrypted_password
has_many(:Tweet, :tweeted)
end
And everytime I run rails neo4j:migrate it gives error like this
ArgumentError: The 'type' option must be specified( even if it is nil) or origin/rel_class must be specified (Class#tweeted)
How to properly create relationship between nodes in neo4jrb?
As the error message describes, you didn't explicitly define the type of your tweeted relation. Take a look at the official documentation for further details. However, the following should work:
class Tweet
include Neo4j::ActiveNode
property :content, type: String
property :created_at, type: DateTime
property :updated_at, type: DateTime
has_one :out, :author, type: :author, model_class: :User
end
class User
include Neo4j::ActiveNode
property :username, type: String
property :name, type: String
property :email, type: String, default: ''
validates :email, :username, uniqueness: true
property :encrypted_password
has_many :in, :tweets, origin: :author
end

Updating record with Mongoid in Rails

I am testing this from rails console:
Credential.last.token => nil
Credential.last.update_attribute :token, '123' => true
Credential.last.token => nil
Here is my class:
class Credential
include Mongoid::Document
include Mongoid::Timestamps
field :_id, type: String
field :user_id, type: Integer
field :code, type: String
field :provider, type: String
field :token, type: String
end
What am I doing wrong?
If you have identity map enabled you'll need to wrap that in
Mongoid.unit_of_work { Credential.last.token }
Mongoid caches the queries. It is not a problem for web requests, but in the console you won't see the change unless you do it in the unit of work block, or restart the console (not just a reload)
I had to put
attr_accessor :token, ...

Resources