简体   繁体   中英

Nested Objects in GraphQL Schema in NodeJS

I'm creating a GraphQL Server using Node JS.

I'm trying to replicate the mongo Schema which has a nested object purely for organisation. This is my mongo schema:

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

I'm trying to replicate this in a GraphQL schema, so far I have the following:

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

But I'm getting the following errro in GraphiQL

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

Each field in the GraphQLFieldConfigMapThunk or GraphQLFieldConfigMap that you set as your fields must be a GraphQLFieldConfig object that includes properties like type , args , resolve , etc. You cannot set a field to a GraphQLObjectType like you're doing with the pricing field. In other words, your code should look more like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM