Skip to content

Commit 8f92b7c

Browse files
committed
Updated federation and fixed linting
1 parent fdfd53f commit 8f92b7c

File tree

18 files changed

+132
-111
lines changed

18 files changed

+132
-111
lines changed
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
const { ApolloServer } = require('apollo-server');
2-
const { ApolloGateway } = require('@apollo/gateway');
1+
import { ApolloServer } from 'apollo-server';
2+
import { ApolloGateway } from '@apollo/gateway';
33

4-
5-
const run = async function() {
4+
const run = async function(): Promise<void> {
65
const gateway = new ApolloGateway({
76
serviceList: [
87
{ name: 'property', url: 'http://localhost:4001' },
@@ -11,12 +10,11 @@ const run = async function() {
1110
});
1211

1312
const server = new ApolloServer({
14-
gateway,
15-
subscriptions: false
13+
gateway
1614
});
1715

1816
const { url } = await server.listen({port: 4000});
1917
console.log(`🚀 Gateway ready at ${url}`);
2018
}
2119

22-
module.exports = { run };
20+
export { run };
Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
import { ApolloServer } from 'apollo-server';
2+
import { ApolloGateway } from '@apollo/gateway';
13

2-
const { ApolloServer } = require('apollo-server');
3-
const { ApolloGateway } = require('@apollo/gateway');
4-
5-
const startGateway = async () => {
4+
const startGateway = async (): Promise<void> => {
65
const gateway = new ApolloGateway({
76
serviceList: [
87
{ name: 'property', url: 'http://localhost:4001' },
@@ -11,13 +10,11 @@ const startGateway = async () => {
1110
});
1211

1312
const server = new ApolloServer({
14-
gateway,
15-
subscriptions: false
13+
gateway
1614
});
1715

1816
const { url } = await server.listen({port: 4000});
1917
console.log(`🚀 Gateway ready at ${url}`);
2018
}
2119

22-
module.exports = startGateway;
23-
20+
export default startGateway;

examples/federation/property-service/datasource.js

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
import { ComponentContext, DataSourceDefinition } from "../../../src";
4+
5+
interface Property {
6+
id: number;
7+
geo: string[];
8+
}
9+
10+
const propertiesDB: Record<number, Property> = {
11+
1: { id: 1, geo: ['41.40338', '2.17403']},
12+
2: { id: 2, geo: ['111.1111', '222.2222']}
13+
}
14+
15+
class PropertyDataSource implements DataSourceDefinition<PropertyDataSource> {
16+
name = 'PropertyDataSource';
17+
18+
getPropertyById(context: ComponentContext, id: string): Property | undefined {
19+
return propertiesDB[id];
20+
}
21+
}
22+
23+
export default PropertyDataSource;
Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
'use strict';
22

3-
const { ApolloServer } = require('apollo-server');
4-
const GraphQLComponent = require('../../../dist').default;
5-
const PropertyDataSource = require('./datasource');
6-
const resolvers = require('./resolvers');
7-
const types = require('./types');
3+
import { ApolloServer } from 'apollo-server';
4+
import GraphQLComponent from '../../../dist';
5+
import PropertyDataSource from './datasource';
6+
import resolvers from './resolvers';
7+
import types from './types';
8+
9+
interface PropertyComponentOptions {
10+
[key: string]: any;
11+
}
812

913
class PropertyComponent extends GraphQLComponent {
10-
constructor(options) {
14+
constructor(options: PropertyComponentOptions) {
1115
super(options);
1216
}
1317
}
1418

15-
const run = async function () {
19+
const run = async function (): Promise<void> {
1620
const { schema, context } = new PropertyComponent({
1721
types,
1822
resolvers,
@@ -22,12 +26,11 @@ const run = async function () {
2226

2327
const server = new ApolloServer({
2428
schema,
25-
context,
26-
subscriptions: false,
29+
context
2730
});
2831

2932
const { url } = await server.listen({port: 4001})
3033
console.log(`🚀 Property service ready at ${url}`)
3134
}
3235

33-
module.exports = { run };
36+
export { run };

examples/federation/property-service/resolvers.js

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
import { ComponentContext } from "../../../src";
4+
5+
const resolvers = {
6+
Query: {
7+
property(_: any, { id }: { id: string }, { dataSources }: ComponentContext) {
8+
return dataSources.PropertyDataSource.getPropertyById(id);
9+
}
10+
},
11+
Property: {
12+
__resolveReference(ref: { id: string }, { dataSources }: ComponentContext) {
13+
return dataSources.PropertyDataSource.getPropertyById(ref.id);
14+
}
15+
}
16+
};
17+
18+
export default resolvers;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const fs = require('fs');
4-
const path = require('path');
3+
import * as fs from 'fs';
4+
import * as path from 'path';
55

66
const types = fs.readFileSync(path.resolve(path.join(__dirname, 'schema.graphql')), 'utf-8');
77

8-
module.exports = types;
8+
export default types;
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
'use strict';
22

3+
import { ComponentContext, DataSourceDefinition } from "../../../src";
4+
5+
interface Review {
6+
id: string;
7+
content: string;
8+
}
9+
310
// reviews indexed by property id
4-
const reviewsDB = {
11+
const reviewsDB: Record<number, Review[]> = {
512
1: [ { id: 'rev-id-1-a', content: 'this property was great'}, { id: 'rev-id-1-b', content: 'this property was terrible'}],
613
2: [ { id: 'rev-id-2-a', content: 'This property was amazing for our extended family'}, { id: 'rev-id-2-b', content: 'I loved the proximity to the beach'}, { id: 'rev-id-2-c', content: 'The bed was not comfortable at all'}]
714
}
815

9-
class ReviewsDataSource {
10-
getReviewsByPropertyId(context, propertyId) {
16+
class ReviewsDataSource implements DataSourceDefinition<ReviewsDataSource> {
17+
getReviewsByPropertyId(context: ComponentContext, propertyId: string): Review[] | undefined {
1118
return reviewsDB[propertyId];
1219
}
1320
}
1421

15-
module.exports = ReviewsDataSource;
22+
export default ReviewsDataSource;
Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
'use strict';
22

3-
const { ApolloServer } = require('apollo-server');
4-
const GraphQLComponent = require('../../../dist').default;
5-
const ReviewsDataSource = require('./datasource');
6-
const resolvers = require('./resolvers');
7-
const types = require('./types');
8-
const toUppercaseDirective = require('./toUppercaseDirective')
3+
import { ApolloServer } from 'apollo-server';
4+
import GraphQLComponent from '../../../dist';
5+
import ReviewsDataSource from './datasource';
6+
import resolvers from './resolvers';
7+
import types from './types';
8+
import toUppercaseDirective from './toUppercaseDirective';
9+
10+
interface ReviewsComponentOptions {
11+
[key: string]: any;
12+
}
913

1014
class ReviewsComponent extends GraphQLComponent {
11-
constructor(options) {
15+
constructor(options: ReviewsComponentOptions) {
1216
super(options);
1317
}
1418
}
1519

16-
const run = async function () {
20+
const run = async function (): Promise<void> {
1721
const { schema, context } = new ReviewsComponent({
1822
types,
1923
resolvers,
@@ -26,13 +30,11 @@ const run = async function () {
2630

2731
const server = new ApolloServer({
2832
schema,
29-
context,
30-
subscriptions: false
33+
context
3134
});
3235

3336
const { url } = await server.listen({port: 4002})
3437
console.log(`🚀 Reviews service ready at ${url}`)
35-
3638
}
3739

38-
module.exports = { run };
40+
export { run };

examples/federation/reviews-service/resolvers.js

Lines changed: 0 additions & 16 deletions
This file was deleted.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
import { ComponentContext } from "../../../src";
4+
5+
const resolvers = {
6+
Query: {
7+
reviewsByPropertyId(_: any, { propertyId }: { propertyId: string }, { dataSources }: ComponentContext) {
8+
return dataSources.ReviewsDataSource.getReviewsByPropertyId(propertyId);
9+
}
10+
},
11+
Property: {
12+
reviews(root: { id: string }, _args: any, { dataSources }: ComponentContext) {
13+
return dataSources.ReviewsDataSource.getReviewsByPropertyId(root.id);
14+
}
15+
}
16+
};
17+
18+
export default resolvers;
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
const {getDirective, MapperKind, mapSchema} = require("@graphql-tools/utils");
2-
const {defaultFieldResolver} = require("graphql");
1+
import { getDirective, MapperKind, mapSchema } from "@graphql-tools/utils";
2+
import { defaultFieldResolver, GraphQLSchema } from "graphql";
33

4-
function toUppercaseDirective(directiveName) {
5-
return (schema) => mapSchema(schema, {
4+
function toUppercaseDirective(directiveName: string) {
5+
return (schema: GraphQLSchema) => mapSchema(schema, {
66
[MapperKind.OBJECT_FIELD]: (fieldConfig) => {
77
const upperDirective = getDirective(schema, fieldConfig, directiveName)?.[0];
88
if (upperDirective) {
99
const {resolve = defaultFieldResolver} = fieldConfig;
1010
return {
1111
...fieldConfig,
12-
resolve: async function (source, args, context, info) {
12+
resolve: async function (source: any, args: any, context: any, info: any) {
1313
const result = await resolve(source, args, context, info);
1414
if (typeof result === 'string') {
1515
return result.toUpperCase();
@@ -22,4 +22,4 @@ function toUppercaseDirective(directiveName) {
2222
})
2323
}
2424

25-
module.exports = toUppercaseDirective
25+
export default toUppercaseDirective;
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
const fs = require('fs');
4-
const path = require('path');
3+
import * as fs from 'fs';
4+
import * as path from 'path';
55

66
const types = fs.readFileSync(path.resolve(path.join(__dirname, 'schema.graphql')), 'utf-8');
77

8-
module.exports = types;
8+
export default types;

examples/federation/run-federation-example.js

Lines changed: 0 additions & 11 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { run as runReviewsService } from './reviews-service';
2+
import { run as runPropertyService } from './property-service';
3+
import { run as runGateway } from './gateway';
4+
5+
const start = async (): Promise<void> => {
6+
await runReviewsService();
7+
await runPropertyService();
8+
await runGateway();
9+
}
10+
11+
start();

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"prepublish": "npm run build",
1616
"test": "tape -r ts-node/register \"test/**/*.ts\"",
1717
"start-composition": "DEBUG=graphql-component ts-node examples/composition/server/index.ts",
18-
"start-federation": "DEBUG=graphql-component node examples/federation/run-federation-example.js",
18+
"start-federation": "DEBUG=graphql-component ts-node examples/federation/run-federation-example.ts",
1919
"lint": "npx eslint src/index.ts",
2020
"cover": "nyc npm test",
2121
"update-deps": "ncu -u && npm install",

0 commit comments

Comments
 (0)