prismake / typegql

Create GraphQL schema with TypeScript classes.
https://prismake.github.io/typegql/
MIT License
423 stars 21 forks source link

Can I somehow pass context directly to resolver? #64

Closed blitss closed 5 years ago

blitss commented 5 years ago

As well as I know, I can only get the context in @Before decorator. How can I do something like this?

@Query({ type: [Provider] })
  async providers({ context }): Promise<Provider[]> {

It might be useful to specify things that belong to the authenticated user (in the database query, where clause) or get user itself, like me{}.

pie6k commented 5 years ago

Sure, checkout @Context argument decorator:

https://prismake.github.io/typegql/explore/inject/

If you need more fine control, use raw @Inject decorator, also described in link above.

import { ObjectType, Field, Context } from 'typegql';

@ObjectType()
class Viewer {
  @Field({ type: () => Person })
  me(@Context context) {
    return db.findUserById(context.currentUser.id);
  }
  @Field() id: number;
}