Skip to content

Commit

Permalink
chore(): resolve conflicts, pull the latest
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed Jul 20, 2022
2 parents b915495 + cb6090d commit c4d1123
Show file tree
Hide file tree
Showing 29 changed files with 868 additions and 17 deletions.
6 changes: 5 additions & 1 deletion packages/apollo/package.json
Expand Up @@ -15,14 +15,18 @@
"url": "git+https://github.com/nestjs/graphql.git"
},
"scripts": {
"test:e2e": "jest --config ./tests/jest-e2e.ts --runInBand",
"test:e2e": "jest --config ./tests/jest-e2e.ts --runInBand && yarn test:e2e:fed2",
"test:e2e:fed2": "jest --config ./tests/jest-e2e-fed2.ts --runInBand",
"test:e2e:dev": "jest --config ./tests/jest-e2e.ts --runInBand --watch"
},
"bugs": {
"url": "https://github.com/nestjs/graphql/issues"
},
"devDependencies": {
"@apollo/gateway": "0.51.0",
"@apollo/gateway-v2": "npm:@apollo/gateway@2.0.5",
"@apollo/subgraph-v2": "npm:@apollo/subgraph@2.0.5",
"graphql-16": "npm:graphql@16.5.0",
"@nestjs/common": "8.4.7",
"@nestjs/core": "8.4.7",
"@nestjs/platform-express": "8.4.7",
Expand Down
@@ -0,0 +1,22 @@
import { IntrospectAndCompose } from '@apollo/gateway-v2';
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloGatewayDriver, ApolloGatewayDriverConfig } from '../../../lib';

@Module({
imports: [
GraphQLModule.forRoot<ApolloGatewayDriverConfig>({
driver: ApolloGatewayDriver,
gateway: {
debug: false,
supergraphSdl: new IntrospectAndCompose({
subgraphs: [
{ name: 'users', url: 'http://localhost:3001/graphql' },
{ name: 'posts', url: 'http://localhost:3002/graphql' },
],
}),
},
}),
],
})
export class AppModule {}
@@ -0,0 +1,24 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core';
import { ApolloDriverConfig } from '../../../lib';
import { ApolloFederationDriver } from '../../../lib/drivers';
import { PostsModule } from './posts/posts.module';
import { User } from './posts/user.entity';

@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloFederationDriver,
autoSchemaFile: {
useFed2: true,
},
buildSchemaOptions: {
orphanedTypes: [User],
},
plugins: [ApolloServerPluginInlineTraceDisabled()],
}),
PostsModule,
],
})
export class AppModule {}
@@ -0,0 +1,10 @@
import { registerEnumType } from '@nestjs/graphql';

export enum PostType {
IMAGE = 'IMAGE',
TEXT = 'TEXT',
}

registerEnumType(PostType, {
name: 'PostType',
});
@@ -0,0 +1,23 @@
import { Directive, Field, ID, ObjectType } from '@nestjs/graphql';
import { PostType } from './post-type.enum';

@ObjectType()
@Directive('@key(fields: "id")')
export class Post {
@Field(() => ID)
id: string;

@Field()
title: string;

@Field()
body: string;

userId: string;

@Field({ nullable: true })
publishDate: Date;

@Field(() => PostType, { nullable: true })
type: PostType;
}
@@ -0,0 +1,9 @@
import { Module } from '@nestjs/common';
import { PostsResolvers } from './posts.resolvers';
import { UsersResolvers } from './users.resolvers';
import { PostsService } from './posts.service';

@Module({
providers: [PostsResolvers, PostsService, UsersResolvers],
})
export class PostsModule {}
@@ -0,0 +1,42 @@
import {
Args,
ID,
Mutation,
Parent,
Query,
ResolveField,
Resolver,
} from '@nestjs/graphql';
import { PostType } from './post-type.enum';
import { Post } from './posts.entity';
import { PostsService } from './posts.service';
import { User } from './user.entity';

