Answer the question
In order to leave comments, you need to log in
How to display an array in a nested GraphQL query?
node server.
There is a user and his images, there are two arrays of objects. Images are attached to users.
Data from DB
obj =[{
id: 1,
name: "qwe",
img: [
{id: 1, name: "img1"},
{id: 2, name: "img2"},
{id: 3, name: "img3"},
]
}]
{
user(id:1){
id name
img{id name }
}
}
{
"id": 1,
"name": "qwe",
"img": {
"id": null
}
}
{
"id": 1,
"name": "qwe",
"img":[
{id: 1, name: "img1"},
{id: 2, name: "img2"},
{id: 3, name: "img3"}
]
}
Answer the question
In order to leave comments, you need to log in
The problem is solved, and did not understand why it did not work. But this time all is well.
Scheme
const graphql = require('graphql');
const img = new graphql.GraphQLObjectType({
name: "img",
fields: () => {
return {
id: {type: graphql.GraphQLInt},
idUser: {type: graphql.GraphQLInt},
name: {type: graphql.GraphQLString}
}
}
})
const user = new graphql.GraphQLObjectType({
name: 'user',
description: 'This represents a Person',
fields: () => {
return {
id: {
type: graphql.GraphQLInt,
resolve (res) {
return res.id;
}
},
mail: {
type: graphql.GraphQLString,
resolve (res) {
return res.mail;
}
},
password: {
type: graphql.GraphQLString,
resolve (res) {
return "privat info";
}
},
name: {
type: graphql.GraphQLString,
resolve (res) {
return res.name;
}
},
img: {
type: new graphql.GraphQLList(img),
}
};
}
});
module.exports = user;
const graphql = require('graphql');
const userSchema = require("../schema/userSchema")
module.exports = {
type: new graphql.GraphQLList(userSchema),
args: {
id: {type: new graphql.GraphQLNonNull(graphql.GraphQLInt)}
},
resolve (source, args, context) {
return [
{
id: 123,
name: "pero",
img:
[
{id: 1, name: "pero111"},
{id: 2, name: "pero112"},
{id: 3, name: "pero113"}
]
}
]
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question