• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

AEB-labs/cruddl: Create a GraphQL API for your database, using the GraphQL SDL t ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

AEB-labs/cruddl

开源软件地址(OpenSource Url):

https://github.com/AEB-labs/cruddl

开源编程语言(OpenSource Language):

TypeScript 93.5%

开源软件介绍(OpenSource Introduction):

cruddl

npm version Build Status Package Quality

cruddl - create a cuddly GraphQL API for your database, using the GraphQL SDL to model your schema.

This TypeScript library creates an executable GraphQL schema from a model definition and provides queries and mutations to access a database. Currently, it supports the multi-model database ArangoDB. The concept being inspired by existing projects like prisma and join-monster, cruddl exploits the expressiveness of the Arango Query Language (AQL) to generate one tailored query for each GraphQL request.

Try it online

Features

  • Schema definition using GraphQL types, fields and directives
  • Modelling features like relations, embedded lists and objects
  • Query features include filtering, sorting, cursor-based pagination and arbitrary nesting
  • Schema validation
  • Role-based authorization (field and type-based; static and data-dependent)
  • Pluggable database backends (currently supports ArangoDB and an in-memory implementation)

Usage

npm install --save cruddl

Install ArangoDB and create a new database.

import { ArangoDBAdapter } from 'cruddl';
const db = new ArangoDBAdapter({
    databaseName: 'databaseName',
    url: 'http://root:@localhost:8529',
    user: 'root',
    password: ''
});

If you just want to explore the features, you can also use an in-memory database implementation - but don't use this for anything else.

import { InMemoryAdapter } from 'cruddl';
const db = new InMemoryAdapter();

Define your data model and create a project:

import { Project } from 'cruddl';
const project = new Project({
    sources: [
        {
            name: 'schema.graphqls',
            body: `
            type Movie @rootEntity {
              title: String
              actors: Actor @relation
            }
            
            type Actor @rootEntity {
              name: String
              movies: Movie @relation(inverseOf: "actors")
            }`
        },
        {
            name: 'permission-profiles.json',
            body: JSON.stringify({
                permissionProfiles: {
                    default: {
                        permissions: [
                            {
                                roles: ['users'],
                                access: 'readWrite'
                            }
                        ]
                    }
                }
            })
        }
    ],
    getExecutionOptions: ({ context }) => ({ authRoles: ['users'] }),
    getOperationIdentifier: ({ context }) => context as object // each operation is executed with an unique context object
});

Then, create the GraphQL schema and serve it:

import { ApolloServer } from 'apollo-server';
const schema = project.createSchema(db);
db.updateSchema(project.getModel()); // create missing collections
const server = new ApolloServer({
    schema,
    context: ({ req }) => req // pass request as context so we have a unique context object for each operation
});
server.listen(4000, () => console.log('Server is running on http://localhost:4000/'));

See the modelling guide and the api documentation for details.

Usage in a browser environment

The core of cruddl perfectly works in a browser (e.g., using webpack), and this can be useful to generate a mock GraphQL schema on the fly or to validate a cruddl project. However, the ArangoDB adapter only works with node imports like path. Unless you configure webpack to provide mock modules for them, you will get an error when you import cruddl in a webpack environment. To solve this, you can import the core symbols from cruddl/core and the InMemoryAdapter from cruddl/inmemory.

Running Tests

For consistency, tests shall be run against a single arangodb node:

  1. npm i
  2. npm run start_arangodb
  3. ensure you have access to console at http://localhost:8529
  4. npm run test

When done, stop the instance with npm run stop_arangodb

Documentation




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap