2017-12-01 4 views
0

Ich habe eine verschachtelte GraphQL-Struktur.Warum kann ich verschachtelte Argumente nicht sehen?

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 
    } 
    }) 
}); 

Wenn ich versuche, die Disbursement abzufragen, ich möchte in der Lage sein, eine Grenze zu Transaction

query { 
    allDisbursements { 
    transaction(limit:10) { 
     transactionId 
    } 
    } 
} 

Aber ich keine Begrenzung auf Transaction zur Verfügung haben zu bestehen. Was mache ich falsch? Was vermisse ich?

+0

Sie können Args nicht auf einen Typ platzieren, sie gehen auf ein Feld – vbranden

Antwort

1

Ihr Schema ist ein bisschen falsch eingerichtet und Sie müssen Args auf einem Feld platzieren. Sie möchten etwas mehr wie folgt. siehe Beispiel für das 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 
}) 
Verwandte Themen