@Resolver(Post)
export class PostsResolvers {
constructor(private readonly postsService: PostsService) {}

@Query(() => [Post])
getPosts(
@Args('type', { nullable: true, type: () => PostType }) type: PostType,
) {
if (type) {
return this.postsService.findByType(type);
} else {
return this.postsService.findAll();
}
}

@Mutation(() => Post)
publishPost(
@Args('id', { type: () => ID }) id,
@Args('publishDate') publishDate: Date,
) {
return this.postsService.publish(id, publishDate);
}

@ResolveField('user', () => User, { nullable: true })
getUser(@Parent() post: Post) {
return { __typename: 'User', id: post.userId };
}
}
@@ -0,0 +1,39 @@
import { Injectable } from '@nestjs/common';
import { Post } from './posts.entity';
import { PostType } from './post-type.enum';

@Injectable()
export class PostsService {
private readonly posts: Post[] = [
{
id: '1',
title: 'HELLO WORLD',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
userId: '5',
publishDate: new Date(0),
type: PostType.TEXT,
},
];

findAll() {
return Promise.resolve(this.posts);
}

findById(id: string) {
return Promise.resolve(this.posts.find((p) => p.id === id));
}

findByUserId(id: string) {
return Promise.resolve(this.posts.filter((p) => p.userId === id));
}

findByType(type: PostType) {
return Promise.resolve(this.posts.filter((p) => p.type === type));
}

async publish(id: string, publishDate: Date) {
const post = await this.findById(id);
post.publishDate = publishDate;
return post;
}
}
@@ -0,0 +1,8 @@
import { Directive, Field, ID, ObjectType } from '@nestjs/graphql';

@ObjectType()
@Directive('@key(fields: "id")')
export class User {
@Field(() => ID)
id: string;
}
@@ -0,0 +1,14 @@
import { ResolveField, Resolver } from '@nestjs/graphql';
import { Post } from './posts.entity';
import { PostsService } from './posts.service';
import { User } from './user.entity';

@Resolver(User)
export class UsersResolvers {
constructor(private readonly postsService: PostsService) {}

@ResolveField('posts', () => [Post])
getPosts(reference: any) {
return this.postsService.findByUserId(reference.id);
}
}
@@ -0,0 +1,20 @@
import { Module } from '@nestjs/common';
import { GraphQLModule } from '@nestjs/graphql';
import { ApolloServerPluginInlineTraceDisabled } from 'apollo-server-core';
import { ApolloDriverConfig } from '../../../lib';
import { ApolloFederationDriver } from '../../../lib/drivers';
import { UsersModule } from './users/users.module';

@Module({
imports: [
GraphQLModule.forRoot<ApolloDriverConfig>({
driver: ApolloFederationDriver,
autoSchemaFile: {
useFed2: true,
},
plugins: [ApolloServerPluginInlineTraceDisabled()],
}),
UsersModule,
],
})
export class AppModule {}
@@ -0,0 +1,11 @@
import { Directive, Field, ID, ObjectType } from '@nestjs/graphql';

@ObjectType()
@Directive('@key(fields: "id")')
export class User {
@Field(() => ID)
id: string;

@Field()
name: string;
}
@@ -0,0 +1,8 @@
import { Module } from '@nestjs/common';
import { UsersResolvers } from './users.resolvers';
import { UsersService } from './users.service';

@Module({
providers: [UsersResolvers, UsersService],
})
export class UsersModule {}
@@ -0,0 +1,18 @@
import { Args, ID, Query, Resolver, ResolveReference } from '@nestjs/graphql';
import { User } from './users.entity';
import { UsersService } from './users.service';

@Resolver(User)
export class UsersResolvers {
constructor(private readonly usersService: UsersService) {}

@Query(() => User, { nullable: true })
getUser(@Args('id', { type: () => ID }) id: string) {
return this.usersService.findById(id);
}

@ResolveReference()
resolveReference(reference: { __typename: string; id: string }) {
return this.usersService.findById(reference.id);
}
}
@@ -0,0 +1,16 @@
import { Injectable } from '@nestjs/common';
import { User } from './users.entity';

@Injectable()
export class UsersService {
private readonly users: User[] = [
{
id: '5',
name: 'GraphQL',
},
];

findById(id: string) {
return Promise.resolve(this.users.find((p) => p.id === id));
}
}

0 comments on commit c4d1123

Please sign in to comment.