Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 of 4 tasks
brgv-code opened this issue Mar 3, 2024 · 0 comments
Open
2 of 4 tasks

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

brgv-code opened this issue Mar 3, 2024 · 0 comments

Comments

@brgv-code
Copy link

Is there an existing issue for this?

  • I have searched the existing issues

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?

  • macOS
  • Windows
  • Linux

Other

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant