Skip to content

Latest commit

 

History

History
87 lines (70 loc) · 2.97 KB

README.md

File metadata and controls

87 lines (70 loc) · 2.97 KB

GraphQL backend

Skeleton NodeJS project for a graphQL server.

This package integrates the code generated by code-generator.

Code-generator will generate four folders with the models' information:

  • models
  • schemas
  • resolvers
  • migrations

After getting ready the generated code for the models, proceed with the server set up.

Set up

Clone the repository and run:

$ npm install
$ node_modules/.bin/sequelize db:migrate
$ node server.js

$ node_modules/.bin/sequelize db:migrate command will create the tables specified in the migrations folder. With credential as in config/config.json file.

Example of use

If you followed the example for generating the code described here, you can try the next queries and mutations. Otherwise, just adapt the same queries and mutations for your own models generated.

We will add the next 4 people to our table people.

Name (firstName) Lastname (lastName) Email (email)
Albert Einstein [email protected]
Thomas Edison [email protected]
Vicent van Gogh [email protected]
Ludwig Beethoven [email protected]

CREATE PERSON

curl -XPOST http://localhost:3000/graphql -H 'Content-Type: application/graphql' -d 'mutation M { addPerson(firstName: "Albert", lastName: "Einstein", email: "[email protected]"){ firstName email } }'

As result we will get firsName and email of the person just created:

{
  "data": {
    "addPerson": {
      "firstName": "Albert",
      "email": "[email protected]"
    }
  }
}

In the same way we add the next 3 people:

curl -XPOST http://localhost:3000/graphql -H 'Content-Type: application/graphql' -d 'mutation M { addPerson(firstName: "Thomas", lastName: "Edison", email: "[email protected]") { firstName email } }'

curl -XPOST http://localhost:3000/graphql -H 'Content-Type: application/graphql' -d 'mutation M { addPerson(firstName: "Vicent", lastName: "van Gogh", email: "[email protected]"){ firstName email } }'

curl -XPOST http://localhost:3000/graphql -H 'Content-Type: application/graphql' -d 'mutation M { addPerson(firstName: "Ludwig", lastName: "Beethoven", email: "[email protected]"){ firstName email } }'

SEARCH PEOPLE WITH FILTER

We'll search people with 'science' as substring of their email and as result we'll get only their name and last name.

curl -XPOST http://localhost:3000/graphql -H 'Content-Type: application/graphql' -d '{ searchPerson(input:{field:email, value:{value:"%science%"}, operator:like}){ firstName lastName}}'

The result will be:

{
  "data": {
    "searchPerson": [
      {
        "firstName": "Albert",
        "lastName": "Einstein"
      },
      {
        "firstName": "Thomas",
        "lastName": "Edison"
      }
    ]
  }
}