@apollo/gateway 0.51.0 → 0.52.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config.d.ts +2 -2
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js.map +1 -1
- package/dist/datasources/LocalGraphQLDataSource.d.ts +2 -2
- package/dist/datasources/LocalGraphQLDataSource.d.ts.map +1 -1
- package/dist/datasources/LocalGraphQLDataSource.js +0 -2
- package/dist/datasources/LocalGraphQLDataSource.js.map +1 -1
- package/dist/datasources/RemoteGraphQLDataSource.d.ts +6 -6
- package/dist/datasources/RemoteGraphQLDataSource.d.ts.map +1 -1
- package/dist/datasources/RemoteGraphQLDataSource.js +12 -18
- package/dist/datasources/RemoteGraphQLDataSource.js.map +1 -1
- package/dist/datasources/types.d.ts +5 -5
- package/dist/datasources/types.d.ts.map +1 -1
- package/dist/executeQueryPlan.d.ts +3 -3
- package/dist/executeQueryPlan.d.ts.map +1 -1
- package/dist/executeQueryPlan.js.map +1 -1
- package/dist/index.d.ts +7 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -11
- package/dist/index.js.map +1 -1
- package/dist/supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.d.ts.map +1 -1
- package/dist/supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.js.map +1 -1
- package/package.json +8 -10
- package/src/__generated__/graphqlTypes.ts +1 -1
- package/src/__tests__/executeQueryPlan.test.ts +14 -5
- package/src/__tests__/execution-utils.ts +3 -3
- package/src/__tests__/gateway/executor.test.ts +2 -2
- package/src/config.ts +2 -4
- package/src/datasources/LocalGraphQLDataSource.ts +2 -4
- package/src/datasources/RemoteGraphQLDataSource.ts +38 -42
- package/src/datasources/__tests__/LocalGraphQLDataSource.test.ts +2 -2
- package/src/datasources/__tests__/RemoteGraphQLDataSource.test.ts +14 -19
- package/src/datasources/types.ts +12 -5
- package/src/executeQueryPlan.ts +13 -17
- package/src/index.ts +21 -42
- package/src/supergraphManagers/IntrospectAndCompose/loadServicesFromRemoteEndpoint.ts +2 -2
package/src/index.ts
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
1
|
import { deprecate } from 'util';
|
|
2
|
-
import { GraphQLService, Unsubscriber } from 'apollo-server-core';
|
|
3
|
-
import {
|
|
4
|
-
GraphQLExecutionResult,
|
|
5
|
-
GraphQLRequestContextExecutionDidStart,
|
|
6
|
-
} from 'apollo-server-types';
|
|
7
2
|
import type { Logger } from '@apollo/utils.logger';
|
|
8
|
-
import
|
|
3
|
+
import LRUCache from 'lru-cache';
|
|
9
4
|
import {
|
|
10
5
|
isObjectType,
|
|
11
6
|
isIntrospectionType,
|
|
@@ -68,6 +63,7 @@ import {
|
|
|
68
63
|
LocalCompose,
|
|
69
64
|
} from './supergraphManagers';
|
|
70
65
|
import { Fetcher } from '@apollo/utils.fetcher';
|
|
66
|
+
import {GatewayInterface, GatewayUnsubscriber, GatewayGraphQLRequestContext, GatewayExecutionResult} from '@apollo/server-gateway-interface';
|
|
71
67
|
|
|
72
68
|
type DataSourceMap = {
|
|
73
69
|
[serviceName: string]: { url?: string; dataSource: GraphQLDataSource };
|
|
@@ -123,12 +119,12 @@ interface GraphQLServiceEngineConfig {
|
|
|
123
119
|
graphVariant?: string;
|
|
124
120
|
}
|
|
125
121
|
|
|
126
|
-
export class ApolloGateway implements
|
|
122
|
+
export class ApolloGateway implements GatewayInterface {
|
|
127
123
|
public schema?: GraphQLSchema;
|
|
128
124
|
private serviceMap: DataSourceMap = Object.create(null);
|
|
129
125
|
private config: GatewayConfig;
|
|
130
126
|
private logger: Logger;
|
|
131
|
-
private queryPlanStore:
|
|
127
|
+
private queryPlanStore: LRUCache<string, QueryPlan>;
|
|
132
128
|
private apolloConfig?: ApolloConfigFromAS3;
|
|
133
129
|
private onSchemaChangeListeners = new Set<(schema: GraphQLSchema) => void>();
|
|
134
130
|
private onSchemaLoadOrUpdateListeners = new Set<
|
|
@@ -210,14 +206,14 @@ export class ApolloGateway implements GraphQLService {
|
|
|
210
206
|
}
|
|
211
207
|
|
|
212
208
|
private initQueryPlanStore(approximateQueryPlanStoreMiB?: number) {
|
|
213
|
-
return new
|
|
209
|
+
return new LRUCache<string, QueryPlan>({
|
|
214
210
|
// Create ~about~ a 30MiB InMemoryLRUCache. This is less than precise
|
|
215
211
|
// since the technique to calculate the size of a DocumentNode is
|
|
216
212
|
// only using JSON.stringify on the DocumentNode (and thus doesn't account
|
|
217
213
|
// for unicode characters, etc.), but it should do a reasonable job at
|
|
218
214
|
// providing a caching document store for most operations.
|
|
219
215
|
maxSize: Math.pow(2, 20) * (approximateQueryPlanStoreMiB || 30),
|
|
220
|
-
|
|
216
|
+
sizeCalculation: approximateObjectSize,
|
|
221
217
|
});
|
|
222
218
|
}
|
|
223
219
|
|
|
@@ -607,7 +603,7 @@ export class ApolloGateway implements GraphQLService {
|
|
|
607
603
|
// Once we remove the deprecated onSchemaChange() method, we can remove this.
|
|
608
604
|
legacyDontNotifyOnSchemaChangeListeners: boolean = false,
|
|
609
605
|
): void {
|
|
610
|
-
|
|
606
|
+
this.queryPlanStore.clear();
|
|
611
607
|
this.schema = toAPISchema(coreSchema);
|
|
612
608
|
this.queryPlanner = new QueryPlanner(coreSchema);
|
|
613
609
|
|
|
@@ -718,7 +714,7 @@ export class ApolloGateway implements GraphQLService {
|
|
|
718
714
|
*/
|
|
719
715
|
public onSchemaChange(
|
|
720
716
|
callback: (schema: GraphQLSchema) => void,
|
|
721
|
-
):
|
|
717
|
+
): GatewayUnsubscriber {
|
|
722
718
|
this.onSchemaChangeListeners.add(callback);
|
|
723
719
|
|
|
724
720
|
return () => {
|
|
@@ -731,7 +727,7 @@ export class ApolloGateway implements GraphQLService {
|
|
|
731
727
|
apiSchema: GraphQLSchema;
|
|
732
728
|
coreSupergraphSdl: string;
|
|
733
729
|
}) => void,
|
|
734
|
-
):
|
|
730
|
+
): GatewayUnsubscriber {
|
|
735
731
|
this.onSchemaLoadOrUpdateListeners.add(callback);
|
|
736
732
|
|
|
737
733
|
return () => {
|
|
@@ -811,9 +807,9 @@ export class ApolloGateway implements GraphQLService {
|
|
|
811
807
|
// ApolloServerPluginUsageReporting) assumes that. In fact, errors talking to backends
|
|
812
808
|
// are unlikely to show up as GraphQLErrors. Do we need to use
|
|
813
809
|
// formatApolloErrors or something?
|
|
814
|
-
public executor = async
|
|
815
|
-
requestContext:
|
|
816
|
-
): Promise<
|
|
810
|
+
public executor = async (
|
|
811
|
+
requestContext: GatewayGraphQLRequestContext,
|
|
812
|
+
): Promise<GatewayExecutionResult> => {
|
|
817
813
|
const spanAttributes = requestContext.operationName
|
|
818
814
|
? { operationName: requestContext.operationName }
|
|
819
815
|
: {};
|
|
@@ -843,10 +839,7 @@ export class ApolloGateway implements GraphQLService {
|
|
|
843
839
|
return { errors: validationErrors };
|
|
844
840
|
}
|
|
845
841
|
|
|
846
|
-
let queryPlan
|
|
847
|
-
if (this.queryPlanStore) {
|
|
848
|
-
queryPlan = await this.queryPlanStore.get(queryPlanStoreKey);
|
|
849
|
-
}
|
|
842
|
+
let queryPlan = this.queryPlanStore.get(queryPlanStoreKey);
|
|
850
843
|
|
|
851
844
|
if (!queryPlan) {
|
|
852
845
|
queryPlan = tracer.startActiveSpan(
|
|
@@ -868,25 +861,11 @@ export class ApolloGateway implements GraphQLService {
|
|
|
868
861
|
},
|
|
869
862
|
);
|
|
870
863
|
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
// whether or not it's successful at this point. We'll instead proceed
|
|
877
|
-
// to serve the rest of the request and just hope that this works out.
|
|
878
|
-
// If it doesn't work, the next request will have another opportunity to
|
|
879
|
-
// try again. Errors will surface as warnings, as appropriate.
|
|
880
|
-
//
|
|
881
|
-
// While it shouldn't normally be necessary to wrap this `Promise` in a
|
|
882
|
-
// `Promise.resolve` invocation, it seems that the underlying cache store
|
|
883
|
-
// is returning a non-native `Promise` (e.g. Bluebird, etc.).
|
|
884
|
-
Promise.resolve(
|
|
885
|
-
this.queryPlanStore.set(queryPlanStoreKey, queryPlan),
|
|
886
|
-
).catch((err) =>
|
|
887
|
-
this.logger.warn(
|
|
888
|
-
'Could not store queryPlan' + ((err && err.message) || err),
|
|
889
|
-
),
|
|
864
|
+
try {
|
|
865
|
+
this.queryPlanStore.set(queryPlanStoreKey, queryPlan);
|
|
866
|
+
} catch (err) {
|
|
867
|
+
this.logger.warn(
|
|
868
|
+
'Could not store queryPlan' + ((err && err.message) || err),
|
|
890
869
|
);
|
|
891
870
|
}
|
|
892
871
|
}
|
|
@@ -908,7 +887,7 @@ export class ApolloGateway implements GraphQLService {
|
|
|
908
887
|
});
|
|
909
888
|
}
|
|
910
889
|
|
|
911
|
-
const response = await executeQueryPlan
|
|
890
|
+
const response = await executeQueryPlan(
|
|
912
891
|
queryPlan,
|
|
913
892
|
serviceMap,
|
|
914
893
|
requestContext,
|
|
@@ -965,8 +944,8 @@ export class ApolloGateway implements GraphQLService {
|
|
|
965
944
|
);
|
|
966
945
|
};
|
|
967
946
|
|
|
968
|
-
private validateIncomingRequest
|
|
969
|
-
requestContext:
|
|
947
|
+
private validateIncomingRequest(
|
|
948
|
+
requestContext: GatewayGraphQLRequestContext,
|
|
970
949
|
operationContext: OperationContext,
|
|
971
950
|
) {
|
|
972
951
|
return tracer.startActiveSpan(OpenTelemetrySpanNames.VALIDATE, (span) => {
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { GraphQLRequest } from 'apollo-server-types';
|
|
2
1
|
import { parse } from 'graphql';
|
|
3
2
|
import { Headers, HeadersInit } from 'node-fetch';
|
|
4
3
|
import { GraphQLDataSource, GraphQLDataSourceRequestKind } from '../../datasources/types';
|
|
5
4
|
import { SERVICE_DEFINITION_QUERY } from '../..';
|
|
6
5
|
import { ServiceDefinitionUpdate, ServiceEndpointDefinition } from '../../config';
|
|
7
6
|
import { ServiceDefinition } from '@apollo/federation';
|
|
7
|
+
import { GatewayGraphQLRequest } from '@apollo/server-gateway-interface';
|
|
8
8
|
|
|
9
9
|
export type Service = ServiceEndpointDefinition & {
|
|
10
10
|
dataSource: GraphQLDataSource;
|
|
@@ -35,7 +35,7 @@ export async function loadServicesFromRemoteEndpoint({
|
|
|
35
35
|
`Tried to load schema for '${name}' but no 'url' was specified.`);
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
const request:
|
|
38
|
+
const request: GatewayGraphQLRequest = {
|
|
39
39
|
query: SERVICE_DEFINITION_QUERY,
|
|
40
40
|
http: {
|
|
41
41
|
url,
|