GithubHelp home page GithubHelp logo

501.graphql-express's Introduction

GraphQL - Demo

Date: 2022/07 by Adem

The sample is learn from Web Dev Simplified

Contents

Summary

This simple demo is to create a graphQL to CRUD data.

Install Package

npm i  express express-graphql graphql
npm i --save-dev nodemon

Change package.json content

  • main : server.js -> start to server.js
  • script: nodemon server.js ->use nodemon to keep refreshing server when you update the code
{
  "name": "501.graphql-demo",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "scripts": {
    "devStart":"nodemon server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.1",
    "express-graphql": "^0.12.0",
    "graphql": "^16.5.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.19"
  }
}

Create a simple Server

create server.js file and type these codes

//server.js
const express = require("express");
const app = express();

app.listen(5000, () => console.log("Server Running"));

Then run server, and you will see the console log Server Running

> npm run devStart

[nodemon] 2.0.19
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node server.js`
Server Running

But when you open the browser localhost:5000,you will get some Error Cannot GET / because you do not already write middleware. Let's create a middleware in next step

Create a middleware

//server.js
...
app.use(
  "/graphql",
  expressGraphQL({
    schema: schema,
    graphiql: true,
  })
);

Create GraphQL Object Type

//server.js
...
const BookType = new GraphQLObjectType({...})
const AuthorType = new GraphQLObjectType({...})
const RootQueryType = new GraphQLObjectType({...})
const RootMutationType = new GraphQLObjectType({...})

Create GraphQL Schema

//server.js
...
const schema = new GraphQLSchema({
  query: RootQueryType,
  mutation: RootMutationType,
});

Create Test Data

//server.js
...
const authors = [...]
const books = [...]

501.graphql-express's People

Contributors

ademkao avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.