gpt4 book ai didi

javascript - 如何为 graphQL 解析器查询 firestore()?

转载 作者:行者123 更新时间:2023-12-01 16:10:46 26 4
gpt4 key购买 nike

我正在将 GraphQL 应用程序与我现有的 Firebase 项目相结合,并且在获取查询以正确从 firestore() 获取数据时遇到很多问题。

到目前为止,我的突变工作正常,但是当我去查询数据时,我无法将 firestore().get() 快照转换为 graphQL 可以识别的形式。

到目前为止,它看起来像这样:

const {GraphQLObjectType,
GraphQLString,
GraphQLBoolean,
GraphQLFloat,
GraphQLSchema,
GraphQLID,
GraphQLList,
GraphQLNonNull} = require("graphql");
const admin = require('firebase-admin');
const functions = require('firebase-functions');


admin.initializeApp(functions.config().firebase);

//Models
const Room = admin.firestore().collection('room');
const Position = admin.firestore().collection('position');
const Plant = admin.firestore().collection('plant');
const PlantInfo = admin.firestore().collection('plantInfo');

const RoomType = new GraphQLObjectType({
name: "Room",
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
description: { type: GraphQLString },
floor: { type: GraphQLString },
building: { type: GraphQLString },
positions: {
type: new GraphQLList(PositionType),
resolve(parent, arg) {
//return _.filter(positions, {inRoomId:parent.id})
return Position.orderByChild('inRoomId').equalTo(parent.id);
}
}
})
});

const PositionType = new GraphQLObjectType({
name: "Position",
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
description: { type: GraphQLString },
exposure: { type: GraphQLString },
size: { type: GraphQLString },
inRoom: {
type: RoomType,
resolve(parent, args) {
//return _.find(rooms, {id:parent.inRoomId})
return Room.child(parent.inRoomId);
}
}
})
});

const RootQuery = new GraphQLObjectType({
name: "RootQueryType",
fields: {
room: {
type: RoomType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
//code to get data from db/othersourse
//return _.find(rooms, {id: args.id});
return Room.child(args.id);
}
},
position: {
type: PositionType,
args: { id: { type: GraphQLID } },
resolve(parent, args) {
//code to get data from db/othersourse
//return _.find(positions, {id: args.id})
return Position.child(args.id);
}
},
rooms: {
type: new GraphQLList(RoomType),
resolve(parent, args) {
//return rooms
return Room.get().then(snapshot => {snapshot.forEach(doc => {return doc})})

}
},
positions: {
type: new GraphQLList(PositionType),
resolve(parent, args) {
//return positions
return Position.get().then(doc => console.log(doc)).catch(err => console.log('Error getting document', err));
}
}
}
});

const Mutation = new GraphQLObjectType({
name: "Mutation",
fields: {
addRoom: {
type: RoomType,
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
floor: { type: new GraphQLNonNull(GraphQLString) },
building: { type: new GraphQLNonNull(GraphQLString) }
},
resolve(parent, args) {
let room = {
name: args.name,
floor: args.floor,
building: args.building
};
return Room.add(room);
}
},
addPosition: {
type: PositionType,
args: {
name: { type: new GraphQLNonNull(GraphQLString) },
exposure: { type: new GraphQLNonNull(GraphQLString) },
size: { type: new GraphQLNonNull(GraphQLString) },
inRoomId: { type: new GraphQLNonNull(GraphQLString) }
},
resolve(parent, args) {
let position = {
name: args.name,
exposure: args.exposure,
size: args.size,
inRoomId: args.inRoomId
};
return Position.add(position);
}
}
}
});

module.exports = new GraphQLSchema({
query: RootQuery,
mutation: Mutation
});

在 RootQuery -> Rooms 下,我试图获取一个 graphQL 查询以返回我的“房间”集合中的所有房间。我已经能够使用以下命令将它发送到 console.log() 文档列表:
return Room.get()
.then(snapshot => {
snapshot.forEach(doc => {
console.log(doc.id, " => ", doc.data());

但是到目前为止,将它放入数组中让我望而却步。非常感谢任何帮助。

最佳答案

看到没有人能够回答这个问题,我最终自己弄清楚了:p

因此,请解决与获取相关数据集合(例如职位)相关的功能。以下作品:

首先,您需要一个函数将快照转换为数组,因为这是 graphQL 所期望的。这也允许您分离 id 并将其与数组项一起添加:

const snapshotToArray = (snapshot) => {
var returnArr = [];

snapshot.forEach((childSnapshot)=> {
var item = childSnapshot.data();
item.id = childSnapshot.id;

returnArr.push(item);
});

return returnArr;
};

接下来,当您使用 .get() 获取数据时,它会返回一个可以传递给 snapshotToArray() 的 promise (和错误)。
return Position.get().then((snapshot) => {
return snapshotToArray(snapshot);
})

对于仅调用一个数据集的解析函数,例如 inRoom。它类似于第一个,除了使用 .where() 并在快照函数中分隔 id 和 data() :
return Room.doc(parent.inRoomId).get().then((snapshot) => {
var item = snapshot.data();
item.id = snapshot.id;
return item;
})

以防其他人遇到同样的问题:)

关于javascript - 如何为 graphQL 解析器查询 firestore()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57737574/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com