如何查询 firestore() 以获取 graphQL 解析器?

Llo*_*rds 6 javascript firebase graphql google-cloud-firestore

我正在将 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
});
Run Code Online (Sandbox Code Playgroud)

在 RootQuery -> Rooms 下,我试图获取一个 graphQL 查询以返回我的“房间”集合中的所有房间。我已经能够使用以下命令将其获取到 console.log() 文档列表:

return Room.get()
.then(snapshot => {
snapshot.forEach(doc => {
        console.log(doc.id, " => ", doc.data());
Run Code Online (Sandbox Code Playgroud)

但是到目前为止,我一直无法将它放入一个数组中。任何帮助都非常感谢。

Llo*_*rds 6

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

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

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

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

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

      returnArr.push(item);
  });

  return returnArr;
};
Run Code Online (Sandbox Code Playgroud)

接下来,在获取数据时,您使用 .get() 返回一个可传递到 snapshotToArray() 的承诺(和错误)。

return Position.get().then((snapshot) => {
          return snapshotToArray(snapshot);
        }) 
Run Code Online (Sandbox Code Playgroud)

对于仅调用一个数据集的解析函数,例如 inRoom。除了在快照函数中使用 .where() 和分隔 id 和 data() 之外,它类似于第一个:

return Room.doc(parent.inRoomId).get().then((snapshot) => {
          var item = snapshot.data();
          item.id = snapshot.id;
          return item;
        })
Run Code Online (Sandbox Code Playgroud)

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