Giter Site home page Giter Site logo

nodejs-graphql's Introduction

Grahpql Nodejs

La base de una API con graphql son los esquemas describen detalladamente todos los modelos, tipos de datos e información que tendrá el API.

  • Comando para generar un nuevo proyecto npx license mit > LICENSE && npx gitignore node && git init && npm init -y

Query

Las query permiten hacer perticiones (Request) a la API para obtener información con la característica de definir que consulta quiere ejecutar y que campos de esa consulta desea obtener.

Equivaldría al método GET en Rest APIs.

Mutation

Las mutaciones son un mecanimos para gestionar (Crear, Actualizar y Borrar) datos en el API.

Equilvaldría a los métodos POST, PUT, DELETE, PATCH en Rest APIs.

Alias & Fragments

# Alias
# Sirve para renombrar consultas y ademas hacer varias a la vez.   
 query Aliases{
   allCourses: getCourses{
     ...CourseFields
   }
   
   Course1: getCourse(id: "5e67a92d1fd93b2db878c8f5"){
     ...CourseFields
     people {
       _id
       document
     }
   }
 }
 

# Fragments
# Resume el codigo requerido por la query cuando se consultan campos iguales. 
 fragment CourseFields on Course {
 	_id
   title
   description
 }

Query variables

mutation AddPersonToCourse ($course: ID!, $student: ID!) {
  addPersonToCourse(courseID: $course studentID: $student) {
    _id
    title
    people{
      document
      name
    }
  }
}

# Query Variables
{
  "course": "5e67a92d1fd93b2db878c8f5",
  "student": "5e67a1b66f3b030e40343aec"
}

Variables of type input

mutation createCourse ($createInput: CourseCreateInput!) {
  createCourse(input: $createInput) {
    _id
    title
    title
    description
    topic
   	level
  }
}

# query variables
{
  "createInput": {
    "title": "Titulo de prueba",
    "teacher": "Oscar Amado",
    "description": "Descripcion del curso",
    "topic": "Progra",
    "level": "advanced"
  }
}

Interfaces

Nos permiten agrupar diferentes tipos y usarlos para relacionarlos con otros tipos.

fragment personAttributes on Person {
  _id
  document
  name
  email
}

query allData{
  Courses: getCourses{
    _id
    title
    level
  }
  
  People: getPeople{
    ...personAttributes
    ... on Monitor {
      phone
    }    
  }
  
  FirstPerson: getPerson(id: "5e67a1b66f3b030e40343aec") {
    ...personAttributes
  }
  
	getMonitor: getPerson(id: "5e67f17f751ac21914866d9b") {
  	...personAttributes
    ... on Monitor {
      phone
    }
  }
    
}

mutation createMonitor($monitorInput: PersonCreateInput!){
  createPerson(input: $monitorInput){
    ...personAttributes    
  }
}

# Query variables
{
  "monitorInput": {
    "document": "122f345",
    "name": "Oscar",
    "email": "[email protected]",
    "phone": "xd"
  }
}

Directivas

Son instrucciones que permiten agregar validaciones a las queries.

query getPeopleData($monitor: Boolean!, $avatar: Boolean!) {
  getPeople{
    _id
    name
    ... on Monitor @include(if: $monitor) {
      phone
    }
    ... on Student @include(if: $avatar) {
      avatar
    }
  }
}
# Query variables
{
  "monitor": true,
  "avatar": true
}

Union

query AllData {
  getCourses {
    title
  }
  getPeople {
    name
  }
}

query Search {
  searchItems(keyword: "oscar") {
    __typename    
    ... on Course {
      title
      teacher
      description
    }
    ... on Monitor {
      document
      name
      phone
    }
    ... on Student {
      document
      email
      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.