Skip to content

repl-hemanth-kodandarama/express-graphql-example

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Express, GraphQL example

License

out

How to run the project

Install dependencies :

yarn

# or

npm install

Update src/database.js file with your credentials:

import Knex from 'knex';

export default new Knex({
  client: 'mysql2',
  connection: {
    host : '127.0.0.1',
    user : 'root',
    password : '',
    database : 'express-graphql-example'
  }
});

Init the database. You can find the SQL script int database.sql.

Run the project :

npm start

Open GraphiQL in your browser http://localhost:8088/graphql

Examples

Get list of authors (it will return only first 10 authors!):

query {
  authors {
    edges {
      node {
        id
        _id
        firstName
        lastName
      }
    }
  }
}

Filter authors based of first name, also return total number of such authors:

query {
  authors(firstName: "Robert") {
    totalCount
    edges {
      node {
        id
        _id
        firstName
        lastName
      }
    }
  }
}

Order authors by first name and last name:

query {
  authors(orderBy:[
    {
      field:FIRST_NAME
      direction:ASC
    }
    {
      field:LAST_NAME
      direction:ASC
    }
  ]) {
    edges {
      cursor
      node {
        _id
        firstName
        lastName
      }
    }
  }
}

Get name of author with ID = 4:

query {
  author(id: 4) {
    id
    _id
    firstName
    lastName
  }
}

Get list of quotes:

query {
  quotes {
    edges {
      node {
        id
        _id
        quote
      }
    }
  }
}

Create new author:

mutation {
  createAuthor(input:{
    firstName:"Kent"
    lastName:"Beck"
  }) {
    id
    _id
    firstName
    lastName
  }
}

Update existing author:

mutation {
  updateAuthor(input:{
    id: 1
    firstName: "JOHN"
    lastName: "JOHNSON"
  }) {
    id
    _id
    firstName
    lastName
  }
}

Old version

Here is a link to an old version, that used sequelize and did not use connections:

License

MIT license

About

Example project how to use Express and GraphQL

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 84.5%
  • TSQL 15.5%