Graphql-如何修复“验证失败:值“转换为数组失败”错误?

时间:2019-02-18 10:45:22

标签: node.js mongodb mongoose graphql apollo-client

在这里,我已经创建了一些用于测试目的的示例对象类型。(例如,配方和联系方式)。

我正在尝试在食谱类型中插入联系方式。

它给我错误

  

“配方验证失败:配方联系人:以下原因转换为数组失败   值\“ [{contactFname:'Richa',\ n contactLname:'Sharma',\ n
  contactCompanyName:'XYZ'}] \“在路径\” recipeContacts \“”

我是Graphql的新手,如果您可以详细说明确切的错误,将会很有帮助。

谢谢。

**//Schema File** 

    exports.typeDefs = `

    input ContactDemoInput
      {
      id:String
      contactFname: String!
      contactLname: String!
      contactCompanyName: String
      contactNotes: String
      contactBirthday: String
      contactCreatedDate:String
      username: String
      },




    type Contact {
      id:String
      contactFname: String!
      contactLname: String!
      contactCompanyName: String
      contactNotes: String
      contactBirthday: String
      contactCreatedDate:String
      username: String
    },

    input RecipeDemoInput {
      id:String
      name: String!
      category: String!
      description: String!
      instructions: String!
      createdDate:String
      likes: Int
      recipeContacts: [ContactDemoInput]

      },


      type Recipe {
        id:String
        name: String!
        category: String!
        description: String!
        instructions: String!
        createdDate:String
        likes: Int
        recipeContacts: [Contact]

        },



**//mutation to add recipe** 

    type Mutation {
        addRecipe(name: String!, description: String!, category: String!, instructions: String!, username: String ,recipeContacts:[ContactDemoInput]): Recipe
    }

**// Graphql query file** 

    export const ADD_RECIPE = gql`
      mutation(
        $name: String!
        $category: String!
        $description: String!
        $instructions: String!
        $username: String
        $recipeContacts: [ContactDemoInput]
      ) {
        addRecipe(
          name: $name
          category: $category
          description: $description
          instructions: $instructions
          username: $username
          recipeContacts: $recipeContacts
        ) {
          name
          recipeContacts {
                  contactFname
                  contactLname
             }
        }
      }
    `;

**// passing values** 

    mutation(
        $name: String!
        $category: String!
        $description: String!
        $instructions: String!
        $username: String
        $recipeContacts: [ContactDemoInput]
      ) {
        addRecipe(
          name: $name
          category: $category
          description: $description
          instructions: $instructions
          username: $username
          recipeContacts: $recipeContacts
        ) {
          name
          category
          cont{
            contactFname
            contactLname
          }
        }
      }



    {
      "name": "Sandwitch",
      "category": "snacks",
      "description": "test",
      "instructions": "test",
      "recipeContacts":[ {
        "contactFname": "richa",
        "contactLname": "sharma",
        "contactCompanyName": "XYZ"
      }]

      }


// Recipe Model 

const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const { Contactschema } = require("./Contact");

// Recipe Model
const RecipeSchema = new Schema({
  name: {
    type: String,
    required: true
  },

  category: {
    type: String,
    required: true
  },

  description: {
    type: String,
    required: true
  },

  instructions: {
    type: String,
    required: true
  },

  createdDate: {
    type: Date,
    default: Date.now
  },

  likes: {
    type: Number,
    default: 0
  },

  recipeContacts: {
    type: [Schema.Types.ObjectId],
    ref: "Contact"
  }
});

module.exports = mongoose.model("Recipe", RecipeSchema);




0 个答案:

没有答案