Graphql: [Request] Implement ArgumentHost.getType() === graphql

Created on 26 Mar 2020  路  7Comments  路  Source: nestjs/graphql

I'm submitting a...


[ ] Regression 
[ ] Bug report
[x] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior


Currently, it is not possible to detect in the ArgumentHost, whether a request is a graphql or normal http request. Recently, this feature was implemented in the ExecutionContext ( see https://github.com/nestjs/graphql/issues/473 ), however, it is not present in the ArgumentHost.

ArgumentHost.getType() returns http for GraphQL Requests.

Expected behavior


I would expect ArgumentHost.getType() to return graphql for GraphQL requests.

Minimal reproduction of the problem with instructions


https://github.com/johannesschobel/nest-gql-httptype

See the instructions here:
https://github.com/johannesschobel/nest-gql-httptype/blob/master/ISSUE.md

What is the motivation / use case for changing the behavior?

In my ExecptionFilter i need to check, whether the request was a regular http or graphql request. This is, at the moment, not easy possible. Currently, I need to manually check the length of the arguments as described here (https://github.com/nestjs/docs.nestjs.com/issues/673)

Environment


Nest version: 7.0.x
with latest nest/graphql package

Most helpful comment

I'm sorry! It should be:

if (host.getType<GqlContextType>() === 'graphql') {}

Would you like to create a PR to update the docs? :)

All 7 comments

I would like to further link to this issue comment here ( https://github.com/nestjs/nest/issues/1581#issuecomment-494526501 ), where @ricardovf asked:

Is there a reason why ExecutionContext is not available on Exception Filters?

Basically, that is - amongst others - also the issue i am struggling with. It is not possible (?) to inject the ExecutionContext into the ExceptionFilter. Otherwise it would be possible to check the latter.

Note, that other features (like Interceptors or Guards) use the ExecutionContext and not the ArgumentHost

Fixed in the @nestjs/[email protected]

awesome support - thank you so much kamil

Dear @kamilmysliwiec ,

i am sorry to bother you again with this issue.
I refactored the code to this:

if (host.getType() === 'graphql') {
  // do something
}

However, the IDE marks this if statement with an error:

This condition will always return 'false' since the types 'ContextType' and '"graphql"' have no overlap.

When executing the code, however, it properly works. Am i missing something here?!
All the best and thanks for your help!

I'm sorry! It should be:

if (host.getType<GqlContextType>() === 'graphql') {}

Would you like to create a PR to update the docs? :)

GqlContextType is exported from the @nestjs/graphql package

I will make a PR tomorrow! Thanks for clarification!

Was this page helpful?
0 / 5 - 0 ratings