Javascript GraphQL突变只返回一个结果

Javascript GraphQL突变只返回一个结果,javascript,node.js,graphql,express-graphql,Javascript,Node.js,Graphql,Express Graphql,我刚开始学习GraphQL,并做了一个简单的示例。 我有这个模式 const { GraphQLObjectType, GraphQLString, GraphQLSchema, GraphQLID, // GraphQLInt, GraphQLList, GraphQLNonNull } = graphql; const ContinentType = new GraphQLObjectType({ name: 'Contine

我刚开始学习GraphQL,并做了一个简单的示例。 我有这个模式

const {
    GraphQLObjectType,
    GraphQLString,
    GraphQLSchema,
    GraphQLID,
    // GraphQLInt,
    GraphQLList,
    GraphQLNonNull
} = graphql;

const ContinentType = new GraphQLObjectType({
    name: 'Continent',
    fields: () => ({
        id: {
            type: GraphQLID
        },
        details: {
            type: GraphQLString
        },
        name: {
            type: GraphQLString
        },
        country_to_show: {
            type: CountryType,
            resolve(parent, args) {
                console.log(parent);
                console.log(parent.countryID);
                return Country.findById(parent.countryID);
            }
        }
    })
});

const CountryType = new GraphQLObjectType({
    name: 'Country',
    fields: () => ({
        id: {
            type: GraphQLID
        },
        name: {
            type: GraphQLString
        },
        flag: {
            type: GraphQLString
        },
        countryCode: {
            type: GraphQLString
        },
        details: {
            type: GraphQLString
        }
    })
});

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {

        continent: {
            type: ContinentType,
            args: {
                id: {
                    type: GraphQLID
                }
            },
            resolve(parent, args) {
                return Continent.findById(args.id);
            }
        },

        country: {
            type: CountryType,
            args: {
                id: {
                    type: GraphQLID
                }
            },
            resolve(parent, args) {
                return Country.findById(args.id);
            }
        },

        countrys: {
            type: new GraphQLList(CountryType),
            resolve(parent, args) {
                return Country.find({});
            }
        },

        contintens: {
            type: new GraphQLList(ContinentType),
            resolve(parent, args) {
                return Continent.find({});
            }
        },

    }

});

const Mutation = new GraphQLObjectType({
    name: 'Mutation',
    fields: {

        addContinent: {
            type: ContinentType,
            args: {
                name: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                details: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                countryID: {
                    type: new GraphQLNonNull(GraphQLList(GraphQLString))
                }
            },
            resolve(parent, args) {
                let continent = new Continent({
                    name: args.name,
                    details: args.details,
                    countryID: args.countryID
                });
                return continent.save();
            }
        },
        addCountry: {
            type: CountryType,
            args: {
                name: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                details: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                flag: {
                    type: new GraphQLNonNull(GraphQLString)
                },
                countryCode: {
                    type: new GraphQLNonNull(GraphQLString)
                },

            },
            resolve(parent, args) {
                let country = new Country({
                    name: args.name,
                    details: args.details,
                    countryCode: args.countryCode,
                    flag: args.flag
                });
                return country.save();
            }
        }
    }
});

module.exports = new GraphQLSchema({
    query: RootQuery,
    mutation: Mutation
});
当我尝试添加一个新大陆时,我可以在数据库上看到存储的记录

{
    "_id": {
        "$oid": "5b60dea3ff0eba10ada3cbff"
    },
    "countryID": [
        "5b5cd39951017b08d3e1303a",
        "5b5cd3c77640c708edbcbf45"
    ],
    "name": "something",
    "details": "something",
    "__v": 0
}
但是在GrapiQL中执行了基因突变后

mutation{
addContinent(name:"something",details:"something",countryID:["5b5cd39951017b08d3e1303a","5b5cd3c77640c708edbcbf45"]){
  name
  country_to_show{
    name
  }
}
}
我看不到嵌套结果在
country\u to\u show
中的嵌套结果,我在结果查询中只得到一个国家名称,而不是我保存的两个国家名称,我无法理解为什么

