Reputation: 7021
I have 3 graphql queries that calls 3 different services :
{
individualQ{
name
}
addressQ{
address
}
contactQ{
phone
}
}
Each one of these 3 queries have its own resolver:
individualQ (root , args) {
const individual_options = getOption(cfg.INDIVIDUAL_ENDPOINT);
return axios(individual_options).then(res => res.data);
} ,
addressQ (root , args) {
const address_options = getOption(cfg.ADDRESS_ENDPOINT);
return axios(address_options).then(res => res.data);
} ,
contactQ (root , args) {
const contact_Options = getOption(cfg.CONTACT_ENDPOINT);
return axios(contact_Options).then(res => res.data);
}
Actually I want to create a new Query that assemble the three queries.
I want to find a manner to do as follow :
personalInfo {
individualQ{
name
}
addressQ{
address
}
contactQ{
phone
}
}
And so my request will be just
{
personalInfo
}
Upvotes: 0
Views: 240
Reputation: 84867
If you're looking to just combine your queries, you can add a new type and a new query. Something like this:
# Type definitions
type PersonalInfo {
individual: Individual
address: Address
contact: Contact
}
type Query {
personalInfo: PersonalInfo
# other queries
}
And then add resolvers for those three fields:
const resolvers = {
PersonalInfo: {
individual (root , args) {
const individual_options = getOption(cfg.INDIVIDUAL_ENDPOINT);
return axios(individual_options).then(res => res.data);
} ,
address (root , args) {
const address_options = getOption(cfg.ADDRESS_ENDPOINT);
return axios(address_options).then(res => res.data);
} ,
contact (root , args) {
const contact_Options = getOption(cfg.CONTACT_ENDPOINT);
return axios(contact_Options).then(res => res.data);
},
},
Query: {
personalInfo: () => ({})
},
}
We return an empty object for the query since our field resolvers are doing the heavy lifting, but we still need to return an object so that they will actually be triggered (otherwise you will get back null
for the query).
If you want to pass down anything to the resolvers (arguments for the query field, for example) you could do so through the object you return if you wanted. That data would then be available as the first parameter passed to each of the field resolvers (the "root" value).
Side note: if you're looking for a way to issue a GraphQL request with no selection of subfields, that's not going to be possible -- you always have to request exactly which fields you want when requesting a field that returns a type instead of a scalar.
Upvotes: 1