Skip to content

Commit 3f13b90

Browse files
committed
add deno graphql server for specific beer details
1 parent 4cebcbf commit 3f13b90

File tree

3 files changed

+87
-33
lines changed

3 files changed

+87
-33
lines changed

deno/deps.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
export {
2-
Application,
3-
Router,
4-
RouterContext,
5-
} from "https://deno.land/x/[email protected]/mod.ts";
1+
export { Application, Router } from "https://deno.land/x/[email protected]/mod.ts";
2+
export type { RouterContext } from "https://deno.land/x/[email protected]/mod.ts";
63
export {
74
applyGraphQL,
85
gql,

deno/main.ts

+74-28
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,109 @@
1-
import { Application } from "./deps.ts";
1+
import { Application, Router } from "./deps.ts";
22
import { applyGraphQL, gql } from "./deps.ts";
3+
import type { InputType, PayloadType } from "./types.ts";
34

45
const app = new Application();
56

7+
app.use(async (ctx, next) => {
8+
await next();
9+
const rt = ctx.response.headers.get("X-Response-Time");
10+
console.log(`${ctx.request.method} ${ctx.request.url} - ${rt}`);
11+
});
12+
13+
app.use(async (ctx, next) => {
14+
const start = Date.now();
15+
await next();
16+
const ms = Date.now() - start;
17+
ctx.response.headers.set("X-Response-Time", `${ms}ms`);
18+
});
19+
620
const types = gql`
7-
type Dino {
8-
name: String
9-
image: String
21+
type Beer {
22+
breweryId: ID!
23+
id: ID!
24+
name: String!
25+
type: String!
1026
}
11-
input DinoInput {
12-
name: String
13-
image: String
27+
input CreateBeerInput {
28+
clientMutationId: String!
29+
name: String!
30+
type: String!
31+
breweryId: ID!
1432
}
15-
type ResolveType {
16-
done: Boolean
33+
type CreateBeerPayload {
34+
beer: Beer!
35+
clientMutationId: String!
1736
}
1837
type Query {
19-
getDino(name: String): Dino
20-
getDinos: [Dino!]!
38+
beer(id: ID!): Beer
39+
beers: [Beer!]!
2140
}
2241
type Mutation {
23-
addDino(input: DinoInput!): ResolveType!
42+
createBeer(input: CreateBeerInput!): CreateBeerPayload!
2443
}
2544
`;
2645

27-
const dinos = [
46+
type Beer = { id: number; breweryId: number; name: string; type: string };
47+
48+
const beers: Beer[] = [
2849
{
29-
name: "Tyrannosaurus Rex",
30-
image: "🦖",
50+
id: 1,
51+
breweryId: 1,
52+
name: "Costumes & Karaoke",
53+
type: "IPA",
3154
},
3255
];
3356

3457
const resolvers = {
3558
Query: {
36-
getDino: (_, { name }) => {
37-
const dino = dinos.find((dino) => dino.name.includes(name));
38-
if (!dino) {
39-
throw new Error(`No dino name includes ${name}`);
59+
beer: (_: unknown, { id }: Beer) => {
60+
const beer = beers.find((beer) => beer.id === id);
61+
if (!beer) {
62+
throw new Error(
63+
`Beer with the id of '${id}' was not able to be found.`
64+
);
4065
}
41-
return dino;
66+
return beer;
4267
},
43-
getDinos: () => {
44-
return dinos;
68+
beers: () => {
69+
return beers;
4570
},
4671
},
4772
Mutation: {
48-
addDino: (_, { input: { name, image } }) => {
49-
dinos.push({
73+
createBeer: (
74+
_: unknown,
75+
{
76+
input: { clientMutationId, name, type, breweryId },
77+
}: InputType<Beer>
78+
): PayloadType<{ beer: Beer }> => {
79+
const id =
80+
beers.reduce((prev, current) => {
81+
if (prev > current.id) {
82+
return prev;
83+
}
84+
85+
return current.id;
86+
}, 0) + 1;
87+
88+
const beer: Beer = {
89+
id,
90+
breweryId,
5091
name,
51-
image,
52-
});
92+
type,
93+
};
94+
95+
beers.push(beer);
96+
5397
return {
54-
done: true,
98+
beer,
99+
clientMutationId,
55100
};
56101
},
57102
},
58103
};
59104

60-
const GraphQLService = applyGraphQL({
105+
const GraphQLService = await applyGraphQL<Router>({
106+
Router,
61107
typeDefs: types,
62108
resolvers: resolvers,
63109
});

deno/types.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export type IdType = { id: unknown };
2+
3+
export type InputType<T extends IdType> = {
4+
input: Omit<T, "id"> & {
5+
clientMutationId: string;
6+
};
7+
};
8+
9+
export type PayloadType<T> = T & {
10+
clientMutationId: string;
11+
};

0 commit comments

Comments
 (0)