nestjs / graphql

GraphQL (TypeScript) module for Nest framework (node.js) 🍷
https://docs.nestjs.com/graphql/quick-start
MIT License
1.46k stars 397 forks source link

How to send partial data with error object using @Context #3174

Closed brgv-code closed 1 month ago

brgv-code commented 9 months ago

Is there an existing issue for this?

Current behavior

(Trying to get help from discord for over a week with no luck, hence posting it here)

I'm integrating two data sources in a search component; need to handle API failures by communicating errors to the frontend without crashing the app (using throw new Error crashes the app). Tried using interceptors and context for error handling without success; looking for advice.

Project has @nestjs/graphql for server-side and Apollo Client for client-side.

Data entity comprises of a union of two types, say foo and bar.


export const SearchResultsUnion = createUnionType({
  name: "SearchResultsUnion",
  types: () => [foo, bar] as const
})

Resolver:

@Query(() => [SearchResultsUnion], { name: "search" })
  @CommonResource()
  search(@Args() { queryStr, onlyDB }: SearchArgs) {
    return this.searchService.search({ queryStr })
  }

search.service.ts:

async search(searchArgs: SearchArgs): Promise<Array<typeof SearchResultsUnion>> {
//rest of the code
try {
autocompleteResult = await axios.get(XYZ,
          {
            params: { query: queryStr },
          }
        )
      } catch (err) {
        //do something here to access the error object
      }}
return results
   }
}

app.module.ts

@Module({
  imports: [
    SearchModule
  ],
  providers: [ ],
  controllers: [AppController, ServicesController]
}) 

Minimum reproduction code

No url, but please refer to the module and service in the description

Steps to reproduce

No response

Expected behavior

I should be able to access the error object in @Context and be able to set custom error message, along with sending a partial data. I read through the docs and tried several approaches in vain.

Package version

12.0.11

Graphql version

graphql: 16.8.1 @nestjs/platform-express: 10.3.0 apollo-server-express:
apollo-server-fastify:

NestJS version

10.3.0

Node.js version

18.7.1

In which operating systems have you tested?

Other

No response

kamilmysliwiec commented 1 month ago

Thank you for taking the time to submit your report! From the looks of it, this could be better discussed on our Discord. If you haven't already, please join here and send a new post in the #⁠ 🐈 nestjs-help forum. Make sure to include a link to this issue, so you don't need to write it all again. We have a large community of helpful members, who will assist you in getting this to work.