2016-02-25 12 views
7

Sto utilizzando GraphQL per interrogare un oggetto che sarà composto da circa 15 diverse chiamate REST. Questa è la mia query di root in cui passo l'ID della query. Funziona correttamente per l'oggetto studente principale che si risolve correttamente. Tuttavia, ho bisogno di capire come passare l'ID al risolutore di indirizzi. Ho provato ad aggiungere argomenti all'oggetto indirizzo ma ottengo un errore che indica che gli argomenti non vengono passati dall'oggetto Studente. Quindi la mia domanda è: come posso passare argomenti dalla query del client agli oggetti secondari in un server GraphQL?GraphQL: come si passano gli argomenti agli oggetti secondari

let rootQuery = new GraphQLObjectType({ 
    name: 'Query', 
    description: `The root query`, 
    fields:() => ({ 
     Student : { 
      type: Student , 
      args: { 
       id: { 
        name: 'id', 
        type: new GraphQLNonNull(GraphQLString) 
       } 
      }, 
      resolve: (obj, args, ast) => { 
       return Resolver(args.id).Student(); 
      } 
     } 
    }) 
}); 

export default rootQuery; 

Questo è il mio oggetto studente principale che collego gli altri oggetti. In questo caso ho allegato l'oggetto ADDRESS.

import { 
GraphQLInt, 
GraphQLObjectType, 
GraphQLString, 
GraphQLNonNull, 
GraphQLList 
} from 'graphql'; 

import Resolver from '../../resolver.js' 
import iAddressType from './address.js' 

let Student = new GraphQLObjectType({ 
    name: 'STUDENT', 
    fields:() => ({ 
     SCHOOLCODE: { type: GraphQLString }, 
     LASTNAME: { type: GraphQLString }, 
     ACCOUNTID: { type: GraphQLInt }, 
     ALIENIDNUMBER: { type: GraphQLInt }, 
     MIDDLEINITIAL: { type: GraphQLString }, 
     DATELASTCHANGED: { type: GraphQLString }, 
     ENROLLDATE: { type: GraphQLString }, 
     FIRSTNAME: { type: GraphQLString }, 
     DRIVERSLICENSESTATE: { type: GraphQLString }, 
     ENROLLMENTSOURCE: { type: GraphQLString }, 
     ADDRESSES: { 
      type: new GraphQLList(Address), 
      resolve(obj, args, ast){ 
       return Resolver(args.id).Address(); 
     }} 
    }) 
}); 

Ecco il mio oggetto indirizzo che viene risolto da una seconda chiamata REST:

let Address = new GraphQLObjectType({ 
    name: 'ADDRESS', 
    fields:() => ({ 
     ACTIVE: { type: GraphQLString }, 
     ADDRESS1: { type: GraphQLString }, 
     ADDRESS2: { type: GraphQLString }, 
     ADDRESS3: { type: GraphQLString }, 
     CAMPAIGN: { type: GraphQLString }, 
     CITY: { type: GraphQLString }, 
     STATE: { type: GraphQLString }, 
     STATUS: { type: GraphQLString }, 
     TIMECREATED: { type: GraphQLString }, 
     TYPE: { type: GraphQLString }, 
     ZIP: { type: GraphQLString }, 
    }) 

}); 

export default Address; 

Questi sono i miei resolver

var Resolver = (id) => { 

    var options = { 
     hostname: "myhostname", 
     port: 4000 
    }; 


    var GetPromise = (options, id, path) => { 
     return new Promise((resolve, reject) => { 
      http.get(options, (response) => { 
       var completeResponse = ''; 
       response.on('data', (chunk) => { 
        completeResponse += chunk; 
       }); 
       response.on('end',() => { 
        parser.parseString(completeResponse, (err, result) => { 
         let pathElements = path.split('.');      
         resolve(result[pathElements[0]][pathElements[1]]); 
        }); 
       }); 
      }).on('error', (e) => { }); 
     }); 
    }; 

    let Student=() => { 
     options.path = '/Student/' + id; 
     return GetPromise(options, id, 'GetStudentResult.StudentINFO'); 
    } 

    let Address=() => { 
     options.path = '/Address/' + id + '/All'; 
     return GetPromise(options, id, 'getAddressResult.ADDRESS'); 
    }; 


    return { 
     Student, 
     Address 
    }; 
} 

export default Resolver; 
+0

FallFast: hai trovato qualche soluzione – Brune

+0

Hai trovato una buona soluzione per questo? – James111

risposta

1
ADDRESSES: { 
    type: new GraphQLList(Address), 
    resolve(obj, args, ast){ 
     return Resolver(args.id).Address(); 
    } 
} 

args passati a indirizzi sono argomenti passati al Campo INDIRIZZI al tempo di interrogazione. Nel metodo di risoluzione, obj dovrebbe essere l'oggetto studente e se si dispone di una proprietà id, è sufficiente: return Resolver(obj.id).Address();.

Problemi correlati