holochainGQLContainer icon indicating copy to clipboard operation
holochainGQLContainer copied to clipboard

Holochain Node.js Container with Apollo GraphQL - learning purpose

Overview

Point to any hApp (bundle) you've created and build schemas (typeDefs and resolvers) over their zome functions.

Note: This project is a simple application for learning purposes. This application implements a graphql layer over a node.js holochain container.

Installation & Usage

If you want to jump to the examples, see the sample application below

  1. First, install all the dependencies:
  npm i -S git+https://[email protected]/rzylber/holochainGQLContainer.git apollo-server-express graphql
  1. Create a setup.json file pointing to the hApps you want to instantiate:
{
    "port": 4141,
    "happs": {
        "movies": "./hApps/movies/dist/bundle.json",
        "hApp2": "./hApps/hApp2/dist/bundle.json"
    }
}
  1. Create a schema.js file with graphQL typeDefs and resolvers. See an example:
const { gql } = require('apollo-server-express');

module.exports = ( hApps ) => {
  
  // Type definitions
  const typeDefs = gql`
    type Query {
      getPeople: [Person]
      getPerson( address: String! ): Person
    }
    type Mutation {
      addPerson( name: String!, gender: String!, place_birth: String! ): String
    }
    type Person {
      address: String,
      name: String
      gender: String
      place_birth: String
    }
  `;

  // Resolvers
  const resolvers = {
    Query: {
      getPeople: () => {
        return hApps['movies'].call("graph", "main", "get_people", {})
      },
      getPerson: (_, { address }) => {
        return hApps['movies'].call("graph", "main", "get_person", { person_address: address })
      },      
    },
    Mutation: {
      addPerson: (_, { name, gender, place_birth }) => {
        return hApps['movies'].call("graph", "main", "create_person", { name, gender, place_birth }).address 
      }
    }
  };

  return { typeDefs, resolvers }
}
  1. Create your index.js following the example below:
const holoGQLContainer = require('hologqlcontainer')

const schema = require('./schema');
const setup = require('./setup.json');

(async ()=>{
    const { server, hApps } = await holoGQLContainer( schema, setup )

    console.log(`🚀 Server ready at http://localhost:${setup.port}${server.graphqlPath}`)
})()
  1. Run your application: node index

  2. Open the browser at this url: http://localhost:4141/graphql

See some GraphQL query samples below

Sample application

  1. Clone this repo and access the project folder:
    git clone https://github.com/rzylber/holochainGQLContainer.git && cd holochainGQLContainer
  1. Install the dependencies
npm install
  1. Build and pack the test hApps
npm run hc-package
  1. Run the application
npm start
  1. Open the browser at this url: http://localhost:4141/graphql

GraphQL Query Samples

In the graphql playground, try out some queries and mutations (some sandbox data was inserted for testing purpuse - examples below)

query getMovies {
  getMovies {
    name
    year
    language
    actors {
      name
      gender
    }
    director {
      name
      place_birth
    }
  }
}