Spring Boot + GraphQL-接受对象的突变列表

时间:2018-12-04 19:27:20

标签: java spring-boot graphql apollo

我正在研究一个示例应用程序,以通过Spring Boot更好地理解GraphQL。

我正在尝试编写一个createHuman方法,该方法接受朋友作为可选输入参数。

该模式取自示例代码:

type Query {
# If episode omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode
    hero(episode: Episode): Character
    # Find human by id
    human(id: String!): Human
    # Find droid by id
    droid(id: String!): Droid
    # Find character by id
    character(id: String!): Character
    # Get all characters
    heroes: [Character!]!
}

# One of the films in the Star Wars Trilogy
enum Episode {
    # Released in 1977
    NEWHOPE,
    # Released in 1980
    EMPIRE,
    # Released in 1983
    JEDI
   }

   # A character in the Star Wars Trilogy
interface Character {
    # The id of the character
    id: String!
    # The name of the character
    name: String
    # The friends of the character, or an empty list if they have none
    friends: [Character]
    # Which movies they appear in
    appearsIn: [Episode]
}

# A humanoid creature in the Star Wars universe
type Human implements Character {
    # The id of the human
    id: String!
    # The name of the human
    name: String
    # The friends of the human, or an empty list if they have none
    friends: [Character]
    # Which movies they appear in
    appearsIn: [Episode]
    # The home planet of the human, or null if unknown
    homePlanet: String
}

# A mechanical creature in the Star Wars universe
type Droid implements Character {
    # The id of the droid
    id: String!
    # The name of the droid
    name: String
    # The friends of the droid, or an empty list if they have none
    friends: [Character]
    # Which movies they appear in
    appearsIn: [Episode]
    # The primary function of the droid
    primaryFunction: String
}

type Mutation {
    # Creates a new human character
    createHuman(input: CreateHumanInput!): Human
}

input FriendInput {
    id: String!
}

input CreateHumanInput {
    # The name of the human
    name: String!
    # The home planet of the human, or null if unknown
    homePlanet: String!
    # friends of the human
    friends: [FriendInput]
}

我修改了该突变以接受FriendInput列表,该列表应该是其他字符的ID,但是我不知道在Spring Boot端需要做些什么来更改我的方法签名?

Mutation.java

package com.test.prototype.graphql.resolvers;

import com.coxautodev.graphql.tools.GraphQLMutationResolver;
import com.londonlife.prototype.graphql.repository.CharacterRepository;
import com.londonlife.prototype.graphql.types.Character;
import com.londonlife.prototype.graphql.types.Human;
import javafx.print.Collation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.*;

@Component
public class Mutation implements GraphQLMutationResolver {

    private CharacterRepository characterRepository;

    public Mutation(@Autowired CharacterRepository characterRepository) {
        this.characterRepository = characterRepository;
    }

// how do I change this method signature so that my list of friend ids is supported?
    public Human createHuman(Map<String,String> createHumanInput) {
        String name = null;
        if (createHumanInput.containsKey("name")) {
            name = createHumanInput.get("name");
        }
        String homePlanet = "Jakku";
        if (createHumanInput.containsKey("homePlanet")) {
            homePlanet = createHumanInput.get("homePlanet");
        }
        List<Character> friends = new ArrayList<>();
        if (createHumanInput.containsKey("friends")) {

        }
        final Human human = new Human(UUID.randomUUID().toString(), name, null, homePlanet);
        this.characterRepository.saveHuman(human);
        return human;
    }

}

此外,Apollo客户端是否支持“连接突变”?我在文档或在线上找不到任何内容。

0 个答案:

没有答案