Skip to content

Commit

Permalink
implements Node && permission!
Browse files Browse the repository at this point in the history
  • Loading branch information
hoshinotsuyoshi committed Oct 14, 2024
1 parent 737ee94 commit 9ad1c14
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 31 deletions.
5 changes: 3 additions & 2 deletions backend/app/graphql/mutations/login.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Login < BaseMutation
argument :email_address, GraphQL::Types::String, required: true
argument :password, GraphQL::Types::String, required: true

field :user, Types::UserType, null: true
field :success, GraphQL::Types::Boolean, null: false
field :errors, [Types::Errors::LoginError], null: false

def resolve(email_address:, password:)
Expand All @@ -18,7 +18,8 @@ def resolve(email_address:, password:)
error = :something_wrong
errors << error
end
{ user:, errors: }

{ success: !!user, errors: }
end
end
end
7 changes: 4 additions & 3 deletions backend/app/graphql/mutations/signup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Signup < BaseMutation

argument :email_address, GraphQL::Types::String, required: true

field :user, Types::UserType, null: true
field :success, GraphQL::Types::Boolean, null: false
field :errors, [Types::Errors::SignupError], null: false

def resolve(email_address:)
Expand All @@ -18,11 +18,12 @@ def resolve(email_address:)
end
if user.valid?
InvitationMailer.invite(user.id).deliver_later
success = true
else
errors += user.errors.errors
user = nil
success = false
end
{ user:, errors: }
{ success:, errors: }
end
end
end
5 changes: 2 additions & 3 deletions backend/app/graphql/mutations/verify_email_address.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class VerifyEmailAddress < BaseMutation

argument :signed_id, GraphQL::Types::String, required: true

field :user, Types::UserType, null: true
field :success, GraphQL::Types::Boolean, null: false

def resolve(signed_id:)
user = nil
Expand All @@ -17,9 +17,8 @@ def resolve(signed_id:)
next unless user
user.update!(onboarding_status: :before_set_own_password)
start_new_session_for(user)
user
end
{ user: }
{ success: !!user }
end
end
end
6 changes: 5 additions & 1 deletion backend/app/graphql/types/user_type.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
module Types
class UserType < Types::BaseObject
field :id, ID, null: false
implements NodeType
field :email_address, String, null: false

def self.authorized?(object, context)
super && object == context.fetch(:current_user)
end
end
end
7 changes: 3 additions & 4 deletions frontend/src/components/Login/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { useMutation } from '@apollo/client'
import React, { useEffect, type FormEvent, useState } from 'react'
import React, { type FormEvent, useState } from 'react'
import { Link } from 'react-router-dom'
import { css } from '../../../styled-system/css'
import type { LoginPayload, MutationLoginArgs } from '../../generated/graphql'
import { LoginInputSchema } from '../../generated/graphql'
import { LoginDocument } from '../../generated/graphql'
import { ROUTES } from '../../routes'
Expand Down Expand Up @@ -35,8 +34,8 @@ export const Login = () => {
},
})

if (data?.login?.user) {
console.log('successful', data.login)
if (data?.login?.success) {
console.log('successful')
location.href = ROUTES.ME
} else if (errors?.length) {
console.log('GraphQL failed', errors[0].message)
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/components/Login/login.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mutation Login($input: LoginInput!) {
login(input: $input) {
user {
__typename
}
success
errors {
__typename
}
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/Signup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export const Signup = () => {
},
})

if (data?.signup?.user) {
if (data?.signup?.success) {
setInviting(true)
console.log('successful', data.signup.user)
console.log('successful')
} else if (data?.signup?.errors[0].__typename === 'Taken') {
setBusinessLogicError('Email address is already taken')
console.log('already taken')
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/components/Signup/signup.graphql
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
mutation Signup($input: SignupInput!) {
signup(input: $input) {
user {
__typename
}
success
errors {
__typename
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
mutation VerifyEmailAddress($input: VerifyEmailAddressInput!) {
verifyEmailAddress(input: $input) {
user {
id
__typename
}
success
}
}
4 changes: 2 additions & 2 deletions frontend/src/components/VerifyEmailAddress/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ export const VerifyEmailAddress = () => {
signedId,
})

const response = await verifyEmailAddress({
const { data } = await verifyEmailAddress({
variables: {
input: validatedInput,
},
})

if (response.data?.verifyEmailAddress?.user) {
if (data?.verifyEmailAddress?.success) {
setSuccess(true)
setTimeout(() => {
navigate(ROUTES.SET_PASSWORD)
Expand Down
42 changes: 38 additions & 4 deletions graphql-schema/backend_schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ type LoginPayload {
"""
clientMutationId: String
errors: [LoginError!]!
user: User
success: Boolean!
}

"""
Expand Down Expand Up @@ -101,11 +101,41 @@ type Mutation {
): VerifyEmailAddressPayload!
}

"""
An object with an ID.
"""
interface Node {
"""
ID of the object.
"""
id: ID!
}

type Query {
"""
Fetch current session
"""
me: User

"""
Fetches an object given its ID.
"""
node(
"""
ID of the object.
"""
id: ID!
): Node

"""
Fetches a list of objects given a list of IDs.
"""
nodes(
"""
IDs of the objects.
"""
ids: [ID!]!
): [Node]
}

"""
Expand Down Expand Up @@ -156,7 +186,7 @@ type SignupPayload {
"""
clientMutationId: String
errors: [SignupError!]!
user: User
success: Boolean!
}

type SomethingWrong {
Expand All @@ -167,8 +197,12 @@ type Taken {
message: String!
}

type User {
type User implements Node {
emailAddress: String!

"""
ID of the object.
"""
id: ID!
}

Expand All @@ -191,5 +225,5 @@ type VerifyEmailAddressPayload {
A unique identifier for the client performing the mutation.
"""
clientMutationId: String
user: User
success: Boolean!
}

0 comments on commit 9ad1c14

Please sign in to comment.