如何在graphql查询中获取相关架构?

时间:2019-07-16 12:04:39

标签: node.js mongodb mongoose graphql graphql-compose-mongoose

我正在使用带有graphql-compose-mongoose的graphql。

如何获取用户对象ID数组?

我尝试处理许多不同的查询,但是没有用。

这是我的查询,用于获取具有类别和用户对象的文档:

{
  docOne {
    name,
    categories {
      name
      _id
      users
    }
  }
}

给我一​​组ID。我希望能排列用户对象。

{
  "data": {
    "docOne": {
      "name": "main",
      "categories": [
        {
          "name": "Category1",
          "_id": "5ca37e1d219190011742473",
          "users": [
            "5c6c0213f081954bd7d4a38c",
            "5c6c02948e8191a16529a1a1",
            "5c6c02ee7e81912075850119"
          ]
        },
        {
          "name": "Category2",
          "_id": "5cae46e1100250001222f9a",
          "users": [
            "5c6c02948e0081a16529a1a1",
            "5c8e2346ac7819d0e75ebac6",
            "5c8e2a49432819ebd73b5fc2",
          ]
        },
      ]
    }
  }
}

这是我如何防御结构:

import mongoose, { Document, Schema } from 'mongoose';
import composeWithMongoose from 'graphql-compose-mongoose';
import { GraphQLNonNull, GraphQLList } from 'graphql';

const UserSchema = new Schema({
  name: { type: String }
});

const DocSchema = new Schema({
  name: { type: String },
  categories: [
    {
      name: { type: String },
      users: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
    },
  ],
});

export const User = mongoose.model<IMongooseUser>('User', UserSchema);
export const Doc = mongoose.model<IMongooseUser>('Doc', UserSchema);
const UserTC = composeWithMongoose(User);
const DocTC = composeWithMongoose(Doc);

const USER_QUERIES = {
  userById: UserTC.getResolver('findById'),
  userByIds: UserTC.getResolver('findByIds'),
  userOne: UserTC.getResolver('findOne'),
  userMany: UserTC.getResolver('findMany'),
  userCount: UserTC.getResolver('count'),
  userConnection: UserTC.getResolver('connection'),
  userPagination: UserTC.getResolver('pagination'),
};

const DOC_QUERIES = {
  docById: DocTC.getResolver('findById'),
  docByIds: DocTC.getResolver('findByIds'),
  docOne: DocTC.getResolver('findOne'),
  docMany: DocTC.getResolver('findMany'),
  docCount: DocTC.getResolver('count'),
  docConnection: DocTC.getResolver('connection'),
  docPagination: DocTC.getResolver('pagination'),
};

schemaComposer.Query.addFields({
 ...USER_QUERIES,
 ...DOC_QUERIES
});

const graphqlSchema = schemaComposer.buildSchema();
export default graphqlSchema;

0 个答案:

没有答案