GraphQL Context

김종민·2022년 6월 28일
0

Nuber-Server

목록 보기
11/34


들어가기
req에 token을 담아서 user를 확인해주는
middleware를 만들었다.
이 과정을 graphql에서도 공유할 수 있게 해 준다.
context에 어떤 property를 담아주든지 간에,
resolver에서 사용할수 있게 해 준다.

https://github.com/apollographql/apollo-server

1. app.module.ts

    GraphQLModule.forRoot<ApolloDriverConfig>({
      driver: ApolloDriver,
      autoSchemaFile: true,
      context: ({ req }) => ({ user: req['user'] }),
      ///context에 req.header에 담아준 user를 context에 담아줌.
      ///이제 모든 resolver에서 user 사용 및 확인 가능
    }),

확인 방법. ***.resolver.ts에서
@Query(()=>User)
me(@Context context){
console.log(context)
}
하면 context에 req.user의 정보가 담긴것을 확인 가능하다.
및 resolver에서 context에 접근 가능하다는 것을 알수 있다.
if(!context.user){
return false}
같은 로직을 사용가능해짐.

profile
코딩하는초딩쌤

0개의 댓글