无法访问graphql spring Boot 应用程序中的后端

b1payxdu  于 2023-11-17  发布在  Spring
关注(0)|答案(1)|浏览(168)

我正在做一个简单的编码挑战,需要创建一个graphql应用程序并保存一些对象(球队,球员,比赛和教练)。我以前从未使用过graphql,所以我依赖于网络上的sove教程,比如:https://www.bezkoder.com/spring-boot-graphql-mysql-jpa/https://medium.com/@memredemir/creating-a-graphql-mysql-and-spring-boot-backend-service-32cec95f4436
由于某种原因,我无法访问后端进行调试:

这是我的模式与突变和类型:

# leagueSchema.graphqls

# Types

type Team {
    id: ID!
    name: String!
    tla: String
    shortName: String
    areaName: String
    address: String
    players: [Player!]
    coach: Coach
    competitions: [Competition!]
}

type Player {
    id: ID!
    name: String!
    position: String!
    dateOfBirth: String! # Utilizando String para representar LocalDate em GraphQL
    nationality: String
    team: Team
}

type Coach {
    id: ID!
    name: String!
    dateOfBirth: String!  # Representando LocalDate como String
    nationality: String
    team: Team
}

type Competition {
    id: ID!
    name: String!
    code: String
    areaName: String
    teams: [Team!]
}

# Queries
type Query {
    getAllTeams: [Team]
    getTeamById(id: ID!): Team
    getAllPlayers: [Player]
    getPlayerById(id: ID!): Player
    getAllCoaches: [Coach]
    getCoachById(id: ID!): Coach
    getAllCompetitions: [Competition]
    getCompetitionById(id: ID!): Competition
}

# Mutations
type Mutation {
    createTeam(name: String!, address: String, areaName: String, shortName: String, tla: String): Team
    createPlayer(name: String!, position: String!, dateOfBirth: String!, nationality: String, teamId: ID!): Player
    createCoach(name: String!, dateOfBirth: String!, nationality: String, teamId: ID!): Coach
    createCompetition(name: String!): Competition
    addTeamToCompetition(teamId: ID!, competitionId: ID!): Competition
    addPlayerToTeam(playerId: ID!, teamId: ID!): Team
    addCoachToTeam(coachId: ID!, teamId: ID!): Team
}

字符串
这是我的突变类:

@Component
public class TeamMutationResolver implements GraphQLMutationResolver {

    @Autowired
    private TeamService teamService;

    public Team createTeam(@Nonnull String name, String address, String areaName, String shortName, String tla) {
        return teamService.createTeam(name, address, areaName, shortName, tla);
    }

    public Team addPlayerToTeam(Long playerId, Long teamId) {
        return teamService.addPlayerToTeam(playerId, teamId);
    }

    public Team addCoachToTeam(Long coachId, Long teamId) {
        return teamService.addCoachToTeam(coachId, teamId);
    }
}


所以我试图调试把一个断点上的js团队的方法,但对 Postman 的请求是不是到达后端。我错过了什么?
这是存储库,以防你想复制:https://github.com/Romulo-S/TeamLeagueApp/tree/master

vxbzzdmp

vxbzzdmp1#

要使GraphQL在Sping Boot 中工作,您需要唯一的Maven依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-graphql</artifactId>
</dependency>

字符串
然后你需要一个与你的Graph QL模式对应的控制器类:

import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.MutationMapping;
import org.springframework.graphql.data.method.annotation.QueryMapping;
import org.springframework.stereotype.Controller;

@Controller
public class LinkGraphQLController {
    @QueryMapping
    public Team getTeamById(@Argument Long id) {
        ...
    }

    @MutationMapping
    public Team addPlayerToTeam(@Argument Long playerId, @Argument Long teamID) {
        ...
    }
}


标识符必须与您的架构完全匹配。
我个人没有使用Long作为Graph QL ID类型的经验,我总是使用java.util.UUID
对于强制参数(在模式中标记为!),只需在类上添加@org.springframework.validation.annotation.Validated,在参数上添加@jakarta.validation.constraints.NotNull。如果参数不简单,还可以添加@Valid

相关问题