A small project to illustrate using GraphQL Queries with a REST API combined with the Facebook DataLoader library.
- Create a GraphQL + Express + Loader project that is as __simple as possible
- GraphiQL Integration to send Queries / Mutations
- Ability to use Postman or other REST Client to POST GraphQL Queries / Mutations
- Use of ES6 (but minimally)
- Use GraphQL queries to make REST API calls using Facebook DataLoader so that the number of API requests is reduced to a minimum
This project makes use of the Star Wars REST API at http://swapi.co/ to demonstrate a GraphQL Server running on Express integrated with Facebook DataLoder to cache and reduce the number of API calls required to return JSON data.
This project makes use of ES6 which requires a 4+ version of Node https://nodejs.org/en/download/
The following NPM modules are required in package.json:
- express
- express-graphql
- graphql
- axios
- dataloader
- babel-cli
- babel-preset-es2015
- nodemon
Install with:
npm install
npm dev is configured with nodmon so that the server automatically restarts when code files are changes
npm run dev
npm start
npm prestart will run first, transpile the ES6 code and save to dist folder. npm start will then run the code directly from the dist folder
You can run these queries within GraphiQL, alternatively you can run them within a tool such as Postman. To do so ensure you POST the query / mutation in the body and set the content-type to GraphQL.
query {
film (id:"1") {
title,
director
}
}
query {
vehicle(id: "8") {
name
}
}
query {
starship(id:"5") {
name
}
}
query {
species(id:"1") {
name
}
}
query {
planet(id: "1") {
name
}
}
query {
character (id: "1") {
name,
height,
mass,
hair_color,
skin_color,
eye_color,
birth_year,
gender,
homeworld {
name
rotation_period
orbital_period
diameter
climate
gravity
terrain
surface_water
population
residents {
name
birth_year
gender
}
created
edited
},
films {
title
director
},
species {
name,
classification,
designation,
language,
people {
name
birth_year
gender
},
films {
title
director
}
}
vehicles{
name,
model,
cost_in_credits,
length,
crew,
passengers,
vehicle_class,
pilots {
name
},
films {
title
}
}
starships{
name,
model,
cost_in_credits,
length,
crew,
passengers,
hyperdrive_rating,
starship_class,
pilots {
name
},
films {
title
}
}
created,
edited
}
}