holochainGQLContainer
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
-
Note 1: You'll need to install Node JS (10 or above)
-
Note 2: You'll need to Install the Holochain command line dev tool
- First, install all the dependencies:
npm i -S git+https://[email protected]/rzylber/holochainGQLContainer.git apollo-server-express graphql
- 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"
}
}
- 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 }
}
- 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}`)
})()
-
Run your application:
node index
-
Open the browser at this url: http://localhost:4141/graphql
See some GraphQL query samples below
Sample application
-
Note 1: You'll need to install Node JS (10 or above)
-
Note 2: You'll need to Install the Holochain command line dev tool
- Clone this repo and access the project folder:
git clone https://github.com/rzylber/holochainGQLContainer.git && cd holochainGQLContainer
- Install the dependencies
npm install
- Build and pack the test hApps
npm run hc-package
- Run the application
npm start
- 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
}
}
}