我有一个嵌套的GraphQL结构。
export default new GraphQLObjectType({
name: "Disbursement",
fields: () => ({
disbursementId: {
type: new GraphQLNonNull(GraphQLID)
}
transaction: {
type: Transaction
}
})
});
export default new GraphQLObjectType({
name: "Transaction",
args: {
limit: {
type: GraphQLInt,
},
},
fields: () => ({
transactionId: {
type: GraphQLID
}
})
});
当我尝试查询Disbursement
时,我希望能够将限制传递给Transaction
query {
allDisbursements {
transaction(limit:10) {
transactionId
}
}
}
但Transaction
我没有限制。我究竟做错了什么?我错过了什么?
答案 0 :(得分:1)
您的架构设置有点错误,您需要在字段上放置args。你想要更像以下的东西。请参阅launchpad示例https://launchpad.graphql.com/0vrj5p80k5
import {
GraphQLObjectType, GraphQLNonNull, GraphQLID, GraphQLSchema, GraphQLInt, GraphQLList } from 'graphql'
const Transaction = new GraphQLObjectType({
name: "Transaction",
fields: () => ({
transactionId: {
type: GraphQLID
}
})
})
const Disbursement = new GraphQLObjectType({
name: "Disbursement",
fields: () => ({
disbursementId: {
type: new GraphQLNonNull(GraphQLID)
},
transaction: {
args: {
limit: {
type: GraphQLInt,
},
},
type: new GraphQLList(Transaction),
resolve (source, args) {
return args.limit ? source.transaction.slice(0, args.limit) : source.transaction
}
}
})
})
const Query = new GraphQLObjectType({
name: 'Query',
fields: {
allDisbursements: {
type: new GraphQLList(Disbursement),
resolve () {
return [
{ disbursementId: 1, transaction: [{ transactionId: 1 }, { transactionId: 2 }] },
{ disbursementId: 2, transaction: [{ transactionId: 5 }, { transactionId: 3 }] }
]
}
}
}
})
// Required: Export the GraphQL.js schema object as "schema"
export const schema = new GraphQLSchema({
query: Query
})