I have a nested GraphQL structure.
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
}
})
});
When I try to query the Disbursement
, I want to be able to pass a limit to Transaction
query {
allDisbursements {
transaction(limit:10) {
transactionId
}
}
}
But I have no limit available on Transaction
. What am I doing wrong? What am I missing?
Your schema is set up a bit wrong and you need to place args on a field. you want something more like the following. see launchpad example 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
})