Guest User

Untitled

a guest
Jun 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. class Books
  2. belongs_to :user
  3. end
  4.  
  5. class Users
  6. has_many :books
  7. end
  8.  
  9. first_name
  10. last_name
  11. email
  12.  
  13. author
  14. title
  15. description
  16.  
  17. Types::BookType = GraphQL::ObjectType.define do
  18. name 'Book'
  19. field :id, !types.ID
  20. field :author, !types.String
  21. field :title, !types.String
  22. field :user, -> {Types::UserType}, property: :user
  23. end
  24.  
  25. Types::UserType = GraphQL::ObjectType.define do
  26. name 'User'
  27. field :id, !types.ID
  28. field :first_name, !types.String
  29. field :last_name, !types.String
  30. field :email, !types.String
  31. end
  32.  
  33. Types::MutationType = GraphQL::ObjectType.define do
  34. name 'Mutation'
  35. field :bookCreate, function: Resolvers::BookCreate.new
  36. end
  37.  
  38. class Resolvers::BookCreate < GraphQL::Function
  39. argument :author, !types.String
  40. argument :title, !types.String
  41. argument :description, !types.String
  42. argument :user, !types.Int
  43.  
  44. type Types::BookType
  45.  
  46. def call(_obj, args, _ctx)
  47. Book.create!(
  48. author: args[:author],
  49. title: args[:title],
  50. description: args[:description],
  51. user: args[:user]
  52. )
  53. end
  54. end
  55.  
  56. mutation book {
  57. bookCreate(
  58. author: "Test",
  59. title: "Book Test",
  60. description: "Book Description",
  61. user: 2) {
  62. id
  63. author
  64. title
  65. description
  66. }
  67. }
  68.  
  69. <ActiveRecord::AssociationTypeMismatch: User(#69982231587660) expected, got 2 which is an instance of Integer(#11387600)>
Add Comment
Please, Sign In to add comment