Skip to content

Commit

Permalink
Merge branch 'jmiraglia-stuzo-fix-federated-directive'
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilmysliwiec committed May 4, 2020
2 parents f056429 + 876be36 commit 1c6a898
Show file tree
Hide file tree
Showing 14 changed files with 53 additions and 29 deletions.
11 changes: 10 additions & 1 deletion lib/federation/graphql-federation.module.ts
Expand Up @@ -10,6 +10,7 @@ import { loadPackage } from '@nestjs/common/utils/load-package.util';
import { ApplicationConfig, HttpAdapterHost } from '@nestjs/core';
import { MetadataScanner } from '@nestjs/core/metadata-scanner';
import { ApolloServerBase } from 'apollo-server-core';
import { SchemaDirectiveVisitor } from 'graphql-tools';
import { GraphQLAstExplorer } from '../graphql-ast.explorer';
import { GraphQLSchemaBuilder } from '../graphql-schema.builder';
import { GraphQLSchemaHost } from '../graphql-schema.host';
Expand All @@ -31,10 +32,10 @@ import {
ScalarsExplorerService,
} from '../services';
import {
extend,
generateString,
mergeDefaults,
normalizeRoutePath,
extend,
} from '../utils';
import { GraphQLFederationFactory } from './graphql-federation.factory';

