Skip to content

Latest commit

 

History

History
332 lines (288 loc) · 8.5 KB

readme.md

File metadata and controls

332 lines (288 loc) · 8.5 KB

Fruits API

Open in Visual Studio Code Netlify Status License License GitHub Pull Requests GitHub contributors Open Source Love made-with-javascript

Other Language


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 :)

Contribute

Read the CONTRIBUTING.md file or check if there are any issues, all PRs are welcome.

URLs

Playground: https://fruits-api.netlify.app/graphql

Web: https://fruit-api.netlify.app/

Get the code for this project

  1. Clone repository:
git clone https://github.com/Franqsanz/fruits-api.git
  1. Enter to the repository directory:
cd fruits-api
  1. Install all dependencies:
npm install

# Or with yarn
yarn
  1. Once all the dependencies are installed run the following command:
npm start

yarn start
  1. Run Testing (optional):
npm test

yarn test

As an alternative, you can use Docker

  1. Open your terminal/command line
  2. 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:

playground

Obviously the playground will be empty, you should make the queries.

Intruduction

This documentation will help you become familiar with the Fruits API resources and show you how to make different queries.

API Schema

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.

Type Query

Get all 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",
          },
          // ...
        ]
      },

      // ...
    ]
  }
}

Get a single tree

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"
    }
  }
}

Tree filtering

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"
      }
    ]
  }
}

Type Mutation

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.

How to add a tree

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
 }
}

How to upgrade a tree

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
 }
}

How to delete a tree

mutation deleteFruit {
 deleteFruit(id: 9) {
   id
   scientific_name
 }
}

Made with ❤ by Franco Andrés Sánchez