· 6 years ago · Mar 14, 2019, 12:06 PM
1films table:
2
3 module.exports =(sequelize, DataTypes) => {
4 const films = sequelize.define(
5 'films',
6 {
7 id:{
8 type: DataTypes.INTEGER,
9 primaryKey: true,
10 allowNull: false,
11
12 },
13 name: {
14 type: DataTypes.STRING,
15
16 },
17 },
18
19 );
20
21 films.associate = (models) => {
22
23
24 films.hasMany(models.movie_stream, {
25 foreignKey: 'movie_id',
26 });
27
28};
29
30 return films;
31
32}
33
34movie_stream table:
35
36 module.exports = (sequelize, DataTypes) => {
37 const movie_streams = sequelize.define('movie_streams', {
38 id:{
39 type: DataTypes.INTEGER,
40 primaryKey: true,
41 allowNull: false,
42 },
43 movie_id: {
44 type: DataTypes.STRING,
45 foreignKey: "movie_id",
46
47 },
48 });
49
50 movie_streams.associate = (models) => {
51
52 movie_streams.hasMany(models.films, {
53 foreignKey: 'id',
54 });
55 };
56
57 return movie_streams;
58};
59
60Schema file:
61
62movieList:{
63 type: new GraphQLList(Films),
64 resolve: (parent,args)=>{
65return newdb.films.findAll({attributes:['id','name','permalink'],
66where: {content_category_value:parent.id },
67include: [{
68 model:newdb.movie_stream,
69 attributes:['id','movie_id'],
70}],
71}).then(data=>{
72 return data;
73})
74}
75
76interface Product {
77 price: Float
78 barcode: Int
79 shelfLocation: ShelfLocation
80}
81
82type Bread implements Product {
83 price: Float
84 barcode: Int
85 shelfLocation: ShelfLocation
86 brand: String
87 numberOfSlices: Int
88 calories: Float
89 bestBefore: Date
90}
91
92extend type Query {
93 searchProducts(phrase: String!): [Product!]
94}
95
96type Shark {
97 name: String
98 numberOfTeeth: Int
99}
100
101type Shoe {
102 brand: String
103 size: String
104}
105
106union SharkOrShoe = Shark | Shoe
107
108extend type Query {
109 searchSharksAndShoes(phrase: String!): [SharkOrShoe!]
110}
111
112query {
113 searchProducts(phrase: "tasty") {
114 # shared fields
115 __typename
116 price
117 barcode
118 shelfLocation { aisle, position }
119
120 # type specific fields
121 ... on Bread { brand }
122 ...breadFrag
123 }
124 searchSharksAndShoes(phrase: "sleek") {
125 # only the introspection fields are shared in a union
126 __typename
127
128 # type specific fields
129 ... on Shark { name, numberOfTeeth }
130 ...shoeFrag
131 }
132}
133
134fragment breadFrag on Bread {
135 barcode
136 bestBefore
137}
138
139fragment shoeFrag on Shoe {
140 brand
141 size
142}
143
144const Films = new GraphQLInterfaceType({
145 name: 'films',
146
147fields: () => ({
148
149 id:{
150 type: GraphQLID
151 },
152 name: {
153 type: GraphQLString,
154
155 },
156
157
158 })
159})
160
161const MovieStream = new GraphQLObjectType({
162 name: 'MovieStream',
163 interfaces: () => [Films],
164 fields: () => ({
165 id: {
166 type: GraphQLID,
167
168 },
169
170 movie_id: {
171 type: GraphQLString,
172
173 },
174
175})
176})
177
178{
179 "errors": [
180 {
181 "message": "Query root type must be Object type, it cannot be { __validationErrors: undefined, __allowedLegacyNames: [], _queryType: undefined, _mutationType: undefined, _subscriptionType: undefined, _directives: [@include, @skip, @deprecated], astNode: undefined, extensionASTNodes: undefined, _typeMap: { __Schema: __Schema, __Type: __Type, __TypeKind: __TypeKind, String: String, Boolean: Boolean, __Field: __Field, __InputValue: __InputValue, __EnumValue: __EnumValue, __Directive: __Directive, __DirectiveLocation: __DirectiveLocation, films: films, ID: ID, Date: Date, JSON: JSON, MovieStream: MovieStream }, _possibleTypeMap: {}, _implementations: { films: [] } }."
182 },
183 {
184 "message": "Expected GraphQL named type but got: { __validationErrors: undefined, __allowedLegacyNames: [], _queryType: undefined, _mutationType: undefined, _subscriptionType: undefined, _directives: [@include, @skip, @deprecated], astNode: undefined, extensionASTNodes: undefined, _typeMap: { __Schema: __Schema, __Type: __Type, __TypeKind: __TypeKind, String: String, Boolean: Boolean, __Field: __Field, __InputValue: __InputValue, __EnumValue: __EnumValue, __Directive: __Directive, __DirectiveLocation: __DirectiveLocation, films: films, ID: ID, Date: Date, JSON: JSON, MovieStream: MovieStream }, _possibleTypeMap: {}, _implementations: { films: [] } }."
185 }
186 ]
187}
188
189const QueryRoot = new GraphQLObjectType({
190
191 name: 'Query',
192 fields: () => ({
193 getContentList:{
194 type: new GraphQLList(contentCategory),
195 args: {
196 id: {
197 type: GraphQLInt
198 },
199 permalink: {
200 type: GraphQLString
201 },
202 language: {
203 type: GraphQLString
204 },
205 content_types_id: {
206 type: GraphQLString
207 },
208 oauth_token:{
209 type: GraphQLString
210 }
211
212 },
213
214 resolve: (parent, args, context, resolveInfo) => {
215 var category_flag = 0;
216 var menuItemInfo = '';
217 user_id = args.user_id ? args.user_id : 0;
218 // console.log("context"+context['oauth_token']);
219 return AuthDb.models.oauth_registration.findAll({attributes: ['oauth_token', 'studio_id'],where:{
220 // oauth_token:context['oauth_token'],
221 $or: [
222 {
223 oauth_token:
224 {
225 $eq: context['oauth_token']
226 }
227 },
228
229 {
230 oauth_token:
231 {
232 $eq: args.oauth_token
233 }
234 },
235 ]
236
237 },limit:1}).then(oauth_registration => {
238 var oauthRegistration = oauth_registration[0]
239 // for(var i = 0;i<=oauth_registration.ength;i++){
240 if(oauth_registration && oauthRegistration && oauthRegistration.oauth_token == context['oauth_token'] || oauthRegistration.oauth_token == args.oauth_token){
241 studio_id = oauthRegistration.studio_id;
242 return joinMonster.default(resolveInfo,{}, sql => {
243 return contentCategoryDb.query(sql).then(function(result) {
244
245 return result[0];
246 });
247 } ,{dialect: 'mysql'});
248
249 }else{
250 throw new Error('Invalid OAuth Token');
251 }
252
253 })
254
255 },
256
257 where: (filmTable, args, context) => {
258 return getLanguage_id(args.language).then(language_id=>{
259 return ` ${filmTable}.permalink = "${args.permalink}" and ${filmTable}.studio_id = "${studio_id}" and (${filmTable}.language_id = "${language_id}" OR ${filmTable}.parent_id = 0 AND ${filmTable}.id NOT IN (SELECT ${filmTable}.parent_id FROM content_category WHERE ${filmTable}.permalink = "${args.permalink}" and ${filmTable}.language_id = "${language_id}" and ${filmTable}.studio_id = "${studio_id}"))`
260 })
261
262 },
263
264 }
265
266 })
267})
268
269module.exports = new GraphQLSchema({
270 query: QueryRoot
271})