首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Graphql-js模式克服代码重复

使用Graphql-js模式克服代码重复
EN

Stack Overflow用户
提问于 2018-12-02 21:21:51
回答 1查看 531关注 0票数 0

我正在用GraphQL-Js & Mongo学习GraphQL。

我发现GraphQL模式中有很多代码重复。

我的GraphQL输入对象如下所示:

代码语言:javascript
复制
const PricingInputType = new GraphQLInputObjectType({
  name: 'PricingInput',
  fields: () => ({
    expires: { type: GraphQLInt },
    private: { type: GraphQLBoolean },
    monthly: { type: GraphQLInt },
    scanEnvelope: { type: GraphQLInt },
    initalScan: { type: GraphQLInt },
    perPage: { type: GraphQLInt },
    forwardMail: { type: GraphQLInt },
    forwardParcel: { type: GraphQLInt },
    shred: { type: GraphQLInt },
    perMonthPerGram: { type: GraphQLInt },
    freeStorePerGram: { type: GraphQLInt },
    setup: { type: GraphQLInt },
    idFree: { type: GraphQLInt }
  })
});

const PlanInputType = new GraphQLInputObjectType({
  name: 'PlanInput',
  fields: () => ({
    id: { type: GraphQLString },
    planName: { type: GraphQLString },
    pricing: { type: PricingInputType }
  })
});

const Mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields: {
      addPlan: {
          type: PlanType,
          args: {
            Plan: { type: PlanInputType }
          },
          resolve(parent, args){
            //TO DO. UPSERT TO MONGO
              return true;
          }
      }
    }
});

其中我的查询对象如下所示:

代码语言:javascript
复制
const PricingType = new GraphQLObjectType({
  name: "Pricing",
  fields: () => ({
    expires: { type: GraphQLInt },
    private: { type: GraphQLBoolean },
    monthly: { type: GraphQLInt },
    scanEnvelope: { type: GraphQLInt },
    initalScan: { type: GraphQLInt },
    perPage: { type: GraphQLInt },
    forwardMail: { type: GraphQLInt },
    forwardParcel: { type: GraphQLInt },
    shred: { type: GraphQLInt },
    perMonthPerGram: { type: GraphQLInt },
    freeStorePerGram: { type: GraphQLInt },
    setup: { type: GraphQLInt },
    idFree: { type: GraphQLInt }
  })
});

const PlanType = new GraphQLObjectType({
  name: "Plan",
  fields: () => ({
    id: { type: GraphQLString },
    planName: { type: GraphQLString },
    pricing: { type: PricingType }
  })
});

const RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    plans: {
      type: new GraphQLList(PlanType),
      resolve(parent, args) {
        return Plans.find({});
      }
    }
  }
});

现在我的Mongo Schema看起来像这样:

代码语言:javascript
复制
var plansSchema = new Schema({
  planName:  {
    type: String,
    required: [true, "Plan name is required"]
  },
  pricing: {
    monthly: Number,
    scanEnvelope: Number,
    initalScan: Number,
    perPage: Number,
    forwardMail: Number,
    forwardParcel: Number,
    shred: Number,
    perMonthPerGram: Number,
    freeStorePerGram: Number,
    setup: Number,
    idFree: Number
  },
  expires: Number,
  private: Boolean,
  deleted: Boolean,
  date: { type: Date, default: Date.now },
});

module.exports =  mongoose.model('plans', plansSchema);

如你所见,我在3个地方复制代码。如果我决定按季收费而不是按月收费,我需要在3个地方更改Monthly属性!?

有没有更好的模式?或者这只是一种方法?

EN

回答 1

Stack Overflow用户

发布于 2019-01-23 02:07:26

你可以看看我的answer来回答类似的问题。它使用一个依赖于GraphQL“查询对象”的package来生成其他所有内容,因此放弃了mongoose而直接使用MongoDB。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53580643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档