Skip to content

Commit

Permalink
fix: type errors with my-server-api now using correct types and added…
Browse files Browse the repository at this point in the history
… decorator to unwrap jwt for user query to do easier ssr
  • Loading branch information
beauwilliams committed Mar 9, 2023
1 parent 86fa329 commit fc624ef
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
32 changes: 32 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,38 @@ benchmark-backend:
- List the files and folders to ignore i.e
- Add files here to ignore them from prettier formatting `*.gql.gen.ts`
# DEV NOTES AND FAQ
- ERROR
`tried: '/Users/admin/Git_Downloads/AfricaRare/Africarare-Monorepo/node_modules/bcrypt/lib/binding/napi-v3/bcrypt_lib.node' (mach-o file, but is an incompatible architecture (have 'x86_64', need 'arm64e'))`
- FIX:
Use node 18
- ERROR:
```
Failed to load schema from dist/apps/africarare-backend/autogenerated-schema.gql:
Unable to find any GraphQL type definitions for the following pointers:
- dist/apps/africarare-backend/autogenerated-schema.gql
```
- FIX:
Ensure the database is in a working state. If the app does not start, running the gen won't work either. Check db-migrations.
- ERROR:
Ran `npm run gen:gql` with some new queries added in `apps/my-frontend/api/**.gql.ts` but the `apps/my-frontend/api/**.gql.gen.ts` is not updating
- FIX:
Check the syntax of your gql queries in `apps/my-frontend/api/**.gql.ts` is correct and perhaps consider trying them on postman to confirm they work
## Application Architecture
### Workspace
Expand Down
7 changes: 7 additions & 0 deletions apps/my-backend/src/app/resources/user/user.decorator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';

export const CurrentUser = createParamDecorator((data: unknown, context: ExecutionContext) => {
const ctx = GqlExecutionContext.create(context);
return ctx.getContext().req.user;
});
23 changes: 23 additions & 0 deletions apps/my-backend/src/app/resources/user/user.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { GqlExecutionContext } from '@nestjs/graphql';
import { HttpException, HttpStatus, Logger } from '@nestjs/common';
import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';

@Injectable()
export class OnlySameUserByIdAllowed implements NestInterceptor {
async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> {
const context_ = GqlExecutionContext.create(context);
const { req } = context_.getContext();
const requestedFromUserId = req.body?.variables?.args?.id;
const requestedForUserId = req?.user?.id;
try {
if (requestedFromUserId === requestedForUserId) {
return next.handle();
} else {
throw new Error('Unauthorized');
}
} catch {
throw new HttpException('Unauthorized', HttpStatus.UNAUTHORIZED);
}
}
}
6 changes: 3 additions & 3 deletions apps/my-frontend/api/my-server-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createClient, fetchExchange, ssrExchange, TypedDocumentNode } from 'urql';
import { AnyVariables, createClient, fetchExchange, ssrExchange, TypedDocumentNode } from 'urql';
import { GetServerSidePropsContext, GetServerSidePropsResult } from 'next';
import { DocumentNode } from 'graphql';
import { SSRData } from '@urql/core/dist/types/exchanges/ssr';
Expand All @@ -20,13 +20,13 @@ export async function serverQuery<
const cookie = context.req.headers.cookie;
const serverClient = createClient({
url: `http://${process.env.API_HOST}:3333/graphql`,
fetchOptions: { headers: { cookie } },
fetchOptions: { headers: { cookie } as HeadersInit },
exchanges: [ssrCache, fetchExchange],
});

try {
const { error } = await serverClient
.query<SsrQuery<QueryResult, Variables>, Variables>(query, variables)
.query<SsrQuery<QueryResult, Variables>, AnyVariables>(query, variables as AnyVariables)
.toPromise();

if (!error) return { props: { urqlState: ssrCache.extractData() } };
Expand Down

0 comments on commit fc624ef

Please sign in to comment.