При использовании пакета graphql-js в моей программе Node.js я вижу распознаватель QueryRoot, который должен возвращать список пользовательских объектов, возвращает список с копиями последнего элемента того, что он разрешает.
const Person = new GraphQLObjectType({
name: "Person",
description: "This represents Person type",
fields: () => ({
favorite_game: { type: new GraphQLNonNull(GraphQLString) },
favorite_movie: { type: new GraphQLNonNull(GraphQLString) }
})
});
const QueryRootType = new GraphQLObjectType({
name: "Schema",
description: "Query Root",
fields: () => ({
persons: {
type: new GraphQLNonNull(new GraphQLList(Person)),
description: "List of persons",
args: {
input: {
type: new GraphQLNonNull(Name)
}
},
resolve: async (rootValue, input) => {
return new Promise(function(resolve, reject) {
getRESTPersonsByName(input, function(searchresults) {
console.log(JSON.stringify(searchresults));
resolve(searchresults);
});
});
}
}
})
});
const Schema = new GraphQLSchema({
query: QueryRootType
});
module.exports = Schema;
Результат, как показано ниже, показывает копии последнего лица в списке из 5 решенных. Я также проверил это в распознавателе на уровне поля, все еще видя, что туда входит тот же человек. (Функция распознавателя getRESTPersonsByName, как и ожидалось, получает точные результаты из базового вызова API)
{
data:
{
persons :
[{
"favorite_game" : "half life",
"favorite_movie" : "life of pi"
},
{
"favorite_game" : "half life",
"favorite_movie" : "life of pi"
},
{
"favorite_game" : "half life",
"favorite_movie" : "life of pi"
},
{
"favorite_game" : "half life",
"favorite_movie" : "life of pi"
},
{
"favorite_game" : "half life",
"favorite_movie" : "life of pi"
}]
}
}
Функция getRESTPersonsByName работает примерно так -
function getRESTPersonsByName(input, callback) {
let searchresults = [];
let person1 = {favorite_game: "spider-man", favorite_movie: "spider-man"};
let person2 = {favorite_game: "god of war", favorite_movie: "lord of the rings"};
let person3 = {favorite_game: "forza horizon 4", favorite_movie: "fast and the furios"};
let person4 = {favorite_game: "super mario", favorite_movie: "super mario bros"};
let person5 = {favorite_game: "half life", favorite_movie: "life of pi"};
searchresults.push(person1);
searchresults.push(person2);
searchresults.push(person3);
searchresults.push(person4);
searchresults.push(person5);
console.log(JSON.stringify(searchresults));
return callback(searchresults);
}