1.0.0 • Published 4 years ago

apollo-micro-graphql v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

帮助文档

1.apollo-server. 2.sequelize

本地开发

1.npm run dev 模拟dev环境 2.npm run jest jest单元测试 3.npm run loadtest loadtest压力并发测试

功能使用

1.dataloader 所有schema的主键以及外键都添加dataloader

  const products = await context.req.loaders.Product.id.load(1)
  const manyProducts = await context.req.loaders.Product.id.loadMany([1,2,3])
  // more

servera

  export default function () {
  return MG.schema('Product', {
    description: 'Product schema',
    // configure to enable default add, delete, modify and query
    plugin: {
      // api hook
      addMutation: {
        hook: async function ({ type, config }, { parent, args, context, info }, next) {
          // change input field value
          return next()
        }
      },
      deleteMutation: true,
      singularQuery: true,
      pluralQuery: true,
      updateMutation: true
    },
    table: {
      // sequelize hooks
      hooks: {
        beforeCreate: async (instance, options) => {

        }
      }
    }
  })
    // field design of your database and schema
    .fields({
      name: {
        $type: String,
        required: true,
        description: 'productName'
      },
      price: {
        $type: Number,
        description: 'selling price'
      },
      effectiveDate: {
        $type: Date,
        description: 'effectiveDate'
      },
      salesVolume: {
        description: 'salesVolume',
        $type: Number
      }
    }).links({
      serialNumber: {
        args: {
          testFields: [{
            string1: { $type: String, description: 'string1-description' },
            number1: { $type: Number, description: 'number1-description' }
          }]
        },
        description: 'add a field resolver to Product schema',
        $type: Number,
        resolve: async (parent, args, context, info, sgContext) => {
          return 100
        }
      }
    }).extends({
      // the User schema from serverb 
      User: {
        recentPurchases: {
          description: 'extend a field resolver to User schema which define other server',
          args: {},
          $type: ['Product'],
          resolve: async (parent, args, context, info, sgContext) => {
            // function code and return 
          }
        }
      }
    }).queries({
      sellWellProducts: {
        description: 'Find out the products with good sales volume',
        args: { salesVolume: { $type: Number, required: true, description: 'salesVolume' } },
        $type: 'ProductConnection',
        resolve: async (args, context, info, sgContext) => {
          let { salesVolume } = args
          let { Product } = sgContext.models
          return MG.Connection.resolve(Product, {
            args,
            condition: {
              salesVolume: {
                [Sequelize.Op.gt]: salesVolume
              }
            }
          })
        }
      },
      dataSourceDemo: {
        description: 'dataSource how to use',
        $type: { code: Number },
        resolve: async (args, context, info, sgContext) => {
          let userApi = context.dataSources.userApi
          let users = await userApi.getMaleUsers({
            first: 2,
            condition: {
              gender: 'Male'
            }
          })
          console.log('users', users)
          return { code: 200 }
        }
      },
      bingHelperDemo: {
        // 兼容老代码 需要网关层自己解析
        description: 'bingHelperDemo how to use',
        $type: { code: Number },
        resolve: async (args, context, info, sgContext) => {
          let users = await BindingHelper.PluralQuery('users', {
            infoOrQuery: `{
              edges{
                node{
                  id
                  gender
                  dateOfBirth
                  age
                  phoneNumber
                  createdAt
                  updatedAt
                }
              }
            }`,
            args: {
              first: 2,
              condition: {
                gender: 'Male'
              }
            }
          })
          console.log('users', users)
          return { code: 200 }
        }
      }
    }).mutations({
      // custom mutations
      addProductsForSale: {
        description: 'add products for sale',
        inputFields: {
          products: [{ name: { $type: String, required: true, description: 'productName' } }]
        },
        outputFields: { code: Number },
        resolve: async (args, context, info, sgContext) => {
          let { name } = args.input
          // function code and return outputFields
          return { code: 200 }
        }
      }
    }).statics({
      // static methods, Product.ExampleA to use
      ExampleA: () => {

      }
    }).methods({
      exampleA: () => {
        // instance methods, productInstance.ExampleA to use
      }
    }).hasMany({
      // // One-on-many
      commoditys: {
        // Other models defined in this service 
        target: 'Commodity',
        as: 'commoditys',
        foreignKey: 'product_id'
      }
    }).hasOne({
      // One-on-one
    }).belongsTo({
      // One-on-one
    }).belongsToMany({
      // Many-on-many
    })
} 

serverb

  MG.schema('User', {
    description: 'User schema'
  })
  .fields({
    realName: {
      $type: String,
      required: true,
      description: 'realName'
    }
  })

最后通过@apollo/gateway实现网关聚集schema。

  Type User{
    # realName
    realName:String!
    # more...
    # extend a field resolver to User schema which define other server
    recentPurchases:['Product']
  }