Skip to content

Milchgraf/express-graphql-mongo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 

Repository files navigation

Express, GraphQL, MongoDB, React - GETTING STARTED

Required Dependencies for GraphQL-Server:

  1. express
  2. graphql
  3. express-graphql
  4. mongoose
  5. cors (optional)
npm i express graphql express-graphql mongoose cors

Create app.js file

1. import dependencies

const express = require('express');
const graphqlHTTP = require('express-graphql');
const mongoose =  require('mongoose');
const cors = require('cors');

2. import GraphQL-Schema

const schema = require('./schema/schema');

3. use express

const app = express();

4. connect to mongodb database

mongoose.connect('mongodb://localhost:27017/gql-demo');
mongoose.connection.once('open', () => {
  console.log('connected to database..');
});

5. use graphql endpoint with schema as option

app.use('/graphql', graphqlHTTP({
  schema,
  
  // optional UI:
  graphiql: true
}));

6. start server

app.listen(4000, () => {
  console.log('Listen on port 4000..');
});

[OPTIONAL] allow cors

app.use(cors());

Create schema.js file

1. import dependencies

const graphql = require('graphql');
const Book = require('../models/book');
const Author = require('../models/author');

2. import GraphQL-Datatypes

const {
  GraphQLObjectType,
  GraphQLString,
  GraphQLSchema,
  GraphQLID,
  GraphQLInt,
  GraphQLList,
  GraphQLNonNull
} = graphql;

3. create some types i.e. BookType (simple get operation)

const BookType = new GraphQLObjectType({
  name: 'Book',
  fields: () => ({
    id: { type: GraphQLID },
    name: { type: GraphQLString },
    genre: { type: GraphQLString },
    author: {
      type: AuthorType,
      resolve(parent, args) {
        return Author.findById(parent.authorId);
      }
    }
  })
});

4. create RootQuery

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {
    book: {
      type: BookType,
      args: {
        id: { type: GraphQLID }
      },
      resolve(parent, args) {
        // code to get data from db / other source
        //return _.find(books, { id: args.id });
        return Book.findById(args.id);
      }
    },
    books: {
      type: GraphQLList(BookType),
      resolve(parent, args) {
        //return books;
        return Book.find({});
      }
    }
  }
});

5. create mutations

const Mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields: {
    addBook: {
      type: BookType,
      args: {
        name: { type: new GraphQLNonNull(GraphQLString) },
        genre: { type: new GraphQLNonNull(GraphQLString) }
      },
      resolve(parent, args) {
        let book = new Book({
          name: args.name,
          genre: args.genre
        });
        return book.save();
      }
    }
  }
});

6. export GraphQLSchema

module.exports = new GraphQLSchema({
  query: RootQuery,
  mutation: Mutation
});

Create a model i.e. book.js

1. import mongoose

const mongoose = require('mongoose');

2. create mongoose-schema

const Schema = mongoose.Schema;

const bookSchema = new Schema({
  name: String,
  genre: String,
  authorId: String
});

3. export schema

module.exports = mongoose.model('Book', bookSchema);

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published