GithubHelp home page GithubHelp logo

stradivario / gapi-starter-neo4j Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 177 KB

@Gapi Starter Neo4J Graph

TypeScript 100.00%
neo4j neo4j-database neo4j-driver neo4j-graphaware-framework neo4j-server neo4j-graph graphql-server graphql

gapi-starter-neo4j's Introduction

@Gapi Starter Neo4J Graph

gapi-cli

To start developing clone repository

git clone https://github.com/Stradivario/gapi-starter-neo4j

Install @gapi command line interface and typescript node

npm i -g @gapi/cli ts-node

Download Neo4J database https://neo4j.com/download/

Follow the steps and create your Graph using interface provided and set password to it

default username for neo4j is neo4j

  • Go to src/app/app.module.ts and find this line:
  • Here we attach neo4j driver to context of our resolvers and passing basic authentication before the actual GraphQL request is born
  • change your-pass with appropriate password
        {
            provide: ON_REQUEST_HANDLER,
            useFactory: () => async (next, context, request: Request, h: ResponseToolkit, err: Error) => {
                context.driver = (neo4j.driver(
                    'bolt://localhost:7687',
                    neo4j.auth.basic('neo4j', 'your-pass')
                ))
                return next;
            }
        }

The same context object can be accessed also when initializing @rxdi/graphql module inside framework-imports.ts

That way we will make single initializing onto neo4j driver instead of assigning it to the object on every request

import { GraphQLModule } from "@rxdi/graphql";
import { Module } from "@rxdi/core";
import { HapiModule } from "@rxdi/hapi";
import { GraphQLModule } from "@rxdi/graphql";
import { v1 as neo4j } from 'neo4j-driver';

@Module({
    imports: [
        HapiModule.forRoot({
            hapi: {
                port: 9000
            }
        }),
        GraphQLModule.forRoot({
            graphqlOptions: {
                context: {
                    driver: (neo4j.driver(
                        'bolt://localhost:7687',
                        neo4j.auth.basic('neo4j', 'your-graph-password')
                    ))
                },
                schema: null
            }
        }),
    ]
})
export class CoreModule {}

Start the application

gapi start

You are ready to write queries :)

Open browser to

http://0.0.0.0:9000/graphiql?operationName=AppType&query=query%20AppType%20%7B%0A%20%20AppType(first%3A%2010)%20%7B%0A%20%20%20%20id%0A%20%20%20%20email%0A%20%20%20%20name%0A%20%20%20%20_id%0A%20%20%7D%0A%7D%0A%0Amutation%20CreateAppType%7B%0A%20%20CreateAppType(id%3A1%2C%20email%3A%22test%22%2C%20name%3A%22pesho%22)%20%7B%0A%20%20%20%20id%0A%20%20%20%20email%0A%20%20%20%20name%0A%20%20%20%20_id%0A%20%20%7D%0A%7D%0A%0Amutation%20UpdateAppType%20%7B%0A%20%20UpdateAppType(id%3A%201%2C%20email%3A%22%22%2C%20name%3A%22%22)%20%7B%0A%20%20%20%20id%0A%20%20%20%20email%0A%20%20%20%20name%0A%20%20%20%20_id%0A%20%20%7D%0A%7D%0A%0Amutation%20DeleteAppType%20%7B%0A%20%20DeleteAppType(id%3A%202)%20%7B%0A%20%20%20%20id%0A%20%20%20%20email%0A%20%20%20%20name%0A%20%20%20%20_id%0A%20%20%7D%0A%7D

query AppType {
  AppType(first: 10) {
    id
    email
    name
    _id
  }
}

mutation CreateAppType{
  CreateAppType(id:1, email:"test", name:"pesho") {
    id
    email
    name
    _id
  }
}

mutation UpdateAppType {
  UpdateAppType(id: 1, email:"", name:"") {
    id
    email
    name
    _id
  }
}

mutation DeleteAppType {
  DeleteAppType(id: 2) {
    id
    email
    name
    _id
  }
}

Explanation

Neo4J Driver load
import { Module, ON_REQUEST_HANDLER, SCHEMA_OVERRIDE, printSchema, GraphQLSchema  } from "@gapi/core";
import { AppQueriesController } from "./app.controller";
import { Request, ResponseToolkit } from 'hapi';
import * as neo4jgql from 'neo4j-graphql-js';
import { v1 as neo4j } from 'neo4j-driver';

@Module({
    controllers: [AppQueriesController],
    providers: [
        {
            provide: SCHEMA_OVERRIDE,
            useFactory: () => (schema: GraphQLSchema) => {
                // Do things with bootstrapped schema
                // For example now we augment schema so we can have neo4j-graphql CRUD operations based on Type
                return neo4jgql.makeAugmentedSchema({ typeDefs: printSchema(schema) });
            }
        },
        {
            provide: ON_REQUEST_HANDLER,
            useFactory: () => async (next, context, request: Request, h: ResponseToolkit, err: Error) => {
                // Authenticate user here if it is not authenticated return Boom.unauthorized()
                // if (request.headers.authorization) {
                //     const tokenData = ValidateToken(request.headers.authorization);
                //     const user = {};
                //     if (!user) {
                //         return Boom.unauthorized();
                //     } else {
                //         context.user = {id: 1, name: 'pesho'};
                //     }
                // }
                // context.user - modifying here context will be passed to the resolver
                // Passing Neo4J driver for every resolver context
                context.driver = (neo4j.driver(
                    'bolt://localhost:7687',
                    neo4j.auth.basic('neo4j', 'your-pass')
                ))
                return next();
            }
        }
    ]
})
export class AppModule { }

gapi-starter-neo4j's People

Contributors

stradivario avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

maierthomas

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.