@dosvit/server-toolkit

3.2.2-beta.8 • Public • Published

API example

import {someSpecialArticle, someSpecialArticles} from './resolvers/customResolvers'

const filePathResolver = ({filename})=>`articles/${md5(new Date + filename) + path.extname(filename)}`;

export default function (entityBuilder: EntityBuilder) {
  entityBuilder
      .addSchema(path.resolve('./schema.graphql'))
      // transform a db type to Upload —  so you can simply import entity type from generated schema
      // and not copy-paste and edit inputs
      .convertInputToUpload('File')
      // apply a custom schema transformer
      .transform(mySchemaTransformer);

  entityBuilder.Query
      // forward with arguments
      .resolve('articles', {where: {publishedAt_lte: "2017-10-10"}, orderBy: "publishedAt_ASC"})
      // or pass arguments with callback, will receive args and ctx from request
      .resolve('articles', {where: ()=>({publishedAt_lte: moment().format()}), orderBy: "publishedAt_ASC"})
      .resolve('articles', {where: (args)=>({...args?.where, publishedAt_lte: moment().format()}), orderBy: "publishedAt_ASC"})
      // custom resolvers
      .resolve('someSpecialArticle',  someSpecialArticle)
      .resolve('someSpecialArticles',  someSpecialArticles)
      // OR short notation:
      .resolve({someSpecialArticle, someSpecialArticles})
      // One can generate translations shortcuts if follows convention
      // all types translated types and resolvers in app schema should have postfixTranslated
      // all parent types should have relation `translations` that accepts {where: {lang: 'en'}}
      .translate('articles')

  entityBuilder.Mutation
      // guard all mutations (maybe should be a default)
      // matching engine https://www.npmjs.com/package/minimatch
      .guard('*',  ["is_admin"])
      // remove guard from some mutations
      .unguard('guestVote')
      // some custom resolver
      .resolve('someMutationWithLogic', someMutationWithLogic)
      // validation using http://validatejs.org/
      // constraints may be an object, or a function returning an object that accepts (args, ctx) args
      .validate('createArticle', constraints)
      // enable uploads. can be default for Upload type props in schema with generic name resolver (md5 in root)
      .upload('createArticle', 'mainImage', filePathResolver)
      .upload('updateArticle', 'mainImage', filePathResolver)
      // add custom HOF
      .decorate('updateArticle',  myHigherOrderFunction);

  // Root types are caught by Proxy object
  entityBuilder.Article
      .resolve('author', resolveAuthorFromAnotherService);

  // The same as `entityBuilder.AnotherType`
  entityBuilder.type('AnotherType').resolve('foo', bar);
}

Readme

Keywords

none

Package Sidebar

Install

npm i @dosvit/server-toolkit

Weekly Downloads

128

Version

3.2.2-beta.8

License

BSD

Unpacked Size

519 kB

Total Files

99

Last publish

Collaborators

  • eight_buddha
  • stulnikov
  • terion.name