Expand Down Expand Up @@ -199,6 +200,14 @@ export class GraphQLFederationModule implements OnModuleInit {
const app = this.httpAdapterHost.httpAdapter.getInstance();
const path = this.getNormalizedPath(apolloOptions);

// If custom directives are provided merge them into schema per Apollo https://www.apollographql.com/docs/apollo-server/federation/implementing-services/#defining-custom-directives
if (apolloOptions.schemaDirectives) {
SchemaDirectiveVisitor.visitSchemaDirectives(
apolloOptions.schema,
apolloOptions.schemaDirectives,
);
}

const apolloServer = new ApolloServer(apolloOptions as any);
apolloServer.applyMiddleware({
app,
Expand Down
8 changes: 4 additions & 4 deletions tests/code-first-federation/post/post.service.ts
Expand Up @@ -4,7 +4,7 @@ import { Injectable } from '@nestjs/common';
const data = [
{
id: 1,
title: 'hello world',
title: 'HELLO WORLD',
authorId: 2,
},
{
Expand All @@ -17,18 +17,18 @@ const data = [
@Injectable()
export class PostService {
public findOne(id: number) {
const post = data.find(p => p.id === id);
const post = data.find((p) => p.id === id);
if (post) {
return new Post(post);
}
return null;
}

public all() {
return data.map(p => new Post(p));
return data.map((p) => new Post(p));
}

public forAuthor(authorId: number) {
return data.filter(p => p.authorId === authorId).map(p => new Post(p));
return data.filter((p) => p.authorId === authorId).map((p) => new Post(p));
}
}
7 changes: 2 additions & 5 deletions tests/e2e/graphql-federation-fastify.spec.ts
Expand Up @@ -14,10 +14,7 @@ describe('GraphQL federation with fastify', () => {

app = module.createNestApplication(new FastifyAdapter());
await app.init();
await app
.getHttpAdapter()
.getInstance()
.ready();
await app.getHttpAdapter().getInstance().ready();
});

it(`should return query result`, () => {
Expand All @@ -40,7 +37,7 @@ describe('GraphQL federation with fastify', () => {
getPosts: [
{
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
},
],
Expand Down
6 changes: 3 additions & 3 deletions tests/e2e/graphql-federation.spec.ts
Expand Up @@ -72,7 +72,7 @@ describe('GraphQL Federation', () => {
getPosts: [
{
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
},
Expand Down Expand Up @@ -104,7 +104,7 @@ describe('GraphQL Federation', () => {
getPosts: [
{
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
user: {
Expand Down Expand Up @@ -136,7 +136,7 @@ describe('GraphQL Federation', () => {
data: {
publishPost: {
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
publishDate: 500,
},
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/graphql-gateway-async-class.spec.ts
Expand Up @@ -57,7 +57,7 @@ describe('GraphQL Gateway async-class', () => {
getPosts: [
{
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
user: {
id: '5',
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/graphql-gateway-async-existing.spec.ts
Expand Up @@ -57,7 +57,7 @@ describe('GraphQL gateway async-existing', () => {
getPosts: [
{
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
user: {
id: '5',
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/graphql-gateway-async.spec.ts
Expand Up @@ -57,7 +57,7 @@ describe('GraphQL Gateway async', () => {
getPosts: [
{
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
user: {
id: '5',
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/graphql-gateway-buildservice.spec.ts
Expand Up @@ -69,7 +69,7 @@ describe('GraphQL Gateway buildservice', () => {
getPosts: [
{
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
user: {
id: '5',
Expand Down
9 changes: 3 additions & 6 deletions tests/e2e/graphql-gateway-fastify.spec.ts
Expand Up @@ -32,10 +32,7 @@ describe('GraphQL Gateway with fastify', () => {

gatewayApp = gatewayModule.createNestApplication(new FastifyAdapter());
await gatewayApp.init();
await gatewayApp
.getHttpAdapter()
.getInstance()
.ready();
await gatewayApp.getHttpAdapter().getInstance().ready();
});

it(`should run lookup across boundaries`, () => {
Expand All @@ -62,7 +59,7 @@ describe('GraphQL Gateway with fastify', () => {
getPosts: [
{
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
user: {
id: '5',
Expand Down Expand Up @@ -101,7 +98,7 @@ describe('GraphQL Gateway with fastify', () => {
posts: [
{
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
},
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/graphql-gateway.spec.ts
Expand Up @@ -57,7 +57,7 @@ describe('GraphQL Gateway', () => {
getPosts: [
{
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
user: {
id: '5',
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('GraphQL Gateway', () => {
posts: [
{
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body:
'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
},
Expand Down
Expand Up @@ -2,11 +2,15 @@ import { Module } from '@nestjs/common';
import { join } from 'path';
import { GraphQLFederationModule } from '../../../lib';
import { PostsModule } from './posts/posts.module';
import { UpperCaseDirective } from './posts/upper.directive';

@Module({
imports: [
GraphQLFederationModule.forRoot({
typePaths: [join(__dirname, '**/*.graphql')],
schemaDirectives: {
upper: UpperCaseDirective,
},
}),
PostsModule,
],
Expand Down
6 changes: 3 additions & 3 deletions tests/graphql-federation/posts-service/posts/posts.service.ts
Expand Up @@ -6,7 +6,7 @@ export class PostsService {
private readonly posts: Post[] = [
{
id: '1',
title: 'Hello world',
title: 'HELLO WORLD',
body: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
userId: '5',
publishDate: new Date(0),
Expand All @@ -18,11 +18,11 @@ export class PostsService {
}

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

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

async publish(id: string, publishDate: Date) {
Expand Down
@@ -1,3 +1,5 @@
directive @upper on FIELD_DEFINITION

scalar Date

type Post @key(fields: "id") {
Expand All @@ -19,4 +21,4 @@ extend type Query {

extend type Mutation {
publishPost(id: ID!, publishDate: Date!): Post
}
}
15 changes: 15 additions & 0 deletions tests/graphql-federation/posts-service/posts/upper.directive.ts
@@ -0,0 +1,15 @@
import { SchemaDirectiveVisitor } from 'apollo-server-express';
import { defaultFieldResolver, GraphQLField } from 'graphql';

export class UpperCaseDirective extends SchemaDirectiveVisitor {
visitFieldDefinition(field: GraphQLField<any, any>) {
const { resolve = defaultFieldResolver } = field;
field.resolve = async function (...args) {
const result = await resolve.apply(this, args);
if (typeof result === 'string') {
return result.toUpperCase();
}
return result;
};
}
}

0 comments on commit 1c6a898

Please sign in to comment.