繁体   English   中英

如何使用 [nestjs/graphql] 在解析器中实现搜索

[英]How to implement a search in a resolver using [nestjs/graphql]

问题朋友们好,

我正在尝试实现进行搜索的决心,但显然实现不正确。 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.    

解决方案

我忘了在查询中定义它

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM