简体   繁体   English

GraphQL 中继返回一条记录

[英]GraphQL relay return one record

I using graphql-relay in my project我在我的项目中使用了 graphql-relay

When run this query in my grapgql:在我的 Grapgql 中运行此查询时:

{
  viewer{
    boRelayCustomerInfo(_id:"59267769de82262d7e39c47c") {
      edges {
        node {
          id
        }
      }
    } 
  }
}

I gave this error: "message": "arraySlice.slice is not a function"我给出了这个错误: "message": "arraySlice.slice is not a function"

My Query code is :我的查询代码是:

import {
  GraphQLID,
  GraphQLNonNull,
} from 'graphql'

import {
  connectionArgs,
  connectionFromPromisedArray,
} from 'graphql-relay'


import { customerConnection } from '@/src/schema/type/customer/CustomerType'
import { CustomerModel, ObjectId } from '@/src/db'


export default {
  type: customerConnection.connectionType,
  args: {
    ...connectionArgs,
    _id: {
      type: new GraphQLNonNull(GraphQLID),
    },
  },
  resolve: (_, args) => connectionFromPromisedArray(
    CustomerModel.findOne({_id: ObjectId(args._id)}),
    args,
  ),
}

please tell us, how to return only one record in relay.请告诉我们,如何在中继中只返回一个记录。

As stated in the official document graphql-relay-js如官方文档graphql-relay-js 中所述

connectionFromPromisedArray takes a promise that resolves to an array connectionFromPromisedArray接受一个解析为数组的承诺

so, the problem here is with the first parameter passed in the connectionFromPromisedArray method.所以,这里的问题在于connectionFromPromisedArray方法中传递的第一个参数。 ie: CustomerModel.findOne({_id: ObjectId(args._id)})即: CustomerModel.findOne({_id: ObjectId(args._id)})

where findOne returns an object, instead you need to use find to get the response as an array.其中findOne返回一个对象,而您需要使用find将响应作为数组获取。

Problem code:问题代码:

  resolve: (_, args) => connectionFromPromisedArray(
    CustomerModel.findOne({_id: ObjectId(args._id)}), // <=== Error
    args,
  ),

Solution to the problem:问题的解决方法:

 resolve: (_, args) => connectionFromPromisedArray(
    CustomerModel.find({_id: ObjectId(args._id)}), // <=== Solution
    args,
  ),

Hope it helps :)希望能帮助到你 :)

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

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