grandstack 是一个方便graphql 应用开发的工具html
使用docker-compose 运行react
官方的starter 比较好,已是使用docker-compose 建立好了全部的依赖,可是目前使用的版本镜像有bug,我修改了版本,能够运行
参考github 项目 https://github.com/rongfengliang/grand-stack-startergit
git clone https://github.com/grand-stack/grand-stack-starter.git
version: '3' services: neo4j: build: ./neo4j ports: - 7474:7474 - 7687:7687 environment: - NEO4J_dbms_security_procedures_unrestricted=apoc.* - NEO4J_apoc_import_file_enabled=true - NEO4J_apoc_export_file_enabled=true - NEO4J_dbms_shell_enabled=true api: build: ./api ports: - 4000:4000 links: - neo4j depends_on: - neo4j ui: build: ./ui ports: - 3000:3000 links: - api depends_on: - api
由于grandstack 数据存储服务、查询服务使用的核心是neo4j 因此须要启动ne4jgithub
docker 配置比较简单,主要是插件添加以及一些简单的参数配置 Dockerfile FROM neo4j:3.4.5 ENV NEO4J_AUTH=neo4j/letmein ENV APOC_VERSION 3.4.0.2 ENV APOC_URI https://github.com/neo4j-contrib/neo4j-apoc-procedures/releases/download/${APOC_VERSION}/apoc-${APOC_VERSION}-all.jar RUN wget -P /var/lib/neo4j/plugins ${APOC_URI} ENV GRAPHQL_VERSION 3.4.0.1 ENV GRAPHQL_URI https://github.com/neo4j-graphql/neo4j-graphql/releases/download/${GRAPHQL_VERSION}/neo4j-graphql-${GRAPHQL_VERSION}.jar RUN wget -P /var/lib/neo4j/plugins ${GRAPHQL_URI} EXPOSE 7474 7473 7687 CMD ["neo4j"] 启动配置 neo4j: build: ./neo4j ports: - 7474:7474 - 7687:7687 environment: - NEO4J_dbms_security_procedures_unrestricted=apoc.* - NEO4J_apoc_import_file_enabled=true - NEO4J_apoc_export_file_enabled=true - NEO4J_dbms_shell_enabled=true
定义graphql api 的schema 以及使用ne4j 进行graphql 查询的转换 简单schenma,集成了neo4j 指令 type User { id: ID! name: String friends: [User] @relation(name: "FRIENDS", direction: "BOTH") reviews: [Review] @relation(name: "WROTE", direction: "OUT") avgStars: Float @cypher(statement: "MATCH (this)-[:WROTE]->(r:Review) RETURN toFloat(avg(r.stars))") numReviews: Int @cypher(statement: "MATCH (this)-[:WROTE]->(r:Review) RETURN COUNT(r)") } 查询指令解析配置,很传统,比较简单 import { neo4jgraphql } from "neo4j-graphql-js"; import fs from 'fs'; import path from 'path'; export const typeDefs = fs.readFileSync(process.env.GRAPHQL_SCHEMA || path.join(__dirname, "schema.graphql")) .toString('utf-8'); export const resolvers = { Query: { usersBySubstring: neo4jgraphql // resovler for neo4j } }; graphql server 启动 import { typeDefs, resolvers } from "./graphql-schema"; import { ApolloServer, gql, makeExecutableSchema } from "apollo-server"; import { v1 as neo4j } from "neo4j-driver"; import { augmentSchema } from "neo4j-graphql-js"; import dotenv from "dotenv"; dotenv.config(); const schema = makeExecutableSchema({ typeDefs, resolvers }); // augmentSchema will add autogenerated mutations based on types in schema const augmentedSchema = augmentSchema(schema); const driver = neo4j.driver( process.env.NEO4J_URI || "bolt://localhost:7687", neo4j.auth.basic( process.env.NEO4J_USER || "neo4j", process.env.NEO4J_PASSWORD || "neo4j" ) ); const server = new ApolloServer({ // using augmentedSchema (executable GraphQLSchemaObject) instead of typeDefs and resolvers //typeDefs, //resolvers, context: { driver }, // remove schema and uncomment typeDefs and resolvers above to use original (unaugmented) schema schema: augmentedSchema }); server.listen(process.env.GRAPHQL_LISTEN_PORT, '0.0.0.0').then(({ url }) => { console.log(`GraphQL API ready at ${url}`); });
基于react 的,也比较简单,就是集成graphql apidocker
项目使用的react 脚手架生成的,主要是apollo-boost react-apollo的集成使用,同时项目使用了pwa 技术,仍是挺好的参考 UI/src/UserList.js 比较重要,包含了数据查询的使用,主要是apollo client 的使用
docker-compose build docker-compose up -d
graphql api 地址 http://hostip:4000
添加数据
shell
总的来讲集成neo4j 是很不错,由于graphql 作的就是graph 查询,neo4j 就是干这个的,可是从实际来讲neo4j 并非免费的(单机是,集群不是)
同时相似的一些图数据库已经直接集成了graphql 了好比dgraph 、startdog数据库
https://grandstack.io/docs/getting-started-grand-stack-starter.html
https://github.com/grand-stack/grand-stack-starter.git
https://github.com/rongfengliang/grand-stack-starterapi