add createUser mutation
This commit is contained in:
@@ -10,3 +10,9 @@ AllCops:
|
|||||||
Exclude:
|
Exclude:
|
||||||
- db/schema.rb
|
- db/schema.rb
|
||||||
- bin/**/*
|
- bin/**/*
|
||||||
|
|
||||||
|
RSpec/ExampleLength:
|
||||||
|
Enabled: false
|
||||||
|
|
||||||
|
RSpec/MultipleExpectations:
|
||||||
|
Enabled: false
|
||||||
|
|||||||
1
Gemfile
1
Gemfile
@@ -48,4 +48,5 @@ end
|
|||||||
|
|
||||||
group :test do
|
group :test do
|
||||||
gem "shoulda-matchers", "~> 5.0"
|
gem "shoulda-matchers", "~> 5.0"
|
||||||
|
gem "rspec-graphql_matchers", "~> 1.3"
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -233,6 +233,8 @@ GEM
|
|||||||
rspec-expectations (3.10.1)
|
rspec-expectations (3.10.1)
|
||||||
diff-lcs (>= 1.2.0, < 2.0)
|
diff-lcs (>= 1.2.0, < 2.0)
|
||||||
rspec-support (~> 3.10.0)
|
rspec-support (~> 3.10.0)
|
||||||
|
rspec-graphql_matchers (1.3.0)
|
||||||
|
graphql (>= 1.8, < 2.0)
|
||||||
rspec-mocks (3.10.2)
|
rspec-mocks (3.10.2)
|
||||||
diff-lcs (>= 1.2.0, < 2.0)
|
diff-lcs (>= 1.2.0, < 2.0)
|
||||||
rspec-support (~> 3.10.0)
|
rspec-support (~> 3.10.0)
|
||||||
@@ -343,6 +345,7 @@ DEPENDENCIES
|
|||||||
puma (~> 5.0)
|
puma (~> 5.0)
|
||||||
pundit
|
pundit
|
||||||
rails (~> 6.1.4)
|
rails (~> 6.1.4)
|
||||||
|
rspec-graphql_matchers (~> 1.3)
|
||||||
rspec-rails
|
rspec-rails
|
||||||
rubocop-rails
|
rubocop-rails
|
||||||
rubocop-rspec
|
rubocop-rspec
|
||||||
|
|||||||
@@ -9,8 +9,8 @@ class GraphqlController < ApplicationController
|
|||||||
query = params[:query]
|
query = params[:query]
|
||||||
operation_name = params[:operationName]
|
operation_name = params[:operationName]
|
||||||
context = {
|
context = {
|
||||||
current_user: current_admin_user, # || current_auth.current_user,
|
|
||||||
current_auth: current_auth,
|
current_auth: current_auth,
|
||||||
|
current_user: current_admin_user, # || current_auth.current_user,
|
||||||
}
|
}
|
||||||
result = XStakeSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
|
result = XStakeSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
|
||||||
render(json: result)
|
render(json: result)
|
||||||
|
|||||||
9
app/graphql/inputs/user_attributes_input.rb
Normal file
9
app/graphql/inputs/user_attributes_input.rb
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
module Inputs
|
||||||
|
class UserAttributesInput < Types::BaseInputObject
|
||||||
|
graphql_name "UserAttributesInput"
|
||||||
|
|
||||||
|
argument :first_name, String, required: true
|
||||||
|
argument :last_name, String, required: true
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -5,5 +5,9 @@ module Mutations
|
|||||||
field_class Types::BaseField
|
field_class Types::BaseField
|
||||||
input_object_class Types::BaseInputObject
|
input_object_class Types::BaseInputObject
|
||||||
object_class Types::BaseObject
|
object_class Types::BaseObject
|
||||||
|
|
||||||
|
field :errors, [String],
|
||||||
|
null: true,
|
||||||
|
description: "Errors encountered during execution of the mutation."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
16
app/graphql/mutations/create_user.rb
Normal file
16
app/graphql/mutations/create_user.rb
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
module Mutations
|
||||||
|
class CreateUser < BaseMutation
|
||||||
|
field :success, Boolean, null: false
|
||||||
|
|
||||||
|
argument :user, Inputs::UserAttributesInput, required: true
|
||||||
|
|
||||||
|
def resolve(user:)
|
||||||
|
User.create!({ **user, email: context[:current_auth].email })
|
||||||
|
|
||||||
|
{ success: true }
|
||||||
|
rescue ActiveRecord::RecordInvalid => e
|
||||||
|
{ success: false, errors: [e.message] }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,11 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
module Types
|
module Types
|
||||||
class MutationType < Types::BaseObject
|
class MutationType < Types::BaseObject
|
||||||
# TODO: remove me
|
field :create_user, mutation: Mutations::CreateUser
|
||||||
field :test_field, String, null: false,
|
|
||||||
description: "An example field added by the generator"
|
|
||||||
def test_field
|
|
||||||
"Hello World"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ class User < ApplicationRecord
|
|||||||
has_many :documents, class_name: "UserDocument", dependent: :destroy
|
has_many :documents, class_name: "UserDocument", dependent: :destroy
|
||||||
|
|
||||||
validates :first_name, :last_name, :email, presence: true
|
validates :first_name, :last_name, :email, presence: true
|
||||||
|
validates :email, uniqueness: true
|
||||||
|
|
||||||
def full_name
|
def full_name
|
||||||
"#{first_name} #{last_name}"
|
"#{first_name} #{last_name}"
|
||||||
|
|||||||
@@ -8,6 +8,8 @@ module Auth
|
|||||||
end
|
end
|
||||||
|
|
||||||
def profile
|
def profile
|
||||||
|
return nil if jwt_token.blank?
|
||||||
|
|
||||||
Auth0Client.find_profile(jwt_token)
|
Auth0Client.find_profile(jwt_token)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
11
spec/graphql/inputs/user_attributes_input_spec.rb
Normal file
11
spec/graphql/inputs/user_attributes_input_spec.rb
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
require "rails_helper"
|
||||||
|
|
||||||
|
RSpec.describe(Inputs::UserAttributesInput) do
|
||||||
|
subject { described_class }
|
||||||
|
|
||||||
|
describe "arguments" do
|
||||||
|
it { is_expected.to(accept_argument(:first_name).of_type("String!")) }
|
||||||
|
it { is_expected.to(accept_argument(:last_name).of_type("String!")) }
|
||||||
|
end
|
||||||
|
end
|
||||||
66
spec/graphql/mutations/create_user_spec.rb
Normal file
66
spec/graphql/mutations/create_user_spec.rb
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
require "rails_helper"
|
||||||
|
|
||||||
|
RSpec.describe(Mutations::CreateUser) do
|
||||||
|
describe "#resolve" do
|
||||||
|
let(:mutation_string) do
|
||||||
|
<<~GQL
|
||||||
|
mutation($input: CreateUserInput!) {
|
||||||
|
createUser(input: $input) {
|
||||||
|
success
|
||||||
|
errors
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GQL
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:context) do
|
||||||
|
{
|
||||||
|
current_auth: Auth::Profile.new({
|
||||||
|
id: "_",
|
||||||
|
email: "user@example.com",
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
let(:variables) do
|
||||||
|
{
|
||||||
|
input: { user: {
|
||||||
|
firstName: "First Name",
|
||||||
|
lastName: "Last Name",
|
||||||
|
} },
|
||||||
|
}
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when current_auth is not being used by any user" do
|
||||||
|
it "create a user to auth" do
|
||||||
|
result = XStakeSchema.execute(
|
||||||
|
mutation_string,
|
||||||
|
variables: variables,
|
||||||
|
context: context
|
||||||
|
).to_h
|
||||||
|
|
||||||
|
expect(result["data"]["createUser"]["success"]).to(eq(true))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context "when auth is being used by no users" do
|
||||||
|
it "returns error" do
|
||||||
|
User.create(
|
||||||
|
first_name: "First Name",
|
||||||
|
last_name: "Last Name",
|
||||||
|
email: "user@example.com"
|
||||||
|
)
|
||||||
|
|
||||||
|
result = XStakeSchema.execute(
|
||||||
|
mutation_string,
|
||||||
|
variables: variables,
|
||||||
|
context: context
|
||||||
|
).to_h
|
||||||
|
|
||||||
|
expect(result["data"]["createUser"]["success"]).to(eq(false))
|
||||||
|
expect(result["data"]["createUser"]["errors"]).to(eq(["Validation failed: Email has already been taken"]))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
Reference in New Issue
Block a user