Add User sdl and service
This commit is contained in:
40
api/src/graphql/users.sdl.js
Normal file
40
api/src/graphql/users.sdl.js
Normal file
@@ -0,0 +1,40 @@
|
||||
export const schema = gql`
|
||||
type User {
|
||||
id: Int!
|
||||
name: String
|
||||
email: String!
|
||||
hashedPassword: String!
|
||||
salt: String!
|
||||
resetToken: String
|
||||
resetTokenExpiresAt: DateTime
|
||||
}
|
||||
|
||||
type Query {
|
||||
users: [User!]! @requireAuth
|
||||
user(id: Int!): User @requireAuth
|
||||
}
|
||||
|
||||
input CreateUserInput {
|
||||
name: String
|
||||
email: String!
|
||||
hashedPassword: String!
|
||||
salt: String!
|
||||
resetToken: String
|
||||
resetTokenExpiresAt: DateTime
|
||||
}
|
||||
|
||||
input UpdateUserInput {
|
||||
name: String
|
||||
email: String
|
||||
hashedPassword: String
|
||||
salt: String
|
||||
resetToken: String
|
||||
resetTokenExpiresAt: DateTime
|
||||
}
|
||||
|
||||
type Mutation {
|
||||
createUser(input: CreateUserInput!): User! @requireAuth
|
||||
updateUser(id: Int!, input: UpdateUserInput!): User! @requireAuth
|
||||
deleteUser(id: Int!): User! @requireAuth
|
||||
}
|
||||
`
|
||||
30
api/src/services/users/users.js
Normal file
30
api/src/services/users/users.js
Normal file
@@ -0,0 +1,30 @@
|
||||
import { db } from 'src/lib/db'
|
||||
|
||||
export const users = () => {
|
||||
return db.user.findMany()
|
||||
}
|
||||
|
||||
export const user = ({ id }) => {
|
||||
return db.user.findUnique({
|
||||
where: { id },
|
||||
})
|
||||
}
|
||||
|
||||
export const createUser = ({ input }) => {
|
||||
return db.user.create({
|
||||
data: input,
|
||||
})
|
||||
}
|
||||
|
||||
export const updateUser = ({ id, input }) => {
|
||||
return db.user.update({
|
||||
data: input,
|
||||
where: { id },
|
||||
})
|
||||
}
|
||||
|
||||
export const deleteUser = ({ id }) => {
|
||||
return db.user.delete({
|
||||
where: { id },
|
||||
})
|
||||
}
|
||||
19
api/src/services/users/users.scenarios.js
Normal file
19
api/src/services/users/users.scenarios.js
Normal file
@@ -0,0 +1,19 @@
|
||||
export const standard = defineScenario({
|
||||
user: {
|
||||
one: {
|
||||
data: {
|
||||
email: 'String5632270',
|
||||
hashedPassword: 'String',
|
||||
salt: 'String',
|
||||
},
|
||||
},
|
||||
|
||||
two: {
|
||||
data: {
|
||||
email: 'String1221605',
|
||||
hashedPassword: 'String',
|
||||
salt: 'String',
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
52
api/src/services/users/users.test.js
Normal file
52
api/src/services/users/users.test.js
Normal file
@@ -0,0 +1,52 @@
|
||||
import { users, user, createUser, updateUser, deleteUser } from './users'
|
||||
|
||||
// Generated boilerplate tests do not account for all circumstances
|
||||
// and can fail without adjustments, e.g. Float and DateTime types.
|
||||
// Please refer to the RedwoodJS Testing Docs:
|
||||
// https://redwoodjs.com/docs/testing#testing-services
|
||||
// https://redwoodjs.com/docs/testing#jest-expect-type-considerations
|
||||
|
||||
describe('users', () => {
|
||||
scenario('returns all users', async (scenario) => {
|
||||
const result = await users()
|
||||
|
||||
expect(result.length).toEqual(Object.keys(scenario.user).length)
|
||||
})
|
||||
|
||||
scenario('returns a single user', async (scenario) => {
|
||||
const result = await user({ id: scenario.user.one.id })
|
||||
|
||||
expect(result).toEqual(scenario.user.one)
|
||||
})
|
||||
|
||||
scenario('creates a user', async () => {
|
||||
const result = await createUser({
|
||||
input: {
|
||||
email: 'String4358842',
|
||||
hashedPassword: 'String',
|
||||
salt: 'String',
|
||||
},
|
||||
})
|
||||
|
||||
expect(result.email).toEqual('String4358842')
|
||||
expect(result.hashedPassword).toEqual('String')
|
||||
expect(result.salt).toEqual('String')
|
||||
})
|
||||
|
||||
scenario('updates a user', async (scenario) => {
|
||||
const original = await user({ id: scenario.user.one.id })
|
||||
const result = await updateUser({
|
||||
id: original.id,
|
||||
input: { email: 'String5861252' },
|
||||
})
|
||||
|
||||
expect(result.email).toEqual('String5861252')
|
||||
})
|
||||
|
||||
scenario('deletes a user', async (scenario) => {
|
||||
const original = await deleteUser({ id: scenario.user.one.id })
|
||||
const result = await user({ id: original.id })
|
||||
|
||||
expect(result).toEqual(null)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user