{
  "data": {
    "addContinent": {
      "name": "something",
      "country_to_show": {
        "name": "albania"
      }
    }
  }
}

我相信这与大陆类型中的
country\u to\u show
在数据库中搜索单个国家有关,但我无法找到解决方案,即使在运行变异时尝试在结果查询中返回国家列表,我得到的结果是
null

你很接近了。下面是一些应该可以工作的修改代码:

const continentSchema = new Schema({
  name: String,
  details: String
});
const Continent = model('Continent', continentSchema);

const countrySchema = new Schema({
  name: String,
  details: String,
  flag: String,
  continents: [{
    type: Schema.Types.ObjectId,
    ref: 'Continent'
  }]
});
const Country = model('Country', countrySchema);

const ContinentType = new GraphQLObjectType({
  name: 'Continent',
  fields: () => ({
    id: {
      type: GraphQLID
    },
    details: {
      type: GraphQLString
    },
    name: {
      type: GraphQLString
    },
    countries: {
      type: new GraphQLList(CountryType),
      resolve: (parent, args) => {
        return Country.find({ continents: parent.id });
      }
    }
  })
});

const CountryType = new GraphQLObjectType({
  name: 'Country',
  fields: () => ({
    id: {
      type: GraphQLID
    },
    name: {
      type: GraphQLString
    },
    flag: {
      type: GraphQLString
    },
    details: {
      type: GraphQLString
    },
    continents: {
      type: new GraphQLList(ContinentType),
      resolve: (parent, args) => {
        return Continent.find({
          '_id': { $in: parent.continents }
        });
      }
    }
  })
});

const RootQuery = new GraphQLObjectType({
  name: 'RootQueryType',
  fields: {

    continent: {
      type: ContinentType,
      args: {
        id: {
          type: new GraphQLNonNull(GraphQLID)
        }
      },
      resolve: (parent, args) => {
        return Continent.findById(args.id);
      }
    },

    country: {
      type: CountryType,
      args: {
        id: {
          type: new GraphQLNonNull(GraphQLID)
        }
      },
      resolve: (parent, args) => {
        return Country.findById(args.id);
      }
    },

    countries: {
      type: new GraphQLList(CountryType),
      resolve: (parent, args) => {
        return Country.find({});
      }
    },

    continents: {
      type: new GraphQLList(ContinentType),
      resolve: (parent, args) => {
        return Continent.find({});
      }
    },

  }

});

const Mutation = new GraphQLObjectType({
  name: 'Mutation',
  fields: {

    addContinent: {
      type: ContinentType,
      args: {
        name: {
          type: new GraphQLNonNull(GraphQLString)
        },
        details: {
          type: new GraphQLNonNull(GraphQLString)
        }
      },
      resolve: (parent, args) => {
        let continent = new Continent({
          name: args.name,
          details: args.details
        });
        return continent.save();
      }
    },

    addCountry: {
      type: CountryType,
      args: {
        name: {
          type: new GraphQLNonNull(GraphQLString)
        },
        details: {
          type: new GraphQLNonNull(GraphQLString)
        },
        flag: {
          type: new GraphQLNonNull(GraphQLString)
        },
        continents: {
          type: new GraphQLNonNull(new GraphQLList(GraphQLID))
        },
      },
      resolve: (parent, args) => {
        let country = new Country({
          name: args.name,
          details: args.details,
          continents: args.continents,
          flag: args.flag
        });
        return country.save();
      }
    }
  }
});

这将允许大陆和国家之间建立多对多关系。

首先感谢您的回复!正如我在这里看到的,你从国家的角度建立了一对多的关系,一个国家拥有一个大陆的ID。但我真正的目标是一个大陆到几个国家,因为我想在这之后很快建立一种多对多的关系,一个大陆可以有不同的国家,一个国家可以有不同的大陆,但到目前为止,我还不能这样做://AndiDomi啊,明白了。我更新了代码以允许大陆和国家之间的多对多关系。我将关系数据保存在Country中,以便您可以首先添加大陆,然后添加具有其所在大陆ID的国家。看看这是否能满足你的需要