Giter VIP home page Giter VIP logo

graphql-bigchaindb's Introduction

GraphQL for BigchainDB

This is an example GraphQL api running on top of the BigchainDB python driver.

The code does not talk to the backend database directly. It just retrieves whatever data it needs using the python driver and constructs the GraphQL objects from the returned json.

It also provides a Flask app that provides an in browser IDE from exploring GraphQL.

Note

The BigchainDB driver tries to connect to BigchainDB server running on localhost:9984. It you want to change this you need to edit prepopulate.py and schema.py and pass the correct parameters to the initialization of the BigchainDB python driver.

Setup

  1. Install the requirements:
$ pip install -r requirements.txt
  1. Start the Flask app:
$ python app.py
  1. Open the GraphQL in your browser by going to http://localhost:5000/graphql

  2. Make sure BigchainDB server is running

  3. (Optional) Prepopulate BigchainDB with the example transactions:

$ python prepopulate.py

This are the transactions used in the next examples.

Examples

After prepopulating BigchainDB with the transactions provided you can copy past these queries into the GraphQL IDE.

  • Query a transaction:
query {
    transaction(id:"3b3fd7128580280052595b9bcda98895a851793cba77402ca4de0963be958c9e") {
        id
        operation
        asset
        # we don't care about the inputs
        # inputs

        # from the outputs we don't care about the condition so we only want
        # the amount and public keys
        outputs {
            amount
            publicKeys
        }

        # we don't care about the metadata
        # metadata
    }
}
  • Query multiple transactions by asset id:
query {
    transactions(assetId:"3b3fd7128580280052595b9bcda98895a851793cba77402ca4de0963be958c9e") {
        # For each transaction returned I only want the id, operation and
        # public keys in the outputs
        id
        operation
        outputs {
            publicKeys
        }
    }
}
  • Query only transfer transactions with asset id:
query {
    transactions(assetId:"3b3fd7128580280052595b9bcda98895a851793cba77402ca4de0963be958c9e", operation:"TRANSFER") {
        # I only want the public keys and amounts of all the outputs that this
        # transfer transaction fulfills
        inputs {
            fulfills {
                outputIndex
                # the `transaction_id` inside fulfills is resolved to the
                # actual transaction so we can query fields on the transaction
                # pointed to in this inputs
                transaction {
                    outputs {
                        amount
                        publicKeys
                    }
                }
            }
        }
    }
}
  • Query the outputs endpoint by public key
query {
    outputs(publicKey:"FxEfUt9ArymGeCB99dZtfCUcsKwC29c8AHZ9EPnVWcyL") {
        outputIndex
        # once again the transaction_id is resolved to the actual transaction
        transaction {
           id
           operation
           asset
        }
    }
}
  • Query without the flask backend:

We don't actually need a flask backend to perform the graphql queries. We can just execute the queries directly from python code

from schema import schema

result = schema.execute('''
{
    outputs(publicKey:"FxEfUt9ArymGeCB99dZtfCUcsKwC29c8AHZ9EPnVWcyL") {
        outputIndex
        # once again the transaction_id is resolved to the actual transaction
        transaction {
           id
           operation
           asset
        }
    }
}
''')
result.data

This means that we can implement this directly on top of the javascript driver and run entirely in the browser.

graphql-bigchaindb's People

Contributors

r-marques avatar ttmc avatar

Watchers

 avatar

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.