2017-10-04 102 views
2

我使用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在繼電器的繩子。

回答

1

由於正式文件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, 
), 

希望它能幫助:)