为什么我的 Graphql 查询返回 null?
Why is my Graphql query returning null?
我正在尝试为所有人设置一个 graphql 应用程序,以便轻松访问来自 games.espn.com 的数据,但我遇到了查询问题 returning null。我想知道我是否可能在某处错过了 return 或 resolve 功能?几天来我一直在搜索这段代码,但似乎无法弄清楚为什么这不是 returning 值。
这是我的 schema.js 文件:
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema,
GraphQLList,
GraphQLNonNull,
GraphQLBoolean,
GraphQLFloat
} = require('graphql');
const axios = require('axios');
const request = require('request');
const PlayerType = new GraphQLObjectType({
name: 'Player',
fields:() => ({
droppable: {type:GraphQLBoolean},
percentStarted: {type:GraphQLFloat},
jersey: {type:GraphQLString},
playerRatingSeason: {type:GraphQLFloat},
isIREligible: {type:GraphQLBoolean},
draftRank: {type:GraphQLInt},
universeId: {type:GraphQLInt},
firstName: {type:GraphQLString},
lastName: {type:GraphQLString},
sportsId: {type:GraphQLInt},
healthStatus: {type:GraphQLInt},
percentOwned: {type:GraphQLFloat},
proTeamId: {type:GraphQLInt},
tickerId: {type:GraphQLInt},
isActive: {type:GraphQLBoolean},
playerId: {type:GraphQLInt},
percentChange: {type:GraphQLFloat},
defaultPositionId: {type: GraphQLInt},
totalPoints: {type:GraphQLFloat},
})
});
const CurrentPeriodProjectedStatsType = new GraphQLObjectType({
name: 'CurrentPeriodProjectedStats',
fields:() => ({
appliedProjectedStatTotal: {type:GraphQLFloat}
})
});
const CurrentPeriodRealStatsType = new GraphQLObjectType({
name: 'CurrentPeriodRealStats',
fields:() => ({
appliedRealStatTotal: {type:GraphQLFloat}
})
});
const PlayerSlotType = new GraphQLObjectType({
name: 'PlayerSlot',
fields:() => ({
pvoRank: {type:GraphQLInt},
player: {
type: PlayerType
},
watchList: {type:GraphQLBoolean},
isKeeper: {type:GraphQLBoolean},
isTradeLocked: {type:GraphQLBoolean},
currentPeriodProjectedStats: {
type: CurrentPeriodProjectedStatsType
},
opponentProTeamId: {type:GraphQLInt},
slotCategoryId: {type:GraphQLInt},
lockStatus: {type:GraphQLInt},
isQueuedWaiverLocked: {type:GraphQLBoolean},
currentPeriodRealStats: {
type: CurrentPeriodRealStatsType
}
})
});
const SlotsType = new GraphQLObjectType({
name: 'Slots',
fields:() => ({
player0: {
type: PlayerSlotType
},
player1: {
type: PlayerSlotType
},
player2: {
type: PlayerSlotType
},
player3: {
type: PlayerSlotType
},
player4: {
type: PlayerSlotType
},
player5: {
type: PlayerSlotType
},
player6: {
type: PlayerSlotType
},
player7: {
type: PlayerSlotType
},
player8: {
type: PlayerSlotType
},
player9: {
type: PlayerSlotType
},
player10: {
type: PlayerSlotType
},
player11: {
type: PlayerSlotType
},
player12: {
type: PlayerSlotType
},
player13: {
type: PlayerSlotType
},
player14: {
type: PlayerSlotType
},
player15: {
type: PlayerSlotType
},
})
});
const DivisionType = new GraphQLObjectType({
name: 'Division',
fields:() => ({
divisionName: {type:GraphQLString},
divisionId: {type:GraphQLInt},
size: {type:GraphQLInt}
})
});
const TeamType = new GraphQLObjectType({
name: 'Team',
fields:() => ({
divisionStanding: {type:GraphQLInt},
overallStanding: {type:GraphQLInt},
waiverRank: {type:GraphQLInt},
division: {
type: DivisionType
},
teamAbbrev: {type:GraphQLString},
teamNickname: {type:GraphQLString},
logoUrl: {type:GraphQLString},
teamLocation: {type:GraphQLString},
teamId: {type:GraphQLInt},
logoType: {type:GraphQLString}
})
});
const List0Type = new GraphQLObjectType({
name: 'List0',
fields: () => ({
slots: {
type: SlotsType
},
team: {
type: TeamType
},
teamId: {type: GraphQLInt},
appliedActiveProjectedTotal: {type: GraphQLFloat},
appliedInactiveProjectedTotal: {type: GraphQLFloat},
appliedActiveRealTotal: {type: GraphQLFloat},
appliedInactiveRealTotal: {type: GraphQLFloat},
})
});
const TeamsType = new GraphQLObjectType({
name: 'Teams',
fields: () => ({
list0: {
type: List0Type
},
list1: {
type: List0Type
}
})
});
// need to define each type individually, working from the bottom up and creating types as needed
const BoxscoreType = new GraphQLObjectType({
name: 'Boxscore',
fields: () => ({
teams: {
type: TeamsType,
/*resolve(boxscore){
return boxscore.teams;
}*/
},
scoringPeriodId: {
type: GraphQLInt,
},
matchupPeriodId: {
type: GraphQLInt,
},
homeTeamBonus: {
type: GraphQLInt,
}
})
});
const MetadataType = new GraphQLObjectType({
name: 'metadata',
fields: {
leagueId: {type: GraphQLString},
status: {type: GraphQLString},
dateModifiedLeague: {type: GraphQLString},
seasonId: {type: GraphQLString},
}
});
const BoxscoreDataType = new GraphQLObjectType({
name: 'BoxscoreData',
fields: {
boxscore: {type:BoxscoreType},
metadata: {type:MetadataType},
},
});
const EspnQuery = new GraphQLObjectType({
name: 'EspnQuery',
fields: {
getBoxscore: {
type: BoxscoreDataType,
args: {
leagueId: {
name: 'leagueId',
type: new GraphQLNonNull(GraphQLInt)
},
seasonId: {
name: 'seasonId',
type: new GraphQLNonNull(GraphQLInt)
},
teamId: {
name: 'teamId',
type: new GraphQLNonNull(GraphQLInt)
},
scoringPeriodId: {
name: 'scoringPeriodId',
type: new GraphQLNonNull(GraphQLInt)
},
},
resolve: (obj, {leagueId, seasonId, teamId, scoringPeriodId}) => {
const url = 'http://games.espn.com/ffl/api/v2/boxscore?leagueId=1150587&seasonId=2017&teamId=5&scoringPeriodId=7'
//const url = 'http://games.espn.com/ffl/api/v2/boxscore?leagueId='+ leagueId + '&seasonId=' + seasonId + '&teamId=' + teamId + '&scoringPeriodId=' + scoringPeriodId
//console.log('leagueId is: ' + leagueId + 'seasonId is: '+seasonId+'teamId is: '+teamId+'scoringPeriodId is: '+scoringPeriodId);
return axios(url)
.then(res => res.data);
}
}
},
});
// Keep at the bottom //
module.exports = new GraphQLSchema({
query: EspnQuery
});
我在 Graphiql 中 运行 的查询是:
{
getBoxscore(leagueId: 1150587, seasonId: 2017, teamId: 5, scoringPeriodId: 7) {
boxscore{
teams {
list0{
slots{
player0{
player{
firstName
}
}
}
}
}
}
}
}
不幸的是 returning:
{
"data": {
"getBoxscore": {
"boxscore": {
"teams": {
"list0": null
}
}
}
}
}
您的模式结构与数据结构不匹配。在返回数组的地方,您应该使用 GraphQLList
——不需要您添加的 ListType
。
例如,如果您查看端点返回的 JSON,teams
是一个数组,而不是对象。您已经创建了一个匹配团队数据结构的 TeamType
,但是我们需要告诉 GraphQL teams
将成为 TeamType
对象的列表(数组),而不仅仅是一个。所以我们写:
teams: { type: new GraphQLList(TeamsType) }
用 GraphQL 模式语言编写,即 [TeamsType]
。 TeamsType 中的大多数字段都是对象或标量。但是,slots
也是一个数组,因此您可以类似地写:
slots: { type: new GraphQLList(SlotsType) }
以此类推
我正在尝试为所有人设置一个 graphql 应用程序,以便轻松访问来自 games.espn.com 的数据,但我遇到了查询问题 returning null。我想知道我是否可能在某处错过了 return 或 resolve 功能?几天来我一直在搜索这段代码,但似乎无法弄清楚为什么这不是 returning 值。
这是我的 schema.js 文件:
const {
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLSchema,
GraphQLList,
GraphQLNonNull,
GraphQLBoolean,
GraphQLFloat
} = require('graphql');
const axios = require('axios');
const request = require('request');
const PlayerType = new GraphQLObjectType({
name: 'Player',
fields:() => ({
droppable: {type:GraphQLBoolean},
percentStarted: {type:GraphQLFloat},
jersey: {type:GraphQLString},
playerRatingSeason: {type:GraphQLFloat},
isIREligible: {type:GraphQLBoolean},
draftRank: {type:GraphQLInt},
universeId: {type:GraphQLInt},
firstName: {type:GraphQLString},
lastName: {type:GraphQLString},
sportsId: {type:GraphQLInt},
healthStatus: {type:GraphQLInt},
percentOwned: {type:GraphQLFloat},
proTeamId: {type:GraphQLInt},
tickerId: {type:GraphQLInt},
isActive: {type:GraphQLBoolean},
playerId: {type:GraphQLInt},
percentChange: {type:GraphQLFloat},
defaultPositionId: {type: GraphQLInt},
totalPoints: {type:GraphQLFloat},
})
});
const CurrentPeriodProjectedStatsType = new GraphQLObjectType({
name: 'CurrentPeriodProjectedStats',
fields:() => ({
appliedProjectedStatTotal: {type:GraphQLFloat}
})
});
const CurrentPeriodRealStatsType = new GraphQLObjectType({
name: 'CurrentPeriodRealStats',
fields:() => ({
appliedRealStatTotal: {type:GraphQLFloat}
})
});
const PlayerSlotType = new GraphQLObjectType({
name: 'PlayerSlot',
fields:() => ({
pvoRank: {type:GraphQLInt},
player: {
type: PlayerType
},
watchList: {type:GraphQLBoolean},
isKeeper: {type:GraphQLBoolean},
isTradeLocked: {type:GraphQLBoolean},
currentPeriodProjectedStats: {
type: CurrentPeriodProjectedStatsType
},
opponentProTeamId: {type:GraphQLInt},
slotCategoryId: {type:GraphQLInt},
lockStatus: {type:GraphQLInt},
isQueuedWaiverLocked: {type:GraphQLBoolean},
currentPeriodRealStats: {
type: CurrentPeriodRealStatsType
}
})
});
const SlotsType = new GraphQLObjectType({
name: 'Slots',
fields:() => ({
player0: {
type: PlayerSlotType
},
player1: {
type: PlayerSlotType
},
player2: {
type: PlayerSlotType
},
player3: {
type: PlayerSlotType
},
player4: {
type: PlayerSlotType
},
player5: {
type: PlayerSlotType
},
player6: {
type: PlayerSlotType
},
player7: {
type: PlayerSlotType
},
player8: {
type: PlayerSlotType
},
player9: {
type: PlayerSlotType
},
player10: {
type: PlayerSlotType
},
player11: {
type: PlayerSlotType
},
player12: {
type: PlayerSlotType
},
player13: {
type: PlayerSlotType
},
player14: {
type: PlayerSlotType
},
player15: {
type: PlayerSlotType
},
})
});
const DivisionType = new GraphQLObjectType({
name: 'Division',
fields:() => ({
divisionName: {type:GraphQLString},
divisionId: {type:GraphQLInt},
size: {type:GraphQLInt}
})
});
const TeamType = new GraphQLObjectType({
name: 'Team',
fields:() => ({
divisionStanding: {type:GraphQLInt},
overallStanding: {type:GraphQLInt},
waiverRank: {type:GraphQLInt},
division: {
type: DivisionType
},
teamAbbrev: {type:GraphQLString},
teamNickname: {type:GraphQLString},
logoUrl: {type:GraphQLString},
teamLocation: {type:GraphQLString},
teamId: {type:GraphQLInt},
logoType: {type:GraphQLString}
})
});
const List0Type = new GraphQLObjectType({
name: 'List0',
fields: () => ({
slots: {
type: SlotsType
},
team: {
type: TeamType
},
teamId: {type: GraphQLInt},
appliedActiveProjectedTotal: {type: GraphQLFloat},
appliedInactiveProjectedTotal: {type: GraphQLFloat},
appliedActiveRealTotal: {type: GraphQLFloat},
appliedInactiveRealTotal: {type: GraphQLFloat},
})
});
const TeamsType = new GraphQLObjectType({
name: 'Teams',
fields: () => ({
list0: {
type: List0Type
},
list1: {
type: List0Type
}
})
});
// need to define each type individually, working from the bottom up and creating types as needed
const BoxscoreType = new GraphQLObjectType({
name: 'Boxscore',
fields: () => ({
teams: {
type: TeamsType,
/*resolve(boxscore){
return boxscore.teams;
}*/
},
scoringPeriodId: {
type: GraphQLInt,
},
matchupPeriodId: {
type: GraphQLInt,
},
homeTeamBonus: {
type: GraphQLInt,
}
})
});
const MetadataType = new GraphQLObjectType({
name: 'metadata',
fields: {
leagueId: {type: GraphQLString},
status: {type: GraphQLString},
dateModifiedLeague: {type: GraphQLString},
seasonId: {type: GraphQLString},
}
});
const BoxscoreDataType = new GraphQLObjectType({
name: 'BoxscoreData',
fields: {
boxscore: {type:BoxscoreType},
metadata: {type:MetadataType},
},
});
const EspnQuery = new GraphQLObjectType({
name: 'EspnQuery',
fields: {
getBoxscore: {
type: BoxscoreDataType,
args: {
leagueId: {
name: 'leagueId',
type: new GraphQLNonNull(GraphQLInt)
},
seasonId: {
name: 'seasonId',
type: new GraphQLNonNull(GraphQLInt)
},
teamId: {
name: 'teamId',
type: new GraphQLNonNull(GraphQLInt)
},
scoringPeriodId: {
name: 'scoringPeriodId',
type: new GraphQLNonNull(GraphQLInt)
},
},
resolve: (obj, {leagueId, seasonId, teamId, scoringPeriodId}) => {
const url = 'http://games.espn.com/ffl/api/v2/boxscore?leagueId=1150587&seasonId=2017&teamId=5&scoringPeriodId=7'
//const url = 'http://games.espn.com/ffl/api/v2/boxscore?leagueId='+ leagueId + '&seasonId=' + seasonId + '&teamId=' + teamId + '&scoringPeriodId=' + scoringPeriodId
//console.log('leagueId is: ' + leagueId + 'seasonId is: '+seasonId+'teamId is: '+teamId+'scoringPeriodId is: '+scoringPeriodId);
return axios(url)
.then(res => res.data);
}
}
},
});
// Keep at the bottom //
module.exports = new GraphQLSchema({
query: EspnQuery
});
我在 Graphiql 中 运行 的查询是:
{
getBoxscore(leagueId: 1150587, seasonId: 2017, teamId: 5, scoringPeriodId: 7) {
boxscore{
teams {
list0{
slots{
player0{
player{
firstName
}
}
}
}
}
}
}
}
不幸的是 returning:
{
"data": {
"getBoxscore": {
"boxscore": {
"teams": {
"list0": null
}
}
}
}
}
您的模式结构与数据结构不匹配。在返回数组的地方,您应该使用 GraphQLList
——不需要您添加的 ListType
。
例如,如果您查看端点返回的 JSON,teams
是一个数组,而不是对象。您已经创建了一个匹配团队数据结构的 TeamType
,但是我们需要告诉 GraphQL teams
将成为 TeamType
对象的列表(数组),而不仅仅是一个。所以我们写:
teams: { type: new GraphQLList(TeamsType) }
用 GraphQL 模式语言编写,即 [TeamsType]
。 TeamsType 中的大多数字段都是对象或标量。但是,slots
也是一个数组,因此您可以类似地写:
slots: { type: new GraphQLList(SlotsType) }
以此类推