繁体   English   中英

如何查询firestore()中的graphQL解析器?

[英]How to query firestore() for graphQL resolver?

我将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查询以返回“ room”集合中的所有房间。 我已经能够使用以下命令将它发送到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()返回一个promise(和错误),该promise可以传递给snapshotToArray()。

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;
        })

万一有人遇到相同的问题:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM