GraphQL:如何进行嵌套查询?

时间:2019-05-21 14:50:07

标签: graphql

我定义了两个独立的查询,分别为 nextgamedaylf1 teamclub 。两者都可以很好地隔离,但是我想在nextgamedaylf1查询中包含teamclub查询。

为此,我在nextgamedaylf1的架构中创建了一个名为home_team的字段,该字段来自teamclub类型。

import { 
    GraphQLObjectType,
    GraphQLNonNull,
    GraphQLID,
    GraphQLInt,
    GraphQLBoolean
} from "graphql";
import {DateTime} from "../scalar/dateTime";
import {TeamClub} from "./teamclub.schema";

const NextGameday = new GraphQLObjectType({
    name: "NextGameday",
    description: "List of games of next gameday",
    fields: () => {
        return {
            id: {
                type: new GraphQLNonNull(GraphQLID),
                description: "ID of NextGameday",
                resolve(ng){
                    return ng.id
                }                
            },
            id_competition: {
                type: new GraphQLNonNull(GraphQLInt),
                description: "ID of competition",
                resolve(ng){
                    return ng.id_competition
                }
            },
            id_home_team: {
                type: new GraphQLNonNull(GraphQLInt),
                description: "ID of home team",
                resolve(ng){
                    return ng.id_home_team
                }
            },            
            home_team: {
                type: TeamClub
            },
            id_away_team: {
                type: new GraphQLNonNull(GraphQLInt),
                description: "ID of away team",
                resolve(ng){
                    return ng.id_away_team
                }
            },
            date: {
                type: new GraphQLNonNull(DateTime),
                description: "Date time of the game",
                resolve(ng){
                    return ng.date
                }
            },
            played: {
                type: new GraphQLNonNull(GraphQLBoolean),
                description: "Game played. 0: Game NOT played. 1: Game played.",
                resolve(ng){
                    return ng.played
                }
            }
        }
    }
});

module.exports.NextGameday = NextGameday;

当我尝试从GraphiQL获取数据时,出现此错误:

那么,如何使用GraphQL进行嵌套查询?

teamclub的架构:

import { 
    GraphQLObjectType,
    GraphQLNonNull,
    GraphQLID,
    GraphQLInt,
    GraphQLString
} from "graphql";
import {DateTime} from "../scalar/dateTime";

const TeamClub = new GraphQLObjectType({
    name: "TeamClub",
    description: "Data from a team which belongs to a club",
    fields: () => {
        return {
            id: {
                type: new GraphQLNonNull(GraphQLID),
                description: "ID of team club",
                resolve(tc){
                    return tc.id
                },       
            },
            id_league: {
                type: new GraphQLNonNull(GraphQLInt),
                description: "ID of league",
                resolve(tc){
                    return tc.id_league
                }
            },
            id_season: {
                type: new GraphQLNonNull(GraphQLInt),
                description: "ID of season",
                resolve(tc){
                    return tc.id_season
                }
            },
            id_club: {
                type: new GraphQLNonNull(GraphQLInt),
                description: "ID of Club",
                resolve(tc){
                    return tc.id_club
                }
            },
            id_team_feb: {
                type: new GraphQLNonNull(GraphQLInt),
                description: "ID of FEB",
                resolve(tc){
                    return tc.id_team_feb
                }
            },
            logo: {
                type: GraphQLString,
                description: "url to the logo",
                resolve(tc){
                    return tc.logo
                }
            },
            name: {
                type: new GraphQLNonNull(GraphQLString),
                description: "Name of the team which belongs to a club",
                resolve(tc){
                    return tc.name
                }
            },
            abrev: {
                type: GraphQLString,
                description: "Abreviature of the name of the team",
                resolve(tc){
                    return tc.abrev
                }
            },
            url_name: {
                type: new GraphQLNonNull(GraphQLString),
                description: "Name for the url of the team",
                resolve(tc){
                    return tc.url_name
                }
            },
            r_name: {
                type: GraphQLString,
                description: "Name of the team for R graphics",
                resolve(tc){
                    return tc.r_name
                }
            },
            date_start: {
                type: new GraphQLNonNull(DateTime),
                description: "Date of insert in database",
                resolve(tc){
                    return tc.date_start
                }
            }
        }
    }
});

module.exports.TeamClub = TeamClub;

0 个答案:

没有答案