Skip to content

Instantly share code, notes, and snippets.

@ssaumyaranjan7
ssaumyaranjan7 / employee.js
Created June 23, 2019 19:43
This is the model files
const mongoose = require(`mongoose`);
let employeeSchema = new mongoose.Schema({
firstName: {
type: String,
default: ""
},
lastName: {
type: String,
default: ""
@ssaumyaranjan7
ssaumyaranjan7 / employee.resolvers.js
Created June 23, 2019 19:35
This is the resolver files
// Imported the employee model from the models directory
const models = require(`../models/index`);
const employeeResolvers = {
Query: {
/**
* This method will return an array of employees according to the query
*/
employees: (root, args, context, info) => {
/**
@ssaumyaranjan7
ssaumyaranjan7 / employee.type.js
Last active June 23, 2019 19:00
This is the typedefs file
const { gql } = require('apollo-server-express');
const employeeType = gql`
# Defined the query type
type Query {
employees: [employee!]
employee(id: ID!): employee
}
# Defined the mutation type
type Mutation {
@ssaumyaranjan7
ssaumyaranjan7 / index.js
Last active June 23, 2019 18:37
This file is for typedefs index
// Imported a message type
const messageType = require(`./message.type`);
// Imported a employee type
const employeeType = require(`./employee.type`)
// Both the types are indexed and combined into an array
const typeDefs = [messageType, employeeType];
// Exported the typedefs to entry file
module.exports = { typeDefs };
@ssaumyaranjan7
ssaumyaranjan7 / index.js
Created June 23, 2019 18:12
This is the index file of GraphQL part-2
const express = require('express');
const mongoose = require(`mongoose`);
const { ApolloServer } = require('apollo-server-express');
// Imported the typeders and resolver
const { typeDefs } = require( `./typedefs`);
const { resolvers } = require( `./resolvers`);
// Created a instance of Apollo server with typedefs and resolvers.
const server = new ApolloServer({typeDefs, resolvers});
const app = express();
(async () => {
@ssaumyaranjan7
ssaumyaranjan7 / index.js
Created June 22, 2019 16:00
This is the index.js file of Hello world GraphQL project.
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
// This is the schema types
const typeDefs = gql`
type Query {
hello: String
}
`;
@ssaumyaranjan7
ssaumyaranjan7 / utils.go
Created June 9, 2019 07:39
This is the utility package.
package utils
import (
"encoding/json"
"io/ioutil"
"net/http"
)
func ParseBody(r *http.Request, x interface{}) {
if body, err := ioutil.ReadAll(r.Body); err == nil {
@ssaumyaranjan7
ssaumyaranjan7 / book.go
Created June 9, 2019 07:36
This is the file for book model.
package models
import (
"github.com/example/simple-REST/pkg/config"
"github.com/jinzhu/gorm"
)
var db *gorm.DB
type Book struct {
@ssaumyaranjan7
ssaumyaranjan7 / app.go
Created June 9, 2019 07:31
This is the database configuration file for MySQL.
package config
import (
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/mysql"
)
var (
db * gorm.DB
)
package controllers
import (
"encoding/json"
"fmt"
"github.com/example/simple-REST/pkg/utils"
"github.com/gorilla/mux"
"net/http"
"strconv"
"github.com/example/simple-REST/pkg/models"