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

标签 javascript firebase google-cloud-firestore graphql

我正在将 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/

相关文章:

javascript - 为什么console.log 在作为参数传递给forEach 时不起作用?

javascript - Qt QML 如何格式化(突出显示)文本

javascript - 如何让 elevateZoom 与两个 div 一起使用

android - 如何编写 Firebase Android 仪器测试?

java - Android Studio 中的 Firestore 引用

javascript - 如何将 firebase 查询重构为特定的 thenable 函数

javascript - 如何等待 Firestore 数据库请求完成?

javascript - 单击后如何切换移动菜单?

Firebase 存储 url,新文件保持相同的访问 token

google-cloud-firestore - 如何在 Firestore 中跨集合查询数据?