Как реализовать поиск в резольвере, используя [nestjs / graphql] - PullRequest
0 голосов
/ 06 февраля 2020

Проблема Здравствуйте, друзья,

Я пытаюсь реализовать решение для поиска, но, видимо, реализация не верна. resolver search должен получить запрос в качестве параметра, затем функция useSearch вернет данные. Но это показывает мне ошибку, которую вы можете увидеть внизу.

Если кто-то может помочь мне решить проблему, я буду признателен.

Крючки


export async function useSearch(query: String){
  const data = await axios.get(`${BASE_URL}/Search/${query}`)
    .then(res =>{
      return res.data.search;
    }).catch(err =>{
      console.log(err)
    });
  return data;
};

Решатели

import { Query , Resolver, Args}  from '@nestjs/graphql';
import { 
  useLatestAnime,
  useSearch
 } from './hooks/index';


@Resolver()
export class AnimesResolver{
  latestAnime = useLatestAnime()
    .then(res =>{
      return res;
    });

  search = (query: String) =>{
    const data = useSearch(query)
      .then(res =>{
        return res;
      });
    return data;
  }


  @Query('latestAnime')
  getLatestAnime(){
    const data = this.latestAnime.then(res =>{
      return res;
    })
    return data;
  }

  @Query('search')
  getSearch(@Args('query') query: String){
    const res = this.search(query)
      .then(res =>{
        return res;
      })
    return res;
  }
}

Ошибка

(node:11940) UnhandledPromiseRejectionWarning: Error: Query.search defined in resolvers, but not in schema
(node:11940) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not 
handled with .catch(). (rejection id: 2)
(node:11940) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.    

1 Ответ

1 голос
/ 07 февраля 2020

Решение

Я забыл определить его в запросе

type Query{
  latestAnime: [Animes]
  search(query: String): [Animes]
}
...