-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubscription-server.js
More file actions
118 lines (104 loc) · 3.32 KB
/
subscription-server.js
File metadata and controls
118 lines (104 loc) · 3.32 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
import express from "express";
import { createHandler } from "graphql-http/lib/use/express";
import { GraphQLObjectType, GraphQLInputObjectType, GraphQLSchema, GraphQLNonNull, GraphQLList, GraphQLString, GraphQLFloat, GraphQLInt, GraphQLBoolean } from "graphql";
import { PubSub } from 'graphql-subscriptions';
import { createDB } from "./codedDB.js";
import cors from "cors";
const ActorType = new GraphQLObjectType({
name: 'Actor',
fields: {
firstName: { type: new GraphQLNonNull(GraphQLString)},
secondName: { type: GraphQLString },
age: { type: GraphQLInt },
gender: { type: GraphQLString }
}
});
const MovieType = new GraphQLObjectType({
name: 'Movie',
fields: {
name: { type: new GraphQLNonNull(GraphQLString) },
rating: { type: GraphQLFloat },
length: { type: new GraphQLNonNull(GraphQLInt)},
available: { type: new GraphQLNonNull(GraphQLBoolean)},
actors: { type: new GraphQLList(ActorType)}
}
});
const RootType = new GraphQLObjectType({
name: 'Root',
fields: {
movies: { type: new GraphQLList(MovieType) },
actors: { type: new GraphQLList(ActorType) },
availableMovies: { type: new GraphQLList(MovieType) },
moviesLongerThan: {
type: new GraphQLList(MovieType),
args: {
time: { type: GraphQLInt }
}
}
}
});
const MovieInputType = new GraphQLInputObjectType({
name: 'MovieInput',
fields: {
name: { type: new GraphQLNonNull(GraphQLString) },
length: { type: new GraphQLNonNull(GraphQLInt)},
available: { type: new GraphQLNonNull(GraphQLBoolean)},
}
});
const MutationOperationsType = new GraphQLObjectType({
name: 'MutationOperations',
fields: {
setAllUnavailable: { type: new GraphQLList(MovieType) },
makeAvailable: {
type: MovieType,
args: {
name: { type: new GraphQLNonNull(GraphQLString) }
}
},
addMovie1: {
type: MovieType,
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
length: { type: new GraphQLNonNull(GraphQLInt) },
available: { type: new GraphQLNonNull(GraphQLBoolean) }
}
},
addMovie2: {
type: MovieType,
args: {
movie: { type: new GraphQLNonNull(MovieInputType) }
}
}
}
});
const SubscriptionOperationsType = new GraphQLObjectType({
name: 'SubscriptionOperations',
fields: {
movieCreated: {
type: GraphQLString,
subscribe: () => { console.log("someone just subscribed!"); return "Subscribed!"; }
}
}
});
// const schema = new GraphQLSchema({
// subscription: SubscriptionType,
// });
const schema = new GraphQLSchema({
query: RootType,
mutation: MutationOperationsType,
subscription: SubscriptionOperationsType
});
const root = createDB();
const app = express();
app.use(cors());
app.all(
"/graphql",
createHandler({
schema,
rootValue: root,
})
);
// for some reason port 4000 doesnt work??
app.listen(4001, () => {
console.log("Running a GraphQL API server at http://localhost:4001/graphql");
});