@tcwd/jsonschema-graphql
TypeScript icon, indicating that this package has built-in type declarations

0.1.7 • Public • Published

微搭数据源 graphql 解释器

将微搭数据源 jsonschema 动态转换成 graphql 类型和 resolver

用法

import { graphql } from 'graphql';
const transformer = new SchemaTransformer([...wedaDatasources]);

const schema = transformer.getSchema(); // graphql schema

const result = graphql({
  schema,
  rootValue: {},
  source: 'mutation{ xxxx }',
  contextValue: {},
  variableValues: {},
});

除了使用默认的 resolver 也可以自定义数据获取方法,一种是定义完全的 resolver 方法,另一种是自定义数据获取的 fetcher 方法。这两者都可以自定义部分方法来代替默认的,而两种的区别在于 resolver 会处理数据获取的完整方法,而 fetcher 只是数据获取方法,其他的像出参入参处理等逻辑都可以由默认的 resolver 处理。 如:

const localData = {
  xxx,
};

const transformer = new SchemaTransformer([...wedaJSONSchemas], {
  getResolvers(opt) {
    return {
      getItem(value, { _id: _id }, context) {
        return localData[_id];
      },
    };
  },
  fetchers: {
    http: (url, init = {}, datasource) => {
      init.headers = { ...init.headers, 'x-seqId': 'xxxxx' };
      return fetch(url, init);
    },
  },
});

TODO

  1. 复杂 where 查询支持

  2. 聚合设计

Readme

Keywords

none

Package Sidebar

Install

npm i @tcwd/jsonschema-graphql

Weekly Downloads

2

Version

0.1.7

License

ISC

Unpacked Size

88.7 kB

Total Files

27

Last publish

Collaborators

  • zhangzimeng
  • ceoyp
  • wedabot
  • miusuncle
  • daniel-dx
  • vancece
  • binggg
  • fengkx
  • bigyounger
  • bobbyzhao
  • yican
  • justan
  • yhyang
  • easonruan