So I am about to graduate in my school, and I’m creating an ECommerce shop using GRAPHQL and MERN. Except the problem is I quite suck at GRAPHQL/MERN. I’m trying to grasp it. And i do quite get SOME stuff of it such as mutation and query BUT not in localhost:3001/GRAPHQL, but we simply started this last month… I’m not an expert at this but we HAVE to incorporate GRAPHQL and MERN in a project.
So I am trying my best to fix any issues myself and I have spent a whole entire day working on it. From morning to almost midnight, I can’t seem to figure out the issue when trying to create an ID. I’ve tried doing
createProduct: async(parent,{name,price,description}) => {
return Product.create({name,price,description});
}
and incorporating the ID in updateProduct, instead.
updateProduct: async(parent,{id,name,price,description}) => {
return Product,fineOneandUpdate(
{_id: id},
{$set:{name,price,description}},
{new:true}
);}, // Comma to go to the deleteProduct
const resolvers = {
Query: {
getProduct: async (parent, {productId}) =>{
return Product.find.one({
_id: productId //specific product
});
},
getAllProduct: async() => {
return Product.find({productId}); //empty ID might return all products
}
},
Mutation:{
createProduct: async(parent,{id,name,price,description}) => {
return Product.create({id,name,price,description}); //mutation will create new product
},
updateProduct: async(parent,{id,name,price,description}) => {
return Product.findOneAndUpdate( //mutation will update product
{_id: id},
{$set: {name,price,description}},
{new: true}
);
},
deleteProduct: async(parent,{id}) => {
return Product.findOneAndDelete({_id: id}); //mutation will delete product
}
}
}
const {resolvers} = require('./resolvers');
const typeDefs = `
type Product {
_id: ID!
name: String!
price: Float!
description: String!
}
type Query {
getProduct(productId:ID!): Product
getAllProduct: [Product]
}
type Mutation {
createProduct(id: ID!,name: String!, price: Float!, description: String!): Product
updateProduct(id: ID!, name: String!, price: Float!, description: String!): Product
deleteProduct(id: ID!): Product
}
`;
Then for the models, i have three files, index.js,items.js,Tech.js(Ignore this one).
The items.js file has
const { Schema, model } = require('mongoose');
const itemSchema = new Schema({
item1: {
id:{
type: Number,
required: true
},
name: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
},
description: {
type: String,
required: true,
},
},
item2: {
name: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
},
description: {
type: String,
required: true,
},
},
item3: {
name: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
},
description: {
type: String,
required: true,
},
},
item4: {
name: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
},
description: {
type: String,
required: true,
},
},
item5: {
name: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
},
description: {
type: String,
required: true,
},
},
item6: {
name: {
type: String,
required: true,
},
price: {
type: Number,
required: true,
},
description: {
type: String,
required: true,
},
},
},
);
const Items = model('items', itemSchema);
module.exports = Items;
I’ve trying incorporating the ID in their too. But i can’t seem to tell what i’m doing wrong because in “localhost:3001/graphql”, i do
query Query($productId: ID!) {
getProduct(productId: $productId) {
_id
name
description
}
}
and in return I get
{
"data": {},
"errors": [
{
"message": "Variable "$productId" of required type "ID!" was not provided.",
"locations": [
{
"line": 1,
"column": 13
}
],
"extensions": {
"code": "BAD_USER_INPUT",
"stacktrace": [
"GraphQLError: Variable "$productId" of required type "ID!" was not provided."