Git Product home page Git Product logo

cf-graphql-crud's Introduction

CRUD with GraphlQL

Getting started with Express & GraphQL

Necessary packages

npm init -y
npm i express express-graphql graphql
npm i nodemon -D

Create the file server.js on root

const express = require('express');
const { buildSchema } = require('graphql');

const app = express();
const port = process.env.PORT || 8080;

const courses = require('./courses');
const { graphqlHTTP } = require('express-graphql');

const schema = buildSchema(`
    type Course{
        id:  ID!
        title: String!
        views: Int
    }

    type Query{
        getCourses: [Course]
        getCourse(id: ID!): Course
    }
`);

const root = {
    getCourses(){
        return courses;
    },
    getCourse({ id }){
        console.log(id);
        const course = courses.find( (course) => course.id == id)
        return course;
    }
}

app.get('/', function (req, res) {
    res.json(courses);
});

// Middleware
app.use('/graphql', graphqlHTTP({
    schema,
    rootValue: root,
    graphiql: true
}));

app.listen(port, function(){
    console.log(`Servidor iniciado en http://localhost:${ port }`);
})

Create a mocking data for courses on the courses.js

const courses = [
    { id: "1", title: "Curso de Golang", views: 1200 },
    { id: "2", title: "Curso de GraphQL", views: 1000 }
]

module.exports = courses;

Create a script for run server on package.json scripts

 "scripts": {
    "start": "nodemon server.js"
  },

Queries on GraphQL

Creating a simples Queries on GraphiQL

{
  getCourse(id: 1) {
    title
  }
  getCourses{
    title
  }
}

Example of return

{
  "data": {
    "getCourse": {
      "title": "Curso de Golang"
    },
    "getCourses": [
      {
        "title": "Curso de Golang"
      },
      {
        "title": "Curso de GraphQL"
      }
    ]
  }
}

Creating Queries with query variables

// GraphiQL

query($id:ID!){
  getCourse(id:$id){
    id
    title
    views
  }
}

// Query variables
{
  "id": 2
}

Example of return

{
    "data": {
        "getCourse": {
            "id": "2",
            "title": "Curso de GraphQL",
            "views": 1000
        }
    }
}

Mutations on GraphQL

Creating a simples Mutations on GraphiQL, this needs specify the word mutation because by default is query and is optional.

mutation {
  addCourse(title: "Curso de Express", views: 345) {
    title
  }
}

Response

{
    "data": {
        "addCourse": {
            "title": "Curso de Express"
        }
    }
}

Creating Mutations with query variables, here we need to consider that CourseInput is an Objet

// GraphiQL

mutation($entrada: CourseInput) {
  addCourse(input: $entrada){
    title
  }
}

// Query variables
{
  "entrada": {
    "title": "Hola Curso de Golang3",
    "views": 16111
  }
}

Example of return

{
  "data": {
    "addCourse": {
      "title": "Hola Curso de Golang3"
    }
  }
}

Creating Mutations for method DELETE

// GraphiQL

mutation($id: ID!) {
    deleteCourse(id: $id){
        message
    }
}


// Query variables
{
  "id": 2
} 

// o
mutation{
    deleteCourse(id: "2"){
        message
    }
}

Example of return

{
    "data": {
        "deleteCourse": {
            "message": "El curso con el id: 2 fue eliminado"
        }
    }
}

cf-graphql-crud's People

Contributors

benji-mtz 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.