Spring Boot + GraphQL - мутация, принимающая список объектов - PullRequest
0 голосов
/ 04 декабря 2018

Я работаю над примером приложения, чтобы лучше понять GraphQL с Spring Boot.

Я пытаюсь написать метод 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, который должен быть идентификатором других символов, однако я не знаю, что мне нужно делать на стороне 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 'Connect Mutations'?Я не могу найти в документации или в Интернете ничего такого, что говорит об этом.

...