GraphQL继电器返回一条记录
问题描述:
我使用graphql继电器,我的项目GraphQL继电器返回一条记录
当我grapgql运行此查询:
{
viewer{
boRelayCustomerInfo(_id:"59267769de82262d7e39c47c") {
edges {
node {
id
}
}
}
}
}
我给了这个错误: "message": "arraySlice.slice is not a function"
我的查询代码:
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,
),
}
请告诉我们,如何只返回一个re在继电器的绳子。
答
由于正式文件graphql-relay-js
connectionFromPromisedArray takes a promise that resolves to an array
中陈述所以,这里的问题是在connectionFromPromisedArray方法传递的第一个参数。 即: CustomerModel.findOne({_id: ObjectId(args._id)})
其中findOne返回一个对象,而不是你需要使用找到获得响应作为一个数组。
问题代码:
resolve: (_, args) => connectionFromPromisedArray(
CustomerModel.findOne({_id: ObjectId(args._id)}), // <=== Error
args,
),
解决的问题:
resolve: (_, args) => connectionFromPromisedArray(
CustomerModel.find({_id: ObjectId(args._id)}), // <=== Solution
args,
),
希望它能帮助:)