Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
Insert data from javascript file into mongodb using graphql

I am new to using Graphql and MongoDB. I am trying to insert data from an existing javascript file where the data has been defined. I was trying to use a mutation in order to achieve this but I have no clue what I'm really doing. Any help would be nice.

const dotenv = require('dotenv');

dotenv.config();

const { ApolloServer, gql } = require('apollo-server');

const { MongoClient } = require('mongodb');

const  items  = require('./itemsListData');



    const typeDefs = gql`
    type Query {

      items:[Item!]!

    }

    
    type Item{
      id:ID!,
      name:String!,
      aisle:String!,
      bay:String!,
      price:Float!,
      xVal:Int!,
      yVal:Int!
    }
    type Mutation {
        createItem(name: String!, aisle: String!): Item!
    }
`;
   



console.log(items)

const resolvers = {

Query:  {

 items:() => items,

  },


Item:{

id: ( { _id, id }) => _id || id,

},


Mutation: {
  createItem: async(_, { name }, { db }) => {
    //  name:String!, bays:[Bay!]!, xStartVal:Int!, xEndVal:Int!, yStartVal:Int!, yEndVal:Int!
    const newItem = {
        items
    }

    // insert Item object into database
    const result = await db.collection('Items').insert(newItem);
    console.log("This is the result " + result);
    return result.ops[0]; // first item in array is the item we just added
  }
}



};




const start = async () => {

  const client = new MongoClient("mongodb+srv://admin:admin@quickkartcluster.o0bsfej.mongodb.net/test", { useNewUrlParser: true, useUnifiedTopology: true });
  

  await client.connect();

  const db = client.db("QuickKart");
  

 

  const context = {

    db,

  }

  

  const server = new ApolloServer({

      typeDefs,

      resolvers,

      context,

      introspection: true

  }); 


  // The `listen` method launches a web server.
  server.listen().then(({ url }) => {
    console.log(`๐Ÿš€  Server ready at ${url}`);
});


}



start();

here is my javascript data file

https://pastebin.com/wvGANBgR

almost 4 years ago ยท Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!