Skip to content

Commit

Permalink
remove dependency on apollo-graphql and port over functions (#1364)
Browse files Browse the repository at this point in the history
* remove dependencies on `apollo-graphql` and port over functions as required
  • Loading branch information
clenfest committed Jan 10, 2022
1 parent 85bf5bb commit b15459f
Show file tree
Hide file tree
Showing 26 changed files with 690 additions and 32 deletions.
1 change: 0 additions & 1 deletion federation-integration-testsuite-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
},
"homepage": "https://github.com/apollographql/federation#readme",
"dependencies": {
"apollo-graphql": "^0.9.5",
"graphql-tag": "^2.10.4",
"pretty-format": "^27.0.0"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gql from 'graphql-tag';
import { GraphQLResolverMap } from 'apollo-graphql';
import { GraphQLResolverMap } from '../resolverMap';

export const name = 'accounts';
export const url = `https://${name}.api.com.invalid`;
Expand Down
2 changes: 1 addition & 1 deletion federation-integration-testsuite-js/src/fixtures/books.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gql from 'graphql-tag';
import { GraphQLResolverMap } from 'apollo-graphql';
import { GraphQLResolverMap } from '../resolverMap';

export const name = 'books';
export const url = `https://${name}.api.com.invalid`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gql from 'graphql-tag';
import { GraphQLResolverMap } from 'apollo-graphql';
import { GraphQLResolverMap } from '../resolverMap';

export const name = 'inventory';
export const url = `https://${name}.api.com.invalid`;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import gql from 'graphql-tag';
import { GraphQLResolverMap } from 'apollo-graphql';
import { GraphQLResolverMap } from '../resolverMap';

export const name = 'product';
export const url = `https://${name}.api.com.invalid`;
Expand Down
4 changes: 2 additions & 2 deletions federation-integration-testsuite-js/src/fixtures/reviews.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { GraphQLResolverMap } from 'apollo-graphql';
import gql from 'graphql-tag';
import { GraphQLResolverMap } from '../resolverMap';

export const name = 'reviews';
export const url = `https://${name}.api.com.invalid`;
Expand Down Expand Up @@ -153,7 +153,7 @@ const reviews = [
},
];

export const resolvers: GraphQLResolverMap<any> = {
export const resolvers: GraphQLResolverMap<unknown> = {
Query: {
review(_, args) {
return { id: args.id };
Expand Down
23 changes: 23 additions & 0 deletions federation-integration-testsuite-js/src/resolverMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { GraphQLFieldResolver, GraphQLScalarType, DocumentNode } from 'graphql';

export interface GraphQLSchemaModule {
typeDefs: DocumentNode;
resolvers?: GraphQLResolverMap<any>;
}

// eslint-disable-next-line @typescript-eslint/ban-types
export interface GraphQLResolverMap<TContext = {}> {
[typeName: string]:
| {
[fieldName: string]:
| GraphQLFieldResolver<any, TContext>
| {
requires?: string;
resolve: GraphQLFieldResolver<any, TContext>;
};
}
| GraphQLScalarType
| {
[enumValue: string]: string | number;
};
}
1 change: 0 additions & 1 deletion gateway-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"@apollo/query-planner": "file:../query-planner-js",
"@opentelemetry/api": "^1.0.1",
"@types/node-fetch": "2.5.12",
"apollo-graphql": "^0.9.5",
"apollo-reporting-protobuf": "^0.8.0 || ^3.0.0",
"apollo-server-caching": "^0.7.0 || ^3.0.0",
"apollo-server-core": "^2.23.0 || ^3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion gateway-js/src/__tests__/executeQueryPlan.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
GraphQLObjectType,
print,
} from 'graphql';
import { addResolversToSchema, GraphQLResolverMap } from 'apollo-graphql';
import gql from 'graphql-tag';
import { GraphQLExecutionResult, GraphQLRequestContext } from 'apollo-server-types';
import { AuthenticationError } from 'apollo-server-core';
Expand All @@ -21,6 +20,7 @@ import { ApolloGateway } from '..';
import { ApolloServerBase as ApolloServer } from 'apollo-server-core';
import { getFederatedTestingSchema } from './execution-utils';
import { Schema, Operation, parseOperation, buildSchemaFromAST } from '@apollo/federation-internals';
import { addResolversToSchema, GraphQLResolverMap } from '../schema-helper';

expect.addSnapshotSerializer(astSerializer);
expect.addSnapshotSerializer(queryPlanSerializer);
Expand Down
2 changes: 1 addition & 1 deletion gateway-js/src/__tests__/execution-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
GraphQLSchemaValidationError,
GraphQLSchemaModule,
GraphQLResolverMap,
} from 'apollo-graphql';
} from '../schema-helper';
import { GraphQLRequest, GraphQLExecutionResult, Logger } from 'apollo-server-types';
import { buildSubgraphSchema } from '@apollo/subgraph';
import {
Expand Down
2 changes: 1 addition & 1 deletion gateway-js/src/__tests__/gateway/endToEnd.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { GraphQLSchemaModule } from 'apollo-graphql';
import { buildSubgraphSchema } from '@apollo/subgraph';
import { ApolloServer } from 'apollo-server';
import fetch from 'node-fetch';
import { ApolloGateway } from '../..';
import { fixtures } from 'apollo-federation-integration-testsuite';
import { ApolloServerPluginInlineTrace } from 'apollo-server-core';
import { GraphQLSchemaModule } from '../../schema-helper';

async function startFederatedServer(modules: GraphQLSchemaModule[]) {
const schema = buildSubgraphSchema(modules);
Expand Down
2 changes: 1 addition & 1 deletion gateway-js/src/__tests__/gateway/reporting.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { gunzipSync } from 'zlib';
import nock from 'nock';
import { GraphQLSchemaModule } from 'apollo-graphql';
import gql from 'graphql-tag';
import { buildSubgraphSchema } from '@apollo/subgraph';
import { ApolloServer } from 'apollo-server';
Expand All @@ -14,6 +13,7 @@ import { Plugin, Config, Refs } from 'pretty-format';
import { Report, Trace } from 'apollo-reporting-protobuf';
import { fixtures } from 'apollo-federation-integration-testsuite';
import { nockAfterEach, nockBeforeEach } from '../nockAssertions';
import { GraphQLSchemaModule } from '../../schema-helper';

// Normalize specific fields that change often (eg timestamps) to static values,
// to make snapshot testing viable. (If these helpers are more generally
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LocalGraphQLDataSource } from '../LocalGraphQLDataSource';
import { buildSubgraphSchema } from '@apollo/subgraph';
import gql from 'graphql-tag';
import { GraphQLResolverMap } from 'apollo-graphql';
import { GraphQLResolverMap } from '../../schema-helper';
import { GraphQLRequestContext } from 'apollo-server-types';
import { GraphQLDataSourceRequestKind } from '../types';

Expand Down
83 changes: 83 additions & 0 deletions gateway-js/src/schema-helper/addResolversToSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import {
GraphQLSchema,
isObjectType,
GraphQLEnumType,
isAbstractType,
isScalarType,
isEnumType,
GraphQLEnumValueConfig,
} from 'graphql';

import { GraphQLResolverMap } from './resolverMap';

export function addResolversToSchema(
schema: GraphQLSchema,
resolvers: GraphQLResolverMap<any>
) {
for (const [typeName, fieldConfigs] of Object.entries(resolvers)) {
const type = schema.getType(typeName);

if (isAbstractType(type)) {
for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
}
}
}

if (isScalarType(type)) {
for (const fn in fieldConfigs) {
(type as any)[fn] = (fieldConfigs as any)[fn];
}
}

if (isEnumType(type)) {
const values = type.getValues();
const newValues: { [key: string]: GraphQLEnumValueConfig } = {};
values.forEach(value => {
let newValue = (fieldConfigs as any)[value.name];
if (newValue === undefined) {
newValue = value.name;
}

newValues[value.name] = {
value: newValue,
deprecationReason: value.deprecationReason,
description: value.description,
astNode: value.astNode,
extensions: undefined
};
});

// In place updating hack to get around pulling in the full
// schema walking and immutable updating machinery from graphql-tools
Object.assign(
type,
new GraphQLEnumType({
...type.toConfig(),
values: newValues
})
);
}

if (!isObjectType(type)) continue;

const fieldMap = type.getFields();

for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
continue;
}

const field = fieldMap[fieldName];
if (!field) continue;

if (typeof fieldConfig === "function") {
field.resolve = fieldConfig;
} else {
field.resolve = fieldConfig.resolve;
}
}
}
}
11 changes: 11 additions & 0 deletions gateway-js/src/schema-helper/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { GraphQLError } from "graphql";

