grandstack graphql 工具基本试用
grandstack 是一个方便graphql 应用开发的工具
使用docker-compose 运行
环境准备
官方的starter 比较好,已经是使用docker-compose 创建好了所有的依赖,但是目前使用的版本镜像有bug,我修改了版本,可以运行
参考github 项目 https://github.com/rongfengliang/grand-stack-starter
- clone starter
git clone https://github.com/grand-stack/grand-stack-starter.git
- docker-compose 文件
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
starter 说明
因为grandstack 数据存储服务、查询服务使用的核心是neo4j 所以需要启动ne4j
- neo4j server
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
- api server 配置
定义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}`);
});
- UI
基于react 的,也比较简单,就是集成graphql api
项目使用的react 脚手架生成的,主要是apollo-boost react-apollo的集成使用,同时项目使用了pwa
技术,还是挺好的参考
UI/src/UserList.js 比较重要,包含了数据查询的使用,主要是apollo client 的使用
运行&&测试
- 启动服务
docker-compose build
docker-compose up -d
- 访问测试
graphql api 地址
http://hostip:4000
添加数据
- neo4j 数据
- ui
说明
总的来说集成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-starter