Coder Social home page Coder Social logo

holochaingqlcontainer's Introduction

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

holochaingqlcontainer's People

Contributors

rzylber avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  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.