如何使用 GraphQL 和 Mongodb 编写 lambda 函数?
How to write lambda function using GraphQL and Mongodb?
我是 GraphQL 新手,我阅读了有关 GraphQL 的文章,它与我的预期相似,但我不明白如何在 aws lambda 函数中使用它。我有两个 collections 1) user_posts 2)user_profile。找到以下 collection 数据供您参考。
1) user_posts collection
_id :ObjectId("5d519f861c9d4400005ebd1b")
userid : ObjectId("5d518caed55bc00001d235c1")
media : "hello.jpg"
type : "jpg"
created : " "
modified : " "
like : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "like"
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "happy"
comment : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
comment : "hello"
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
comment : "welcome"
share : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "shared"
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "shared"
2) User_profile collection
_id : ObjectId("5d518caed55bc00001d235c1")
username : "ramesh",
photo : " ",
created : " ",
modified : " "
_id : ObjectId("5d518da6d55bc00001d235c2")
username : "shekar",
photo : " ",
created : " ",
modified : " "
通常我使用 lambda 函数来获得这样的输出。但它没有得到我预期的输出。
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID;
var databasename = "trans_db";
var db;
exports.handler = (event, context, callback) => {
var Userid = event['userid'];
var uid = ObjectId(Userid);
MongoClient.connect(uri,{ useNewUrlParser: true }, (error, client) => {
if (error) return 1; // Checking the connection
console.log('Connection Successful');
db = client.db(databasename);
db.collection("user_posts").find({"userid" : uid}).toArray(function(err,
res) {
if (err) throw err;
context.succeed(res);
});
});
};
我需要像下面这样的输出。
_id :ObjectId("5d519f861c9d4400005ebd1b")
userid : ObjectId("5d518caed55bc00001d235c1")
username : "ramesh"
photo : " ",
media : "hello.jpg"
type : "jpg"
created : " "
modified : " "
like : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "like"
username : "ramesh"
photo : " "
1 : Object
username : "shekar"
photo : " "
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "happy"
username : "shekar"
photo : " "
comment : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
comment : "hello"
username : "ramesh"
photo : " "
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
comment : "welocme"
username : "shekar"
photo : " "
share : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "shared"
username : "ramesh"
photo : " "
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "shared"
username : "shekar"
photo : " "
您需要 mongo 最低版本 3.6。
根据 user_posts
return 更新您的架构以获取下面的查询。
Mongodb查询
MongoClient.connect(uri,{ useNewUrlParser: true }, (error, client) => {
if (error) return 1; // Checking the connection
console.log('Connection Successful');
db = client.db(databasename);
db.collection("user_posts").aggregate(
{ $match: {"userid" : uid}},
{ $unwind: '$like' },
{ $lookup: { from: "users", localField: "like.userid", foreignField: "_id", as: "users" }},
{ $group: {
_id: "$_id",
like: { $push: { $mergeObjects: ['$like', { $arrayElemAt: [ "$users", 0 ] } ]}},
data: { $first: "$$ROOT" }
}},
{ $replaceRoot: { newRoot: { $mergeObjects: ['$data', { like: "$like"} ]} } }
{ $unwind: '$comment' },
{ $lookup: { from: "users", localField: "comment.userid", foreignField: "_id", as: "users" }},
{ $group: {
_id: "$_id",
comment: { $push: { $mergeObjects: ['$comment', { $arrayElemAt: [ "$users", 0 ] } ]}},
data: { $first: "$$ROOT" }
}},
{ $replaceRoot: { newRoot: { $mergeObjects: ['$data', { comment: "$comment"} ]} } }
{ $unwind: '$share' },
{ $lookup: { from: "users", localField: "share.userid", foreignField: "_id", as: "users" }},
{ $group: {
_id: "$_id",
share: { $push: { $mergeObjects: ['$share', { $arrayElemAt: [ "$users", 0 ] } ]}},
data: { $first: "$$ROOT" }
}},
{ $replaceRoot: { newRoot: { $mergeObjects: ['$data', { share: "$share"} ]} } },
{ $project: { users: 0 }}
).then(res => {
context.succeed(res);
}).catch(error => {
console.log({ error })
});
GraphQL
user_posts {
_id
userid
username
photo
media
type
created
modified
like {
userid
username
status
photo
}
comment {
userid
username
status
photo
}
share {
userid
username
status
photo
}
}
基于 graphql 中的查询更改 graphql 中的解析器我们可以简化 mongodb 查询,但现在我们必须专注于 Graphql
我是 GraphQL 新手,我阅读了有关 GraphQL 的文章,它与我的预期相似,但我不明白如何在 aws lambda 函数中使用它。我有两个 collections 1) user_posts 2)user_profile。找到以下 collection 数据供您参考。
1) user_posts collection
_id :ObjectId("5d519f861c9d4400005ebd1b")
userid : ObjectId("5d518caed55bc00001d235c1")
media : "hello.jpg"
type : "jpg"
created : " "
modified : " "
like : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "like"
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "happy"
comment : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
comment : "hello"
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
comment : "welcome"
share : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "shared"
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "shared"
2) User_profile collection
_id : ObjectId("5d518caed55bc00001d235c1")
username : "ramesh",
photo : " ",
created : " ",
modified : " "
_id : ObjectId("5d518da6d55bc00001d235c2")
username : "shekar",
photo : " ",
created : " ",
modified : " "
通常我使用 lambda 函数来获得这样的输出。但它没有得到我预期的输出。
var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectID;
var databasename = "trans_db";
var db;
exports.handler = (event, context, callback) => {
var Userid = event['userid'];
var uid = ObjectId(Userid);
MongoClient.connect(uri,{ useNewUrlParser: true }, (error, client) => {
if (error) return 1; // Checking the connection
console.log('Connection Successful');
db = client.db(databasename);
db.collection("user_posts").find({"userid" : uid}).toArray(function(err,
res) {
if (err) throw err;
context.succeed(res);
});
});
};
我需要像下面这样的输出。
_id :ObjectId("5d519f861c9d4400005ebd1b")
userid : ObjectId("5d518caed55bc00001d235c1")
username : "ramesh"
photo : " ",
media : "hello.jpg"
type : "jpg"
created : " "
modified : " "
like : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "like"
username : "ramesh"
photo : " "
1 : Object
username : "shekar"
photo : " "
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "happy"
username : "shekar"
photo : " "
comment : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
comment : "hello"
username : "ramesh"
photo : " "
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
comment : "welocme"
username : "shekar"
photo : " "
share : Array
0 : Object
userid : ObjectId("5d518caed55bc00001d235c1")
status : "shared"
username : "ramesh"
photo : " "
1 : Object
userid : ObjectId("5d518da6d55bc00001d235c2")
status : "shared"
username : "shekar"
photo : " "
您需要 mongo 最低版本 3.6。
根据 user_posts
return 更新您的架构以获取下面的查询。
Mongodb查询
MongoClient.connect(uri,{ useNewUrlParser: true }, (error, client) => {
if (error) return 1; // Checking the connection
console.log('Connection Successful');
db = client.db(databasename);
db.collection("user_posts").aggregate(
{ $match: {"userid" : uid}},
{ $unwind: '$like' },
{ $lookup: { from: "users", localField: "like.userid", foreignField: "_id", as: "users" }},
{ $group: {
_id: "$_id",
like: { $push: { $mergeObjects: ['$like', { $arrayElemAt: [ "$users", 0 ] } ]}},
data: { $first: "$$ROOT" }
}},
{ $replaceRoot: { newRoot: { $mergeObjects: ['$data', { like: "$like"} ]} } }
{ $unwind: '$comment' },
{ $lookup: { from: "users", localField: "comment.userid", foreignField: "_id", as: "users" }},
{ $group: {
_id: "$_id",
comment: { $push: { $mergeObjects: ['$comment', { $arrayElemAt: [ "$users", 0 ] } ]}},
data: { $first: "$$ROOT" }
}},
{ $replaceRoot: { newRoot: { $mergeObjects: ['$data', { comment: "$comment"} ]} } }
{ $unwind: '$share' },
{ $lookup: { from: "users", localField: "share.userid", foreignField: "_id", as: "users" }},
{ $group: {
_id: "$_id",
share: { $push: { $mergeObjects: ['$share', { $arrayElemAt: [ "$users", 0 ] } ]}},
data: { $first: "$$ROOT" }
}},
{ $replaceRoot: { newRoot: { $mergeObjects: ['$data', { share: "$share"} ]} } },
{ $project: { users: 0 }}
).then(res => {
context.succeed(res);
}).catch(error => {
console.log({ error })
});
GraphQL
user_posts {
_id
userid
username
photo
media
type
created
modified
like {
userid
username
status
photo
}
comment {
userid
username
status
photo
}
share {
userid
username
status
photo
}
}
基于 graphql 中的查询更改 graphql 中的解析器我们可以简化 mongodb 查询,但现在我们必须专注于 Graphql