-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
edf94a2
commit 8b3ea76
Showing
6 changed files
with
253 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
'use strict' | ||
|
||
import * as ProductService from "../services/ProductService.js"; | ||
import * as CommentService from "../services/CommentService.js"; | ||
import {typeDefs} from "./typedef.js"; | ||
|
||
export const resolvers = { | ||
Query: { | ||
product: async (parent, args, context, info) => { | ||
const numOfComments = info.fieldNodes[0] | ||
.selectionSet | ||
.selections | ||
.find(e => e.name.value == "comments") | ||
?.arguments[0] | ||
.value | ||
.value | ||
try{ | ||
return await ProductService.getProductById(args.id, numOfComments) | ||
}catch (e) { | ||
console.error(e) | ||
throw e | ||
} | ||
}, | ||
|
||
products: async (parent, args, context, info) => { | ||
const filter = args.filter | ||
const sort = args.sort | ||
|
||
const numOfComments = info.fieldNodes[0] | ||
.selectionSet | ||
.selections | ||
.find(e => e.name.value == "comments") | ||
?.arguments[0] | ||
.value | ||
.value | ||
try{ | ||
return await ProductService.getProducts(filter, sort, numOfComments) | ||
} catch (e) { | ||
console.error(e) | ||
throw e | ||
} | ||
|
||
} | ||
}, | ||
|
||
Product: { | ||
comments: async (product) => { | ||
try { | ||
return await CommentService.getCommentsById(product.comments) | ||
} catch (e) { | ||
console.error(e) | ||
throw e | ||
} | ||
} | ||
}, | ||
|
||
Mutation: { | ||
createProduct: async (parent, args) => { | ||
try{ | ||
return await ProductService.addProduct(args.createProductInput) | ||
} catch (e) { | ||
console.error(e) | ||
throw e | ||
} | ||
|
||
}, | ||
createComment: async (parent, args) => { | ||
try{ | ||
return await CommentService.addComment(args.createCommentInput, args.productId) | ||
} catch (e) { | ||
console.error(e) | ||
throw e | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
'use strict' | ||
|
||
export const typeDefs = ` | ||
scalar DateTime | ||
enum ProductCategory { | ||
STYLE | ||
FOOD | ||
TECH | ||
SPORT | ||
} | ||
enum SortingValue { | ||
createdAt | ||
price | ||
} | ||
enum SortingOrder { | ||
asc | ||
desc | ||
} | ||
input ProductCreateInput { | ||
name : String!, | ||
description : String, | ||
price : Float! @constraint(min: 0), | ||
category: ProductCategory! | ||
} | ||
input CommentCreateInput { | ||
title: String!, | ||
body: String, | ||
stars: Int! @constraint(min: 0, max: 5) | ||
} | ||
type Comment { | ||
_id: ID!, | ||
title: String!, | ||
body: String, | ||
stars: Int!, | ||
date: DateTime! | ||
} | ||
type Product { | ||
_id: ID!, | ||
name: String!, | ||
createdAt: DateTime!, | ||
description: String, | ||
price: Float!, | ||
category: ProductCategory!, | ||
stars: Float, | ||
comments (numberOfLastRecentComments: Int) : [Comment], | ||
} | ||
input FilterProductInput { | ||
categories: [ProductCategory], | ||
minStars: Int @constraint(min: 0, max: 5), | ||
minPrice: Float @constraint(min: 0), | ||
maxPrice: Float @constraint(min: 0) | ||
} | ||
input SortProductInput { | ||
value: SortingValue!, | ||
order: SortingOrder! | ||
} | ||
type Query { | ||
products (filter: FilterProductInput, sort: SortProductInput) : [Product], | ||
product (id: ID!) : Product, | ||
} | ||
type Mutation { | ||
createProduct (createProductInput: ProductCreateInput!) : Product, | ||
createComment ( | ||
createCommentInput: CommentCreateInput!, | ||
productId: ID! | ||
) : Comment | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,127 +1,23 @@ | ||
'use strict' | ||
'use strict'; | ||
|
||
import express from 'express' | ||
import {makeExecutableSchema} from 'graphql-tools' | ||
import {graphqlHTTP} from 'express-graphql' | ||
import './db.js' | ||
import * as ProductService from './services/ProductService.js' | ||
import * as CommentService from './services/CommentService.js' | ||
import {graphqlHTTP} from "express-graphql"; | ||
import {makeExecutableSchema} from "graphql-tools"; | ||
import "./db.js" | ||
import {typeDefs} from './graphql/typedef.js'; | ||
import {resolvers} from './graphql/resolver.js'; | ||
import {constraintDirective, constraintDirectiveTypeDefs} from 'graphql-constraint-directive'; | ||
|
||
const typeDefs = ` | ||
scalar DateTime | ||
enum ProductCategory { | ||
STYLE | ||
FOOD | ||
TECH | ||
SPORT | ||
} | ||
enum SortingValue { | ||
createdAt | ||
price | ||
} | ||
enum SortingOrder { | ||
asc | ||
desc | ||
} | ||
input ProductCreateInput { | ||
name : String!, | ||
description : String, | ||
price : Float!, | ||
category: ProductCategory! | ||
} | ||
input CommentCreateInput { | ||
title: String!, | ||
body: String, | ||
stars: Int! | ||
} | ||
type Comment { | ||
_id: ID!, | ||
title: String!, | ||
body: String, | ||
stars: Int!, | ||
date: DateTime! | ||
} | ||
type Product { | ||
_id: ID!, | ||
name: String!, | ||
createdAt: DateTime!, | ||
description: String, | ||
price: Float!, | ||
comments (numberOfLastRecentComments: Int) : [Comment], | ||
category: ProductCategory!, | ||
stars: Float | ||
} | ||
input FilterProductInput { | ||
categories: [ProductCategory], | ||
minStars: Int, | ||
minPrice: Float, | ||
maxPrice: Float | ||
} | ||
input SortProductInput { | ||
value: SortingValue!, | ||
order: SortingOrder! | ||
} | ||
type Query { | ||
products (filter: FilterProductInput, sort: SortProductInput) : [Product], | ||
product (id: ID!) : Product, | ||
hello: String | ||
} | ||
type Mutation { | ||
createProduct (createProductInput: ProductCreateInput!) : Product, | ||
createComment ( | ||
createCommentInput: CommentCreateInput!, | ||
productId: ID! | ||
) : Comment | ||
} | ||
` | ||
|
||
const resolvers = { | ||
Query: { | ||
hello: () => { | ||
return 'Hello Graphql....' | ||
}, | ||
product: async (parent, args, context, info) => { | ||
const numOfComments = info.fieldNodes[0] | ||
.selectionSet | ||
.selections | ||
.find(e => e.name.value == "comments") | ||
?.arguments[0] | ||
.value | ||
.value | ||
return await ProductService.getProductById(args.id, numOfComments) | ||
} | ||
}, | ||
Product: { | ||
comments: async (product) => { | ||
return CommentService.getCommentsById(product.comments) | ||
} | ||
}, | ||
Mutation: { | ||
createProduct: async (parent, args) => { | ||
return await ProductService.addProduct(args.createProductInput) | ||
}, | ||
createComment: async (parent, args) => { | ||
return await CommentService.addComment(args.createCommentInput, args.productId) | ||
} | ||
} | ||
} | ||
|
||
const schema = makeExecutableSchema({typeDefs, resolvers}) | ||
const schema = makeExecutableSchema({ | ||
typeDefs: [constraintDirectiveTypeDefs, typeDefs], | ||
resolvers, | ||
schemaTransforms: [constraintDirective()] | ||
}) | ||
|
||
const app = express() | ||
const app = express(); | ||
|
||
app.use('/graphql', graphqlHTTP({schema, graphiql: true})) | ||
|
||
app.listen(3000) | ||
app.use('/graphql', graphqlHTTP({schema, graphiql:true})); | ||
|
||
app.listen(3000); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.