-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
121 lines (105 loc) · 3.3 KB
/
index.js
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { ApolloServer } from '@apollo/server';
import { startStandaloneServer } from '@apollo/server/standalone';
import {
getName,
getHostId,
getHostName,
getNeighbourhood,
getLatitude,
getLongitude,
getRoomType,
getPrice,
getMinimumNights,
getNumberOfReviews,
getLastReview,
getReviewsPerMonth,
getCalculatedHostListingsCount,
getAvailability365,
getIds
} from './database_queries.js';
import { MongoClient, ServerApiVersion } from 'mongodb';
import 'dotenv/config'
const password = process.env.MONGO_PASSWORD;
const username = process.env.MONGO_USERNAME;
const uri = `mongodb+srv://${username}:${password}@cluster0.vvmtyfv.mongodb.net/?retryWrites=true&w=majority`;
const client = new MongoClient(uri, {
serverApi: {
version: ServerApiVersion.v1,
strict: true,
deprecationErrors: true,
}
});
async function startServer() {
try {
await client.connect();
console.log('Connected to MongoDB Atlas');
const typeDefs = `#graphql
# Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
# This "Book" type defines the queryable fields for every book in our data source.
type Publication {
name: String
host_id: Int
host_name: String
neighbourhood: String
latitude: Float
longitude: Float
room_type: String
price: Float
minimum_nights: Int
number_of_reviews: Int
last_review: String
reviews_per_month: Float
calculated_host_listings_count: Int
availability_365: Int
}
# The "Query" type is special: it lists all of the available queries that
# clients can execute, along with the return type for each. In this
# case, the "books" query returns an array of zero or more Books (defined above).
type Query {
publication(id: String): Publication
getIds(page: Int): [String]
}
`;
const resolvers = {
Query: {
publication: (_, { id }) => {
// Aquí utilizamos las funciones para obtener los campos específicos por ID
const publicationData = {
name: getName(id),
host_id: getHostId(id),
host_name: getHostName(id),
neighbourhood: getNeighbourhood(id),
latitude: getLatitude(id),
longitude: getLongitude(id),
room_type: getRoomType(id),
price: getPrice(id),
minimum_nights: getMinimumNights(id),
number_of_reviews: getNumberOfReviews(id),
last_review: getLastReview(id),
reviews_per_month: getReviewsPerMonth(id),
calculated_host_listings_count: getCalculatedHostListingsCount(id),
availability_365: getAvailability365(id),
};
return publicationData;
},
getIds: (_, { page }) => {
const itemsPerPage = 20;
const skip = (page - 1) * itemsPerPage;
const ids = getIds(skip, itemsPerPage);
return ids;
}
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
});
const { url } = await startStandaloneServer(server, {
listen: { port: 4000 },
});
console.log(`🚀 Server ready at: ${url}`);
} catch (err) {
console.error(err);
}
}
startServer();