-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
136 lines (117 loc) · 3.76 KB
/
Copy pathindex.js
File metadata and controls
136 lines (117 loc) · 3.76 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
'use strict';
const express = require('express');
const { gql, ApolloServer } = require("apollo-server");
const { Neo4jGraphQL } = require("@neo4j/graphql");
const neo4j = require("neo4j-driver");
const { BreakingChangeType } = require('graphql');
require("dotenv").config();
const handler = require('./handler.js');
const typeDefs = gql`
type Prod {
id: Int
genres: [String!]!
originalTitle: String
runtimeMinutes: Int
startYear: Int
tconst: String
title: String!
titleType: [String!]!
actors: [Person!]! @relationship(type: "ACTED_IN", direction: IN)
directors: [Person!]! @relationship(type: "DIRECTED", direction: IN)
}
type Person {
id: Int
name: String!
nconst: String
birthYear: Int!
deathYear: Int
primaryProfession: [String!]!
moviesActed: [Prod!]! @relationship(type: "ACTED_IN", direction: OUT)
productions: [Prod!]! @relationship(type: "DIRECTED", direction: OUT)
}
`;
const driver = neo4j.driver(
process.env.NEO4J_URI,
neo4j.auth.basic(process.env.NEO4J_USER, process.env.NEO4J_PASSWORD)
);
const neoSchema = new Neo4jGraphQL({ typeDefs, driver });
neoSchema.getSchema().then((schema) => {
const server = new ApolloServer({
schema: schema
});
server.listen().then(({ url }) => {
//Default localhost:4000
console.log(`GraphQL server ready on ${url}`);
});
});
const server = express();
server.use(express.json());
server.use(express.urlencoded({ extended: false }));
server.get('*', (req, res) => {
res.send("Everything Working! Send a POST to /theatro");
});
server.post('/theatro', async (req, res) => {
const intent = req.body.queryResult.intent.displayName ?? '';
const movies = req.body.queryResult.parameters.movie ?? [];
const people = req.body.queryResult.parameters.person ?? [];
const categories = req.body.queryResult.parameters.categories ?? [];
//Convert person object to string
for (let i = 0; i < people.length; i++)
people[i] = people[i].name;
//Convert movies, people & categories to CamelCase
for (let i = 0; i < movies.length; i++)
movies[i] = handler.titleCase(movies[i]);
for (let i = 0; i < people.length; i++)
people[i] = handler.titleCase(people[i]);
for (let i = 0; i < categories.length; i++)
categories[i] = handler.titleCase(categories[i]);
//Handle the intent
let fulfillmentMessage;
switch (intent) {
case 'recommendation':
try{
fulfillmentMessage = await handler.handleRecommendation(movies, people, categories);
} catch(e){
fulfillmentMessage =
{
"text": {
"text": [
"Sorry, I haven't found anything on our database😟"
]
}
};
}
break;
case 'information':
try{
fulfillmentMessage = await handler.handleSearch(movies, people);
} catch(e){
fulfillmentMessage =
{
"text": {
"text": [
"Sorry, I haven't found anything on our database😢"
]
}
};
}
break;
default:
fulfillmentMessage =
{
"text": {
"text": [
"Error"
]
}
};
}
return res.json({
"fulfillmentMessages": [
fulfillmentMessage
]
});
});
server.listen((process.env.PORT || 3000), () => {
console.log("Server is up and running on http://localhost:3000");
});