parent
7f47152375
commit
80683c4ad8
File diff suppressed because it is too large
Load Diff
@ -1,7 +1,14 @@ |
||||
import "reflect-metadata"; |
||||
import { Field, ObjectType } from "type-graphql"; |
||||
import { BaseEntity, Column, Entity, PrimaryGeneratedColumn } from "typeorm"; |
||||
|
||||
@ObjectType() |
||||
export class User { |
||||
@Entity() |
||||
export class User extends BaseEntity { |
||||
@PrimaryGeneratedColumn() |
||||
id!: number
|
||||
|
||||
@Field() |
||||
id: string |
||||
@Column() |
||||
email: string = "" |
||||
} |
||||
|
@ -1,22 +1,59 @@ |
||||
import faker from "faker"; |
||||
import { createConnection, getConnection } from "typeorm"; |
||||
import { callSchema } from "../../utils/callSchema"; |
||||
import { User } from "../User"; |
||||
|
||||
const usersQuery = ` |
||||
query { |
||||
users { |
||||
id |
||||
email |
||||
} |
||||
}` |
||||
|
||||
describe("user resolver", () => { |
||||
it("should return an empty array when no users are created", async () => { |
||||
const response = await callSchema({ |
||||
source: usersQuery, |
||||
beforeEach(() => { |
||||
return createConnection({ |
||||
type: "sqlite", |
||||
database: ":memory:", |
||||
dropSchema: true, |
||||
entities: [User], |
||||
synchronize: true, |
||||
logging: false |
||||
}); |
||||
}); |
||||
|
||||
afterEach(() => { |
||||
let conn = getConnection(); |
||||
return conn.close(); |
||||
}); |
||||
|
||||
describe("resolver of", () => { |
||||
describe("users query", () => { |
||||
it("should return an empty array when no users are created", async () => { |
||||
const response = await callSchema({ |
||||
source: usersQuery, |
||||
}) |
||||
|
||||
expect(response).toMatchObject({ |
||||
data: { |
||||
users: [], |
||||
}, |
||||
}) |
||||
}) |
||||
|
||||
expect(response).toMatchObject({ |
||||
data: { |
||||
users: [], |
||||
}, |
||||
it("should return a populated array when an user is created", async () => { |
||||
const user = await User.create({ |
||||
email: faker.internet.email(), |
||||
}).save() |
||||
|
||||
const response = await callSchema({ |
||||
source: usersQuery, |
||||
}) |
||||
|
||||
expect(response).toMatchObject({ |
||||
data: { |
||||
users: [{ email: user.email }], |
||||
}, |
||||
}) |
||||
}) |
||||
}) |
||||
}) |
||||
|
@ -1,13 +1,14 @@ |
||||
import "reflect-metadata"; |
||||
import { Query, Resolver } from "type-graphql"; |
||||
import { getRepository } from "typeorm"; |
||||
import { User } from "../User"; |
||||
|
||||
@Resolver() |
||||
@Resolver(of => User) |
||||
export class UserResolver { |
||||
private usersCollection: User[] = [] |
||||
|
||||
@Query(returns => [User]) |
||||
async users() { |
||||
return await this.usersCollection |
||||
const userRepository = getRepository(User) |
||||
return userRepository.find() |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue