add currentUser query

This commit is contained in:
João Geonizeli
2021-08-07 12:46:39 -03:00
parent 79047938d1
commit 94e9aa08b4
6 changed files with 43 additions and 15 deletions

View File

@@ -10,7 +10,7 @@ class GraphqlController < ApplicationController
operation_name = params[:operationName]
context = {
current_auth: current_auth,
current_user: current_admin_user, # || current_auth.current_user,
current_user: current_auth&.user,
}
result = XStakeSchema.execute(query, variables: variables, context: context, operation_name: operation_name)
render(json: result)

View File

@@ -4,14 +4,9 @@ module Types
include GraphQL::Types::Relay::HasNodeField
include GraphQL::Types::Relay::HasNodesField
# TODO: remove me
field :test_field, [String], null: false,
description: "An example field added by the generator"
def test_field
[
SecureRandom.uuid,
SecureRandom.uuid,
]
field :current_user, UserType, null: true
def current_user
context[:current_user]
end
end
end

View File

@@ -0,0 +1,14 @@
# frozen_string_literal: true
module Types
class UserType < Types::BaseObject
# implements GraphQL::Types::Relay::Node
global_id_field :id
field :id, ID, null: false
field :first_name, String, null: false
field :last_name, String, null: false
field :full_name, String, null: false
field :email, String, null: false
end
end

View File

@@ -35,10 +35,15 @@ type Mutation {
}
type Query {
"""
An example field added by the generator
"""
testField: [String!]!
currentUser: User
}
type User {
email: String!
firstName: String!
fullName: String!
id: ID!
lastName: String!
}
input UserAttributesInput {

View File

@@ -8,8 +8,8 @@ module Auth
@email = attributes[:email]
end
def customer
@customer ||= Customer.find_by(email: email, auth_id: id)
def user
@user ||= User.find_by(email: email)
end
end
end

View File

@@ -0,0 +1,14 @@
# frozen_string_literal: true
require "rails_helper"
RSpec.describe(Types::UserType) do
subject { described_class }
describe "arguments" do
it { is_expected.to(have_a_field(:id).of_type("ID!")) }
it { is_expected.to(have_a_field(:first_name).of_type("String!")) }
it { is_expected.to(have_a_field(:last_name).of_type("String!")) }
it { is_expected.to(have_a_field(:full_name).of_type("String!")) }
it { is_expected.to(have_a_field(:email).of_type("String!")) }
end
end