-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
64 lines (56 loc) · 1.33 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { createSchema, createYoga } from "graphql-yoga";
import { useDeferStream } from "@graphql-yoga/plugin-defer-stream";
import { users } from "./users";
import { companies } from "./companies";
const typeDefs = /* GraphQL */ `
type User {
name: String!
email: String!
company: Company!
}
type Company {
id: String!
name: String!
branch: String!
}
type Query {
company(id: String!): Company
users(companyId: String): [User!]!
}
`;
const wait = (time: number) =>
new Promise((resolve) => setTimeout(resolve, time));
const resolvers = {
Query: {
// @ts-ignore
company: async (_, { id }) => {
return companies.find((company) => company.id === id);
},
// @ts-ignore
users: async (_, { companyId }) => {
await wait(2000);
return users
.filter(({ company }) => !companyId || company === companyId)
.map((user) => ({
...user,
company: companies.find((company) => company.id === user.company),
}));
},
},
};
const yoga = createYoga({
schema: createSchema({
typeDefs,
resolvers,
}),
plugins: [useDeferStream()],
logging: "debug",
});
// @ts-ignore
const server = Bun.serve(yoga);
console.info(
`Server is running on ${new URL(
yoga.graphqlEndpoint,
`http://${server.hostname}:${server.port}`
)}`
);