Coder Social home page Coder Social logo

book_list_graphql_astra's Introduction

book_list_graphql_astra

This is a simple example project with a GraphQL server connecting to a Datatax Astra databas, with a React UI.

The UI lists the content of a book database and allows to add new books to the database. It pulls details about these books with graphQL.

Here is a screenshot of the UI:

Screenshot 2020-09-25 at 16 19 06

The example is very much based on this FreeCodeCamp tutorial, with some adaptation to use Astra as the database, and some modifications for a newer version of the Apollo client.

https://www.youtube.com/watch?v=ed8SzALpx1Q

(Thanks very much for this excellent tutorial.)

Preparation:

Create an Astra instance here with a keyspace books. In Astra's cql console, create two tables and one custom index:

CREATE TABLE books.books (
    title text PRIMARY KEY,
    author text,
    genre text,
    language text
);

CREATE TABLE books.authors (
    name text PRIMARY KEY,
    gender text,
    language text
);

CREATE CUSTOM INDEX books_by_author ON books.books (author) USING 'StorageAttachedIndex' WITH OPTIONS = {'case_sensitive': 'false'};

Download the secure connect bundle from the Astra connection dialog.

Modify your connection in the /server/connect-database.js file with your secure bundle path and credentials

const client = new Client({
    cloud: { secureConnectBundle: '/Users/bettinaswynnerton/Downloads/creds.zip' },
    credentials: { username: 'KVUser', password: 'KVPassword' }
  });

Start the server with: node app.js

To test the graphQL API, navigate to http://localhost:4000/graphql

You can test the API with the embedded graphiql UI.

To insert some books via graphQL use this mutation query:

    mutation {
        addBook(
          title: "I, Robot", 
          author: "Isaac Asimov", 
          genre: "Science Fiction", 
          language: "English") {
            title
        }
    }

To insert an author, use this mutation query:

    mutation {
        addAuthor(
          name: "Isaac Asimov", 
          gender: "M", 
          language: "Russian") {
            name
        }
    }

The datamodel uses the book titles and the author names as primary keys.

You can also add some data via the Astra cql console, like here:

insert into books.books (title, author, genre, language) values ('A Scanner Darkly', 'Philip K Dick', 'Science Fiction', 'English');
insert into books.books (title, author, genre, language) values ('Ubik', 'Philip K Dick', 'Science Fiction', 'English');
insert into books.books (title, author, genre, language) values ('The Man in the High Castle', 'Philip K Dick', 'Science Fiction', 'English');

insert into books.authors (name, gender, language) values ('Philip K Dick', 'M', 'English') ;

This gives us enough data to play with graphql queries:

The following query looks up one book and then gives us also all the other books by the author:

query {
  book (title: "Ubik")
  {
    title 
    author
    authorName {
	  name
      books {
        title
      }
    }
  }
}

Expected outcome with the data as before:

{
  "data": {
    "book": {
      "title": "Ubik",
      "author": "Philip K Dick",
      "authorName": {
        "name": "Philip K Dick",
        "books": [
          {
            "title": "A Scanner Darkly"
          },
          {
            "title": "Ubik"
          },
          {
            "title": "The Man in the High Castle"
          }
        ]
      }
    }
  }
}

You can inspect the graphQL API with the asynchronous calls to Astra in schema/schema.js

The client code is built with React and Apollog GraphQL client.

First install your modules: npm install

Then to run: npm start

This will launch the frontend on http://localhost:3000

The single page will list all books that are in the database. Selecting one book will display the details in the panel on the right.

You can add another book via the form.

TO DO:

Add another form to add an author, currently not implemented in UI.

Reset the form after submitting form data.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.