所以我有2分贝,气候有1分贝,食谱有1分贝。 当我添加食谱时,我提供了与其他数据库等效的气候的ID。 但在答案中,我需要实际值。 我想知道最佳做法是什么?
案例1:应要求,我在另一个数据库上获取数据
情况2:我使用给定ID的实际值存储数据
情况3:另一种解决方案?
模式
type ClimateZone {
_id: ID!
name: String!
}
input ClimateZoneInput {
name: String!
}
type Mutation {
addClimateZone(input: ClimateZoneInput!): ClimateZone
addRecipe(input: RecipeInput!): Recipe
}
type Query {
allClimateZone: [ClimateZone!]!
climateZone(id: ID!): ClimateZone!
allRecipe: [Recipe!]!
recipe(id: String!): Recipe!
}
type Recipe {
_id: ID!
variety: String!
climate: ClimateZone!
}
input RecipeInput {
variety: String!
climate: ID!
}
解析器:
const Query={
allClimateZone:async ()=>await client.db('test').collection('climateZones').find().toArray(),
climateZone:async (parent:any,input:any)=>await client.db('test').collection('climateZones').findOne({"_id":new ObjectID(input.id)}),
allRecipe:async ()=>await client.db('test').collection('recipes').find().toArray(),
recipe:async (parent:any,input:any)=> {
return await client.db('test').collection('recipes').findOne({"_id":new ObjectID(input.id)})
}
}
const Mutation={
addClimateZone:async (parent:any,args:ClimateZoneInput)=> {
const data=JSON.parse(JSON.stringify(args)).input
const r = await client.db('test').collection('climateZones').insertOne(data);
return {_id:r.insertedId,...data};
},
addRecipe: async (parent:any,args:RecipeInput)=> {
const data:RecipeInput=JSON.parse(JSON.stringify(args)).input
const r= await client.db('test').collection('recipes').insertOne(data)
// here is my probleme
return {_id:r.insertedId,...data};
}
}
const resolvers={
Query,
Mutation
}