Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(backend): simple user query #1399

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions backend/src/graphql/models/UserModel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { User as DbUser } from '@prisma/client'
import { ObjectType, Field, Int } from 'type-graphql'

@ObjectType()
export class User {
constructor(user: DbUser) {
this.id = user.id
this.username = user.username
this.name = user.name
}

@Field(() => Int)
id: number

@Field()
username: string

@Field()
name: string
}
69 changes: 69 additions & 0 deletions backend/src/graphql/resolvers/UserResolver.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { ApolloServer } from '@apollo/server'

import { createTestServer } from '#src/server/server'

let testServer: ApolloServer

beforeAll(async () => {
testServer = await createTestServer()
})

describe('UserResolver', () => {
describe('users query', () => {
describe('unauthenticated', () => {
it('returns an unauthenticated error', async () => {
const response = await testServer.executeOperation({
query: `{users {id name username}}`,
})
expect(response).toMatchObject({
body: {
kind: 'single',
singleResult: {
data: null,
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
errors: expect.arrayContaining([
expect.objectContaining({
message: 'Access denied! You need to be authenticated to perform this action!',
}),
]),
},
},
})
})
})

describe('authenticated', () => {
it('returns a list of users', async () => {
const response = await testServer.executeOperation(
{
query: `{users {id name username}}`,
},
{
contextValue: {
token: 'token',
user: undefined,
},
},
)
expect(response).toMatchObject({
body: {
kind: 'single',
singleResult: {
data: {
users: [
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
id: expect.any(Number),
name: 'User',
username: 'mockedUser',
},
],
},
errors: undefined,
},
},
})
})
})
})
})
13 changes: 13 additions & 0 deletions backend/src/graphql/resolvers/UserResolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Resolver, Query, Authorized } from 'type-graphql'

import { User } from '#graphql/models/UserModel'
import { prisma } from '#src/prisma'

@Resolver()
export class UserResolver {
@Authorized()
@Query(() => [User])
async users(): Promise<User[]> {
return prisma.user.findMany()
}
}
3 changes: 2 additions & 1 deletion backend/src/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import { ContactFormResolver } from './resolvers/ContactFormResolver'
import { checkForOpenRooms } from './resolvers/dal/handleOpenRooms'
import { NewsletterSubscriptionResolver } from './resolvers/NewsletterSubscriptionResolver'
import { RoomResolver } from './resolvers/RoomResolver'
import { UserResolver } from './resolvers/UserResolver'

if (CONFIG.BBB_PULL_MEETINGS) void checkForOpenRooms()

export const schema = async (): Promise<GraphQLSchema> => {
return buildSchema({
resolvers: [ContactFormResolver, NewsletterSubscriptionResolver, RoomResolver],
resolvers: [ContactFormResolver, NewsletterSubscriptionResolver, RoomResolver, UserResolver],
pubSub,
validate: {
validationError: { target: false },
Expand Down
Loading