Вложенные объекты в схеме GraphQL в NodeJS - PullRequest
0 голосов
/ 24 ноября 2018

Я создаю сервер GraphQL с использованием Node JS.

Я пытаюсь скопировать схему Монго, которая имеет вложенный объект исключительно для организации.Это моя схема Монго:

  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 },
});

Я пытаюсь повторить это в схеме GraphQL, пока у меня есть следующее:

const PlanType = new GraphQLObjectType({
  name: "Plan",
  fields: () => ({
    id: { type: GraphQLString },
    planName: { type: GraphQLString },
    pricing: 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 }
      })
    })
  })
});

Но я получаю следующееошибка в GraphiQL

   {
  "errors": [
    {
      "message": "The type of Plan.pricing must be Output Type but got: undefined."
    }
  ]
}

1 Ответ

0 голосов
/ 25 ноября 2018

Каждое поле в GraphQLFieldConfigMapThunk или GraphQLFieldConfigMap, которое вы задаете в качестве fields, должно быть GraphQLFieldConfig объектом, включающим такие свойства, как type, args, resolve и т. Д. Вы не можете установитьот field до GraphQLObjectType, как вы делаете с полем pricing.Другими словами, ваш код должен выглядеть примерно так:

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 },
  }),
})
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...