【问题标题】:How to implement a search in a resolver using [nestjs/graphql]如何使用 [nestjs/graphql] 在解析器中实现搜索
【发布时间】:2020-05-22 23:32:30
【问题描述】:

问题 朋友们好,

我正在尝试执行搜索,但显然执行不正确。 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.    

【问题讨论】:

    标签: typescript graphql nestjs


    【解决方案1】:

    解决方案

    我忘记在查询中定义它

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

    【讨论】:

      猜你喜欢
      • 2019-05-27
      • 2020-12-30
      • 2020-03-21
      • 2021-07-27
      • 2020-08-10
      • 2019-12-12
      • 2019-04-06
      • 2022-08-22
      • 1970-01-01
      相关资源
      最近更新 更多