export class GraphQLSchemaValidationError extends Error {
constructor(public errors: ReadonlyArray<GraphQLError>) {
super();

this.name = this.constructor.name;
Error.captureStackTrace(this, this.constructor);
this.message = errors.map(error => error.message).join("\n\n");
}
}
3 changes: 3 additions & 0 deletions gateway-js/src/schema-helper/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './resolverMap';
export * from './addResolversToSchema';
export * from './error';
23 changes: 23 additions & 0 deletions gateway-js/src/schema-helper/resolverMap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { GraphQLFieldResolver, GraphQLScalarType, DocumentNode } from 'graphql';

export interface GraphQLSchemaModule {
typeDefs: DocumentNode;
resolvers?: GraphQLResolverMap<any>;
}

// eslint-disable-next-line @typescript-eslint/ban-types
export interface GraphQLResolverMap<TContext = {}> {
[typeName: string]:
| {
[fieldName: string]:
| GraphQLFieldResolver<any, TContext>
| {
requires?: string;
resolve: GraphQLFieldResolver<any, TContext>;
};
}
| GraphQLScalarType
| {
[enumValue: string]: string | number;
};
}
13 changes: 1 addition & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion query-planner-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
"dependencies": {
"@apollo/federation-internals": "file:../internals-js",
"@apollo/query-graphs": "file:../query-graphs-js",
"apollo-graphql": "^0.9.5",
"chalk": "^4.1.0",
"deep-equal": "^2.0.5",
"pretty-format": "^27.0.0"
Expand Down
1 change: 0 additions & 1 deletion subgraph-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"access": "public"
},
"dependencies": {
"apollo-graphql": "^0.9.5"
},
"peerDependencies": {
"graphql": "^15.7.0"
Expand Down
11 changes: 6 additions & 5 deletions subgraph-js/src/buildSubgraphSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import {
specifiedDirectives,
} from 'graphql';
import {
buildSchemaFromSDL,
transformSchema,
GraphQLSchemaModule,
modulesFromSDL,
addResolversToSchema,
GraphQLResolverMap,
} from 'apollo-graphql';
addResolversToSchema,
modulesFromSDL,
transformSchema,
buildSchemaFromSDL,
} from './schema-helper';

import { federationDirectives, typeIncludesDirective } from './directives';

import { serviceField, entitiesField, EntityType } from './types';
Expand Down

0 comments on commit b15459f

Please sign in to comment.