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
Apollo Server 2.0 #32
Comments
The apollo@2.0 is not stable yet. We'll support it once it hits the final release. |
v2 is now stable, any advice on how to use it? |
Yeah, I'll move to nest js until it support Apollo 2 |
I'm using this nestjs module with Apollo 2 in the following way: app.module.js import { Module } from '@nestjs/common';
import { GraphQLFactory, GraphQLModule } from '@nestjs/graphql';
import { ApolloServer } from 'apollo-server-express';
import { LostPetsResolver } from 'lost-pets/lost-pets.resolvers';
import { LostPetsService } from './lost-pets/lost-pets.service';
@Module({
imports: [GraphQLModule],
providers: [LostPetsService, LostPetsResolver], // Provide services and resolvers
})
export class AppModule {
// Inject graphQLFactory as in Nestjs Docs
constructor(private readonly graphQLFactory: GraphQLFactory) { }
configureGraphQL(app: any) {
// Same as nestjs docs - graphql guide
const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
const schema = this.graphQLFactory.createSchema({ typeDefs });
// this changed. Apollo lib internally apply app.use(...)
// and other middlewares to work
// but it needs app object
const server = new ApolloServer({ schema });
server.applyMiddleware({ app });
}
} And then I give the app object to this method: main.ts import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// With Nestjs context get the app module to call our configure method
const appModule = app.get(AppModule);
appModule.configureGraphQL(app);
await app.listen(3000);
}
bootstrap(); Thanks. |
@diego-d5000 I haven't start with Nest yet but watching on it. So, based on your setup, how to expose graphql endpoint? Is it like:
|
@viiiprock You can't apply this const server = new ApolloServer({ schema });
const path = '/api/graphql';
server.applyMiddleware({ app, path }); |
great, I should make a try. Thanks @diego-d5000 |
@diego-d5000 do you have an issue with additional headers being sent when loading the graphql playground? I am seeing |
@kyle-mccarthy I do, seems it came from playground, but you could try
And it will do the trick. |
Nice. Thanks @viiiprock, It seems that it's a graphql-playground issue: |
+1 |
@diego-d5000 wrote up how to do it, works like harm or just pull request to nest graphql module new version :) |
How about graphql-subscriptions. |
@arjitkhullar Here is an example including subscription handling: import { Module } from '@nestjs/common';
import { GraphQLFactory, GraphQLModule } from '@nestjs/graphql';
import { ApolloServer } from 'apollo-server-express';
import { LostPetsResolver } from 'lost-pets/lost-pets.resolvers';
import { LostPetsService } from './lost-pets/lost-pets.service';
@Module({
imports: [GraphQLModule],
providers: [LostPetsService, LostPetsResolver], // Provide services and resolvers
})
export class AppModule {
// Inject graphQLFactory as in Nestjs Docs
constructor(private readonly graphQLFactory: GraphQLFactory) { }
configureGraphQL(app: any, httpServer: any) {
// Same as nestjs docs - graphql guide
const typeDefs = this.graphQLFactory.mergeTypesByPaths('./**/*.graphql');
const schema = this.graphQLFactory.createSchema({ typeDefs });
// this changed. Apollo lib internally apply app.use(...)
// and other middlewares to work
// but it needs app object
const server = new ApolloServer({ schema });
server.applyMiddleware({ app });
server.installSubscriptionHandlers(httpServer);
}
} import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
// With Nestjs context get the app module to call our configure method
const appModule = app.get(AppModule);
const httpServer = app.getHttpServer();
appModule.configureGraphQL(app, httpServer);
await app.listen(3000);
}
bootstrap(); |
Unfortunately if you use nestjs/passport it can no extract headers from the execution context. Ending up an internal error |
@kamilmysliwiec v2 is now stable. |
@cschroeter I encountered with the same issue when I tried to use @nestjs/passport. Just for test I implemented simple guard:
and console.log output is |
Apollo v2 integration is now available. Also, the docs + example have been updated as well https://docs.nestjs.com/graphql/quick-start |
I have the same issue, any solutions for this? |
@sofyanhadia just look at the docs https://docs.nestjs.com/graphql/tooling "Execution Context" |
@sofyanhadia #48 |
Using the |
@cschroeter @Injectable()
export class AuthGuard implements CanActivate {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const ctx = GqlExecutionContext.create(context);
// you can get context by using `ctx.getContext()`
return ctx && true;
}
} before update, I did something quite tricky, but it worked like a charm @Injectable()
export class AuthGuard implements CanActivate {
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
// => get the context object with `context.getArgs()[2]`;
} |
@viiiprock Sure you can access your context within the AuthGuard - no problem. But you can not access your request headers for example. You would need to extract them in the ApplicationModule from the |
got it , I don't use those stuff, I implemented jwt my self with jsowebtoken |
@cschroeter there's how I've upgraded: import * as jwt from 'jsonwebtoken';
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { GqlExecutionContext } from '@nestjs/graphql';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private readonly authService: AuthService) {}
canActivate(
context: ExecutionContext,
): boolean | Promise<boolean> | Observable<boolean> {
const ctx = GqlExecutionContext.create(context);
const request = ctx.getContext().request;
const token = request.headers.authorization
? (request.headers.authorization as string).split(' ')
: null;
if (process.env.NODE_ENV === 'development') {
return true;
}
if (token && token[1]) {
const decoded: any = jwt.verify(token[1], process.env.SECRET);
return !!this.authService.findById(decoded.id);
}
}
} import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
@Module({
imports: [
GraphQLModule.forRoot({
typePaths: ['./**/*.gql'],
debug: true,
playground: true,
tracing: true,
path: '/api/graphql',
context: ({ req, res }) => ({
request: req,
}),
rootValue: ({ req }) => ({ req }),
}),
],
})
export class GQLModule {} |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Does this package support Apollo Server 2.0 or the older version? I installed their release candidate for express (apollo-server-express@rc). graphqlExpress is no longer available.
import { graphqlExpress } from 'apollo-server-express';
How would I go about using nestjs/graphql with Apollo Server 2.0?thank you
The text was updated successfully, but these errors were encountered: