From 94e9aa08b407c6c4b2faeb046d1c1097e355930d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Sat, 7 Aug 2021 12:46:39 -0300 Subject: [PATCH] add currentUser query --- app/controllers/graphql_controller.rb | 2 +- app/graphql/types/query_type.rb | 11 +++-------- app/graphql/types/user_type.rb | 14 ++++++++++++++ app/javascript/__generated__/schema.graphql | 13 +++++++++---- app/services/auth/profile.rb | 4 ++-- spec/graphql/types/user_type_spec.rb | 14 ++++++++++++++ 6 files changed, 43 insertions(+), 15 deletions(-) create mode 100644 app/graphql/types/user_type.rb create mode 100644 spec/graphql/types/user_type_spec.rb diff --git a/app/controllers/graphql_controller.rb b/app/controllers/graphql_controller.rb index b729732..06a9438 100644 --- a/app/controllers/graphql_controller.rb +++ b/app/controllers/graphql_controller.rb @@ -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) diff --git a/app/graphql/types/query_type.rb b/app/graphql/types/query_type.rb index bff994f..f662605 100644 --- a/app/graphql/types/query_type.rb +++ b/app/graphql/types/query_type.rb @@ -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 diff --git a/app/graphql/types/user_type.rb b/app/graphql/types/user_type.rb new file mode 100644 index 0000000..61bfec3 --- /dev/null +++ b/app/graphql/types/user_type.rb @@ -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 diff --git a/app/javascript/__generated__/schema.graphql b/app/javascript/__generated__/schema.graphql index 6a12ac4..a90a765 100644 --- a/app/javascript/__generated__/schema.graphql +++ b/app/javascript/__generated__/schema.graphql @@ -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 { diff --git a/app/services/auth/profile.rb b/app/services/auth/profile.rb index 6574c51..d2866d3 100644 --- a/app/services/auth/profile.rb +++ b/app/services/auth/profile.rb @@ -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 diff --git a/spec/graphql/types/user_type_spec.rb b/spec/graphql/types/user_type_spec.rb new file mode 100644 index 0000000..ca35ed6 --- /dev/null +++ b/spec/graphql/types/user_type_spec.rb @@ -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