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
--config | |
--db | |
--src | |
--models | |
--resolvers | |
--schemas | |
server.ts |
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
{ | |
"name": "node-graphql-curd", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1", | |
"start": "pm2 start server.js --watch" | |
}, | |
"keywords": [], |
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
{ | |
"port": 8000, | |
"db_url": "mongodb+srv://<username>:<password>@sandbox.ejy8i.mongodb.net/graphqlDatabase?retryWrites=true&w=majority" | |
} |
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
import config from 'config' | |
import {connect} from 'mongoose' // import connect method from mongoose | |
const MONGO_URI: string = config.get('db_url') // This will get the db_url, that we've declared in config/default.json file | |
export const connectMongoDB = async () => { | |
try { | |
const res = await connect(MONGO_URI) | |
if (res) console.log('Connected to MongoDB') | |
} catch (error) { |
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
import {ApolloServerPluginDrainHttpServer} from 'apollo-server-core' | |
import {ApolloServer} from 'apollo-server-express' | |
import config from 'config' | |
import express from 'express' | |
import {GraphQLError} from 'graphql' | |
import http from 'http' | |
import {connectMongoDB} from './db/mongodb' | |
import {mainSchema} from './src/schema-loader' // will create in next steps, skip for now | |
const port = config.get('port') || 5000 |
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
import {model, Schema} from 'mongoose' | |
const user = new Schema( | |
{ | |
firstname: {type: String}, | |
lastname: {type: String}, | |
age: {type: Number}, | |
email: {type: String}, | |
password: {type: String} | |
}, |
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
scalar Date | |
type User { | |
_id: ID! | |
firstname: String! | |
lastname: String! | |
email: String! | |
age: String! | |
createdAt: Date | |
updatedAt: Date |
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
type Query { | |
getUser(id: ID!): User! | |
} |
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
import {userModel} from '../models/user' | |
export const createUserResolver = async (parent: any, args: any, context: any) => { | |
let user = await userModel.find({email: args.email}).lean() | |
if (user.length) throw new Error('User already exists!') | |
user = await userModel.create(args) | |
if (!user) throw new Error('User creation failed') | |
return user | |
} |
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
import { userModel } from '../models/user'; | |
export const getUserByIdResolver = async (parent: any, args: any, context: any) => { | |
const user = await userModel.findOne({_id: args.id}).lean() | |
if (!user) throw new Error('No user exists!') | |
return user | |
} |
OlderNewer