I have a typeDefs that is not returning a nested object
I would like to query the apartment with, that has owner and document in a nested query, but it is returning null.
the file has reference to the respective data file.
const apartments = require('./data/apartment.json');
const person = require('./data/person.json');
const document = require('./data/document.json');
the apartment query is returning correctly, but the nested info (owner and respective documents) is returning null
query {
apartments{
id
code
owner{
id
name
document{
name
type
}
}
}
const typeDefs = gql`
type Query {
apartments(
id:ID
code: String
description: String
createdAt: String
updatedAt: String
): [Apartment]
person:[Person]
}
type Apartment {
id: ID
code: String
description: String
owner:[Person]
createdAt: String
updatedAt: String
}
type Person {
id: ID
name: String
document: [Document]
createdAt: String
updatedAt: String
}
type Document {
id: ID
name: String
type: String
number: String
createdAt: String
updatedAt: String
}
`
this is the resolver
const resolvers = {
Query: {
apartments: () => {
return apartments;
},
person: () => {
return person;
},
document: () => {
return document;
}
}
}
can anyone help to retrieve the nested data?
I was missing the resolver, so I updated the field to people and updated the resolver
people(parent) {
return people.filter(person => person.apartmentId === parent.apartmentId);
}
This is the new schema
// Schema definition
const typeDefs = gql`
# A Block has a blockName and Apartments
type Block {
blockName: String!
blockCode: String!
apartments: [Apartment!]
}
# A Apartment has a apartmentId and people
type Apartment {
apartmentId: String!
people: [Person!]
}
# People has a name
type Person {
personName: String!
}
# Queries can fetch a list of libraries
type Query {
blocks: [Block]
}
`;
now the resolver map look like this:
// Resolver map
const resolvers = {
Query: {
blocks() {
// Return our hardcoded array of blocks
return blocks;
}
},
Block: {
apartments(parent) {
// Filter the hardcoded array of blocks to only include the respective apartment in the blockName
return apartments.filter(apartment => apartment.blockName === parent.blockName);
}
},
Apartment: {
// filter all the people that live at the same apartment
people(parent) {
return people.filter(person => person.apartmentId === parent.apartmentId);
}
}
};
and the Nested query looks like this
query GetPeopleByBlockAndApartment {
blocks {
blockCode
blockName
apartments {
apartmentId
people {
personName
}
}
}
}
Thanks a lot for everyone that tried to help.