Generate MongoDB Resolvers for GraphQL Centaurish Way

栏目: IT技术 · 发布时间: 5年前

内容简介:Translate GraphQL to Anything and make it your one and only source of truth. It is named centaur because you need to generate resolvers with this CLI and then add some edits to make them custom.CLI tool to generate MongoDB

Generate MongoDB Resolvers for GraphQL Centaurish Way

Once you head down the centaur's path forever will it dominate your destiny.

GraphQL Centaur

Translate GraphQL to Anything and make it your one and only source of truth. It is named centaur because you need to generate resolvers with this CLI and then add some edits to make them custom.

CLI tool to generate MongoDB Stucco Database Resolvers in TypeScript from GraphQL Schema. Compatible with GraphQL Editor projects( Free and Paid Tiers). So the main goal is to provide interactive experience creating GraphQL as a service.

Right now in its early beginnings.

Installation

Global

npm i -g graphql-centaur

Inside Backend Repo

npm i graphql-centaur

then use with npx for example or as a package.json scrip.

Usage

Centaur is an interactive tool to create GraphQL Resolvers connected to MongoDB compatible with stucco hybrid Go and TypeScript backend( the core is a binary and you write in TS). To start using centaur navigate to your backend repository and run command

$ centaur

First it will ask you to configure your project and the source of schema. Next you can create resolvers for your GraphQL Schema.

How it works

Resolver generation

First time when you generate a resolver centaur will also generate needed libraries for collections , DB , Utils and graphql-zeus definitions

Given the following schema:

type Person{
    firstName: String!
}
type Query{
    people: [Person]!
}
schema{
    query: Query
}

After chosing:

Query
people
CRUD
listFilter

It should generate TypeScript resolver placed in $src/Query/people.ts

import { FieldResolveInput, FieldResolveOutput } from "stucco-js";
import { PersonCollection } from "../db/collections";
import { DB } from "../db/mongo";
import { Utils } from "../Utils";
import { Person, ResolverType, ValueTypes } from "../graphql-zeus";

export const handler = async (): Promise<FieldResolveOutput> => {
    const db = await DB();
    const col = await db.collection(PersonCollection);
    return Utils.CursorToGraphQLArray<Person>(
        await col.find({}),
    );
};

and append correct entries to stucco.json file.

{
    "resolvers":{
        "Query.people":{
            "resolve":{
                "name":"lib/Query/people"
            }
        }
    }
}

and after running stucco your resolver should work out of the box.

Some resolver types however need little code to make them work the way you want.

Available Resolvers

Resolvers are split into following categories

CRUD

Create

Create an object in your database and return it

Update

Update an object in your database and return it

List

List all objects of selected type

Get by parameter

Get object by parameter from the database

Remove

Remove object from the database and return true

Common

Pipe

Pipe the arguments of the query as source for the next resolver

Resolver

Simple Resolver you need to write

Rest

Rest proxy resolvers for pointing to existing REST APIs

Source

Resolver that receives source from the parent resolver

SourcedCRUD

The same as CRUD, but also use source


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

马尔可夫链:模型、算法与应用

马尔可夫链:模型、算法与应用

Wai-Ki Ching、Ximin Huang / 陈曦 / 清华大学出版社 / 2015-6 / 39

《马尔可夫链:模型、算法与应用 应用数学译丛》讲述了马尔可夫链模型在排队系统、网页重要性排名、制造系统、再制造系统、库存系统以及金融风险管理等方面的最新应用进展.全书共安排8章内容,第1章介绍马尔可夫链、隐马尔可夫模型和马尔可夫决策过程的基本理论和方法,其余7章分别介绍马尔可夫链模型在不同领域中的应用. 《马尔可夫链:模型、算法与应用 应用数学译丛》可作为自动化、工业工程、统计学、应用数学以及管理......一起来看看 《马尔可夫链:模型、算法与应用》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码