简体   繁体   English

GraphQL突变仅返回一个结果

[英]GraphQL mutation returns only one result

I just started to learn GraphQL and did a simple example. 我刚刚开始学习GraphQL并做了一个简单的例子。 I have this schema 我有这个架构

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

When I try to add a new Continent I can see on the database that the records are stored 当我尝试添加新的大陆时,我可以在数据库上看到记录已存储

{
    "_id": {
        "$oid": "5b60dea3ff0eba10ada3cbff"
    },
    "countryID": [
        "5b5cd39951017b08d3e1303a",
        "5b5cd3c77640c708edbcbf45"
    ],
    "name": "something",
    "details": "something",
    "__v": 0
}

But upon execution of the addContinent mutation in GrapiQL 但是在GrapiQL中执行addContinent突变后

mutation{
addContinent(name:"something",details:"something",countryID:["5b5cd39951017b08d3e1303a","5b5cd3c77640c708edbcbf45"]){
  name
  country_to_show{
    name
  }
}
}

I cannot see the nested the nested results in country_to_show I only get one country name in the result query instead of two I saved and cannot understand why? 我无法在country_to_show看到嵌套的嵌套结果。我在结果查询中只得到一个国家/地区名称,而不是我保存的两个国家/地区名称,因此无法理解为什么?

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

I believe it has to do with the fact that country_to_show in the ContinentType searches for a single country in the database but I cannot pinpoint for a solution even when I try to return a list of countries in the result query upon running the mutation I get null instead. 我认为这与以下事实有关:ContinentType中的country_to_show在数据库中搜索单个国家,但即使在运行变异后尝试在结果查询中返回国家列表的情况下,我也无法找到解决方案,我会得到null代替。

You are close. 你近了 Here is some modified code that should work: 这是一些应该起作用的修改后的代码:

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

This will allow a many-to-many relationship between continents and countries. 这将使各大洲与国家之间建立多对多关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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