GraphQL API with fruit tree data. This API is built with Apollo Server Lambda + Netlify Lambda.
In this project I am not using a database. The data is in data.js, in case you want to add more information :)
Read the CONTRIBUTING.md file or check if there are any issues, all PRs are welcome.
Playground: https://fruits-api.netlify.app/graphql
Web: https://fruit-api.netlify.app/
- Clone repository:
git clone https://github.com/Franqsanz/fruits-api.git
- Enter to the repository directory:
cd fruits-api
- Install all dependencies:
npm install
# Or with yarn
yarn
- Once all the dependencies are installed run the following command:
npm start
yarn start
- Run Testing (optional):
npm test
yarn test
As an alternative, you can use Docker
- Open your terminal/command line
- Run
docker-compose up
Once the API is executed, if all goes well you should see in your terminal the following message: Lambda server is listening on 9000
, open your browser and put in the URL http://localhost:9000/graphql
and this will load the GraphQL Playground, you should see the following:
Obviously the playground will be empty, you should make the queries.
This documentation will help you become familiar with the Fruits API resources and show you how to make different queries.
Key | Type | Description |
---|---|---|
id | ID | Tree ID |
scientific_name | String | Scientific name of the tree |
tree_name | String | Tree name |
fruit_name | String | Fruit name |
family | String | Tree family type |
origin | String | Tree origin |
description | String | Brief tree description |
bloom | String | Flowering date of the tree |
maturation_fruit | String | Fruit ripening date |
life_cycle | String | Tree life cycle |
climatic_zone | String | Tree climate zone |
producing_countries | Array | Countries that produce fruit trees. |
You can access the trees list with the following query
Query example:
query allFruits {
fruits {
id
scientific_name
fruit_name
description
producing_countries {
country
}
}
}
Response:
{
"data": {
"fruits": [
{
"id": "1",
"scientific_name": "Malus domestica",
"fruit_name": "Apple",
"description": "The apple is the fruit of the apple tree, a tree of the rosaceae family. It is a pome fruit with a round shape and a more or less sweet flavor, depending on the variety. The apple is a deciduous tree, generally 2 to 4.5 m (6 to 15 ft) tall in cultivation and up to 9 m (30 ft) in the wild..",
"producing_countries": [
{
"country": "China",
},
{
"country": "United States",
},
// ...
]
},
{
"id": "2",
"scientific_name": "Pyrus Communis",
"fruit_name": "Pear",
"description": "The pear is the fruit of the pear tree, a tree of the rosaceae family. The fruit is an edible knob of brownish green. It is a species of deciduous tree, generally 2 to 20 m high.",
"producing_countries": [
{
"country": "China",
},
{
"country": "Italy",
},
// ...
]
},
// ...
]
}
}
You can get a single tree by adding the id
as parameter: (id: 5)
.
Query example:
query oneFruit {
fruit(id: 5) {
id
scientific_name
tree_name
fruit_name
family
}
}
Response:
{
"data": {
"fruit": {
"id": "5",
"scientific_name": "Citrus x Tangerina",
"tree_name": "Tangerine",
"fruit_name": "Tangerine",
"family": "Rutaceae"
}
}
}
You can also filter trees by family
or origin
, for example: (family: "Rosaceae")
or (origin: "Asia")
.
Query example:
query filterFruit {
filterFruitsFam(family: "Rosaceae") {
id
tree_name
fruit_name
family
}
}
Response:
{
"data": {
"filterFruitsFam": [
{
"id": "1",
"tree_name": "Apple",
"fruit_name": "Apple",
"family": "Rosaceae"
},
{
"id": "2",
"tree_name": "Pear",
"fruit_name": "Pear",
"family": "Rosaceae"
}
]
}
}
In this API you can make Mutations, although the data will not be stored persistently. You can add, update and delete.
⚠ All fields are required.
mutation addFruit {
addFruit(
id: 1
scientific_name: "Malus Domestica"
tree_name: "Apple"
fruit_name: "Apple"
family: "Rosaceae"
origin: "Asia Central"
description: "The Rosaceae apple is the fruit of the apple tree, a tree of the Rosaceae family. It is a pome-shaped fruit"
bloom: "Spring"
maturation_fruit: "Late summer or fall"
life_cycle: "60-80 years"
climatic_zone: "cold"
) {
id
scientific_name
tree_name
fruit_name
family
origin
description
bloom
maturation_fruit
life_cycle
climatic_zone
}
}
mutation updateFruit {
updateFruit(
id: 1
scientific_name: "Malus Domestica"
tree_name: "Apple"
fruit_name: "Apple"
family: "Rosaceae"
origin: "Central Asia"
description: "The Rosaceae apple is the fruit of the apple tree, a tree of the Rosaceae family. It is a pome-shaped fruit"
bloom: "Spring"
maturation_fruit: "Late summer or fall"
life_cycle: "60-80 years"
climatic_zone: "Cold"
) {
id
scientific_name
tree_name
fruit_name
family
origin
description
bloom
maturation_fruit
life_cycle
climatic_zone
}
}
mutation deleteFruit {
deleteFruit(id: 9) {
id
scientific_name
}
}
Made with ❤ by
Franco Andrés Sánchez