Skip to content

tu-cos-343/graphql-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sample GraphQL Server

This simple server illustrates how to serve GraphQL requests. It uses:

  1. The Graphene package for Python
  2. The psycopg2 module to connect to Postgre
  3. A PostgreSQL database set up with the DVD Rental example database.
  4. A simple Flask server to process requests from the GraphQL client
  5. The GraphiQL browser-based GraphQL client

Installation

  1. Clone the repository via Git.
    % git clone https://github.com/tu-cos-343/graphql-example.git
  2. Change directory to your shiny new clone.
    % cd graphql-example
  3. Create a Python3 virtual environment.
    % python3 -m venv venv
  4. Install the required Python packages.
    % pip install -r requirements.txt
  5. Update the database connection parameters near the top of schema.py

Flask Server

Find the Flask server in app.py. It uses flask-graphql to interface with the Graphene package using a specialized view function called GraphQLView.

Run the server from the command line:

% python app.py

By default, the Flask configuration will listen on http://localhost:5000. Because app.py enables GraphiQL support, you can access the GraphiQL IDE in your browser at http://localhost:5000/graphql.

GraphQL Schema

The schema.py file implements our GraphQL schema. You will find extensive comments in that file that explain the various pieces and parts. Please take the time to read over the comments so that you understand how it works.

Sample Queries

Following are examples of queries and mutations that the schema.py file implements. Note that if you are connecting to a read-only database account, you will not be able to execute the mutations.

  1. Retrieve all the films in the database. Note that we also ask for the actors, which causes the resolve_actors method to run.

    query AllFilms {
      films {
        filmId
        title
        description
        actors {
          fullName
        }
      }
    }
    
  2. Retrieve a single actor. Here the actorId is passed as a parameter.

    query OneActor($id: Int!) {
      actor(actorId: $id) {
        firstName
        lastName
        lastUpdate
      }
    }
    

    In GraphiQL, use the Query Variables window to supply a JSON object with the ID. For example:

    { "id": 42 }
  3. Add a new category. Passes the name for the category directly. Probably better to use a parameter as shown for OneActor.

    mutation AddCategory {
      createCategory(name: "Nerd Films") {
        categoryId
        name
      }
    }
    
  4. Delete a category. Delete an existing category by its category ID. Note that the database will still enforce referential integrity if another database entity refers to the category being deleted.

    mutation DeleteCategory($id: Int!) {
      deleteCategory(categoryId: $id) {
        rowsAffected
      }
    }
    
  5. Create an actor.

    mutation NewActor($input: ActorInput!) {
      createActor(actorInput: $input) {
        actorId
        firstName
        lastName
      }
    }
    

    Pass the details in the Query Variables window

    {
      "input": {
        "firstName": "Fred",
        "lastName": "Ziffle"
      }
    }