Skip to content
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

Add gateway version to schema extensions #1751

Merged
merged 6 commits into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions gateway-js/src/__tests__/gateway/extensions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { ApolloGateway } from '@apollo/gateway';
import resolvable from '@josephg/resolvable';
import { GraphQLSchema } from 'graphql';
import { getTestingSupergraphSdl } from '../execution-utils';
const { version } = require('../../../package.json');

describe('addExtensions', () => {
let gateway: ApolloGateway;

beforeEach(() => {
gateway = new ApolloGateway({
supergraphSdl: getTestingSupergraphSdl(),
});
});

afterEach(async () => {
await gateway.stop();
});

it('has extensions on loaded schemas', async () => {
const { schema } = await gateway.load();
expect(schema.extensions).toEqual({ apollo: { gateway: { version: version } } });
});

it('has extensions on schema updates', async () => {
const schemaChangeBlocker = resolvable<{
apiSchema: GraphQLSchema;
coreSupergraphSdl: string;
}>();

gateway.onSchemaLoadOrUpdate(schemaChangeBlocker.resolve);
gateway.load();

const { apiSchema } = await schemaChangeBlocker;
expect(apiSchema.extensions).toEqual({ apollo: { gateway: { version: version } } });
});
});
7 changes: 5 additions & 2 deletions gateway-js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
import { SpanStatusCode } from '@opentelemetry/api';
import { OpenTelemetrySpanNames, tracer } from './utilities/opentelemetry';
import { createHash } from './utilities/createHash';
import { addExtensions } from './schema-helper/addExtensions';
import {
IntrospectAndCompose,
UplinkFetcher,
Expand Down Expand Up @@ -431,6 +432,8 @@ export class ApolloGateway implements GraphQLService {
}`,
);

addExtensions(this.schema!);

return {
schema: this.schema!,
executor: this.executor,
Expand Down Expand Up @@ -640,8 +643,8 @@ export class ApolloGateway implements GraphQLService {
): void {
if (this.queryPlanStore) this.queryPlanStore.flush();
this.apiSchema = coreSchema.toAPISchema();
this.schema = wrapSchemaWithAliasResolver(
this.apiSchema.toGraphQLJSSchema(),
this.schema = addExtensions(
wrapSchemaWithAliasResolver(this.apiSchema.toGraphQLJSSchema()),
);
this.queryPlanner = new QueryPlanner(coreSchema);

Expand Down
11 changes: 11 additions & 0 deletions gateway-js/src/schema-helper/__tests__/addExtensions.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { buildSchema } from 'graphql';
import { addExtensions } from '../addExtensions';
const { version } = require('../../../package.json');

describe('addExtensions', () => {
it('adds gateway extensions to a schema', async () => {
const schema = buildSchema('type Query { hello: String }');
expect(schema.extensions).toEqual({});
expect(addExtensions(schema).extensions).toEqual({ apollo: { gateway: { version: version } } });
});
});
17 changes: 17 additions & 0 deletions gateway-js/src/schema-helper/addExtensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { GraphQLSchema } from 'graphql';
const { version } = require('../../package.json');

export function addExtensions(schema: GraphQLSchema): GraphQLSchema {
schema.extensions = {
...schema.extensions,
apollo: {
...schema.extensions.apollo,
gateway: {
...schema.extensions.apollo?.gateway,
version,
}
},
};

return schema;
}
12 changes: 12 additions & 0 deletions gateway-js/src/typings/graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ interface ApolloSubgraphExtensions<TContext> {
resolveReference?: GraphQLReferenceResolver<TContext>;
}

interface ApolloGatewayExtensions {
version?: String;
}

declare module 'graphql/type/definition' {
interface GraphQLObjectTypeExtensions<_TSource = any, _TContext = any> {
apollo?: {
Expand All @@ -29,3 +33,11 @@ declare module 'graphql/type/definition' {
}
}
}

declare module 'graphql/type/schema' {
interface GraphQLSchemaExtensions {
apollo?: {
gateway?: ApolloGatewayExtensions;
}
}
}
6 changes: 5 additions & 1 deletion gateway-js/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
"files": [
"./src/__tests__/testSetup.ts"
],
"include": ["**/__tests__/**/*", "**/__mocks__/**/*"],
"include": [
"src/typings/*",
"**/__tests__/**/*",
"**/__mocks__/**/*"
],
"references": [
{ "path": "./" },
{ "path": "../federation-integration-testsuite-js" },
Expand Down
5 changes: 4 additions & 1 deletion subgraph-js/tsconfig.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
"files": [
"./src/__tests__/testSetup.ts"
],
"include": ["**/__tests__/**/*"],
"include": [
"src/schemaExtensions.ts",
"**/__tests__/**/*"
],
"references": [
{ "path": "./" },
]
Expand Down