#spring-boot #graphql #mutation
Вопрос:
Я новичок в GraphQL и пытаюсь реализовать функциональность CRUD с помощью SpringBoot и базы данных H2. Функция GET работает нормально, но при попытке выполнить ДОБАВЛЕНИЕ я сталкиваюсь с проблемами. Получение ошибки:
Схема не настроена для мутаций.
graphql
schema { query: Query } type Query{ allBooks: [Book] getBookByIsn(isn: Int): Book } type Book { isn: Int title: String author: String publishedDate: String publisher: Publisher! } type Publisher { pId : Int publisherName: String address: String } type Mutation{ addPublisher(pId : Int, publisherName: String, address: String ) : Publisher }
** Обслуживание **
@Service public class BookService { @Value("classpath:books.graphql") private Resource resource; private GraphQL graphQL; @Autowired private BookRepository bookRepository; @Autowired private PublisherRepository publisherRepository; @Autowired private DataFetcherAllBooks dataFetcherAllBooks; @Autowired private DataFetcherGetBookByIsn dataFetcherGetBookByIsn; @PostConstruct private void loadSchemaAtStartUp() throws IOException{ //load the data loadData(); //schema File schemaFile = resource.getFile(); //parse the schema TypeDefinitionRegistry typeDefinitionRegistry = new SchemaParser().parse(schemaFile); //Build the schema by RuntimeWiring RuntimeWiring runtimeWiring = buildRunTimeWiring(); GraphQLSchema graphQLSchema = new SchemaGenerator().makeExecutableSchema(typeDefinitionRegistry, runtimeWiring); graphQL = GraphQL.newGraphQL(graphQLSchema).build(); } private RuntimeWiring buildRunTimeWiring() { return RuntimeWiring.newRuntimeWiring() .type("Query", typewiring -gt; typewiring .dataFetcher("allBooks", dataFetcherAllBooks) .dataFetcher("getBookByIsn", dataFetcherGetBookByIsn)) .build(); } public GraphQL getGraphQL() { return graphQL; }
This is the query used :
mutation { addPublisher(pid: 2001, publisherName: "Test Publishers", address: "New Jersey") { id } }