Skip to content
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
178 changes: 177 additions & 1 deletion server/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "tsx watch src/index.ts",
"start": "tsx src/index.ts"
},
"scripts": {
"dev": "tsx watch src/index.ts",
"start": "tsx src/index.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"@apollo/server": "^5.5.1",
"graphql": "^16.14.2"
"dotenv": "^18.0.5",
"graphql": "^16.14.2",
"pg": "^8.23.1"
},
"devDependencies": {
"@types/node": "^26.6.4",
"@types/pg": "^8.23.1",
"tsx": "^4.23.15",
"typescript": "^7.0.2"
}
Expand Down
12 changes: 12 additions & 0 deletions server/src/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pg from "pg";
import "dotenv/config";

const { Pool } = pg;

export const pool = new Pool({
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
database: process.env.DB_NAME,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
});
71 changes: 71 additions & 0 deletions server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,86 @@
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import { pool } from "./db.js";

const typeDefs = `#graphql
type Query {
health: String!
}
type Wine {
id: ID!
name: String!
type: String!
abv: Float
body: String
acidity: String
winery: String!
region: String!
country: String!
avgRating: Float
ratingCount: Int!
}
type WinePage {
items: [Wine!]!
totalCount: Int!
}
type Query {
health: String!
wines(
limit: Int = 20
offset: Int = 0
): WinePage!
}
`;

const resolvers = {
Query: {
health: () => "OK",

wines: async (
_: unknown,
args: { limit?: number; offset?: number }
) => {
const limit = args.limit ?? 20;
const offset = args.offset ?? 0;

const winesResult = await pool.query(
`
SELECT
w.id,
w.name,
w.type,
w.abv,
w.body,
w.acidity,
wi.name AS winery,
r.name AS region,
c.name AS country,
w.avg_rating AS "avgRating",
w.rating_count AS "ratingCount"
FROM wines w
JOIN wineries wi ON w.winery_id = wi.id
JOIN regions r ON w.region_id = r.id
JOIN countries c ON w.country_code = c.code
ORDER BY w.name, w.id
LIMIT $1
OFFSET $2
`,
[limit, offset]
);

const countResult = await pool.query(
`SELECT COUNT(*) FROM wines`
);

return {
items: winesResult.rows,
totalCount: Number(countResult.rows[0].count),
};
},
},
};

Expand Down