-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.d.ts
202 lines (180 loc) · 7.49 KB
/
index.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import { FastifyInstance, FastifyReply } from 'fastify'
import { MercuriusContext, MercuriusPlugin, MercuriusOptions, PreExecutionHookResponse } from 'mercurius'
import { IncomingHttpHeaders, OutgoingHttpHeaders } from "http"
import {
DocumentNode,
GraphQLSchema,
ExecutionResult,
} from "graphql";
interface ServiceConfig {
setSchema: (schema: string) => ServiceConfig;
}
interface Gateway {
refresh: (isRetry?: boolean) => Promise<GraphQLSchema | null>;
serviceMap: Record<string, ServiceConfig>;
/**
* `preGatewayExecution` is the hook to be executed in the GraphQL gateway request lifecycle.
* The previous hook was `preExecution`, the next hook will be `onResolution`.
* Notice: in the `preGatewayExecution` hook, you can modify the following items by returning them in the hook definition:
* - `document`
* - `errors`
* Each hook definition will trigger multiple times in a single request just before executing remote GraphQL queries on the federated services.
*/
addHook<TContext = MercuriusContext, TError extends Error = Error>(name: 'preGatewayExecution', hook: preGatewayExecutionHookHandler<TContext, TError>): void;
/**
* `preGatewaySubscriptionExecution` is the hook to be executed in the GraphQL gateway subscription lifecycle.
* The previous hook was `preSubscriptionExecution`, the next hook will be `onSubscriptionResolution`.
*/
addHook<TContext = MercuriusContext>(name: 'preGatewaySubscriptionExecution', hook: preGatewaySubscriptionExecutionHookHandler<TContext>): void;
/**
* `onGatewayReplaceSchema` is an application lifecycle hook. When the Gateway service obtains new versions of federated schemas within a defined polling interval, the `onGatewayReplaceSchema` hook will be triggered every time a new schema is built. It is called just before the old schema is replaced with the new one.
* This hook will only be triggered in gateway mode. It has the following parameters:
* - `instance` - The gateway server `FastifyInstance` (this contains the old schema).
* - `schema` - The new schema that has been built from the gateway refresh.
*/
addHook(name: 'onGatewayReplaceSchema', hook: onGatewayReplaceSchemaHookHandler): void;
}
declare module "fastify" {
interface FastifyInstance {
/**
* GraphQL plugin
*/
graphqlGateway: Gateway
}
interface FastifyReply {
/**
* @param source GraphQL query string
* @param context request context
* @param variables request variables which will get passed to the executor
* @param operationName specify which operation will be run
*/
graphql<
TData extends Record<string, any> = Record<string, any>,
TVariables extends Record<string, any> = Record<string, any>
>(
source: string,
context?: Record<string, any>,
variables?: TVariables,
operationName?: string
): Promise<ExecutionResult<TData>>;
}
}
/**
* Federated GraphQL Service metadata
*/
export interface MercuriusServiceMetadata {
name: string;
}
export interface Collectors {
collectHeaders?: boolean;
collectStatutsCodes?: boolean;
collectExtensions?: boolean;
}
interface WsConnectionParams {
connectionInitPayload?:
| (() => Record<string, any> | Promise<Record<string, any>>)
| Record<string, any>;
reconnect?: boolean;
maxReconnectAttempts?: number;
connectionCallback?: () => void;
failedConnectionCallback?: (err: { message: string }) => void | Promise<void>;
failedReconnectCallback?: () => void;
rewriteConnectionInitPayload?: <TContext extends MercuriusContext = MercuriusContext>(payload: Record<string, any> | undefined, context: TContext) => Record<string, any>;
}
export interface MercuriusGatewayService {
name: string;
url: string | string[];
schema?: string;
wsUrl?: string;
mandatory?: boolean;
initHeaders?:
| (() => OutgoingHttpHeaders | Promise<OutgoingHttpHeaders>)
| OutgoingHttpHeaders;
rewriteHeaders?: <TContext extends MercuriusContext = MercuriusContext>(
headers: IncomingHttpHeaders,
context: TContext
) => OutgoingHttpHeaders | Promise<OutgoingHttpHeaders>;
connections?: number;
bodyTimeout?: number;
headersTimeout?: number;
maxHeaderSize?: number;
keepAlive?: number;
keepAliveMaxTimeout?: number;
rejectUnauthorized?: boolean;
wsConnectionParams?:
| (() => WsConnectionParams | Promise<WsConnectionParams>)
| WsConnectionParams;
setResponseHeaders?: (reply: FastifyReply) => void;
collectors?: Collectors;
allowBatchedQueries?: boolean;
}
export interface MercuriusGatewayOptions {
gateway: {
services: Array<MercuriusGatewayService> | (() => Promise<Array<MercuriusGatewayService>>);
pollingInterval?: number;
errorHandler?(error: Error, service: MercuriusGatewayService): void;
retryServicesCount?: number;
retryServicesInterval?: number;
};
}
type MercuriusFederationOptions = MercuriusOptions & MercuriusGatewayOptions
declare const mercuriusGatewayPlugin: (
instance: FastifyInstance,
opts: MercuriusFederationOptions
) => void
export default mercuriusGatewayPlugin;
// ------------------------
// Request Lifecycle hooks
// ------------------------
/**
* `preGatewayExecution` is the hook to be executed in the GraphQL gateway request lifecycle.
* The previous hook was `preExecution`, the next hook will be `onResolution`.
* Notice: in the `preGatewayExecution` hook, you can modify the following items by returning them in the hook definition:
* - `document`
* - `errors`
* Each hook definition will trigger multiple times in a single request just before executing remote GraphQL queries on the federated services.
*
* Because it is a gateway hook, this hook contains service metadata in the `service` parameter:
* - `name`: service name
*/
export interface preGatewayExecutionHookHandler<TContext = MercuriusContext, TError extends Error = Error> {
(
schema: GraphQLSchema,
source: DocumentNode,
context: TContext,
service: MercuriusServiceMetadata
): Promise<PreExecutionHookResponse<TError> | void> | PreExecutionHookResponse<TError> | void;
}
// -----------------------------
// Subscription Lifecycle hooks
// -----------------------------
/**
* `preGatewaySubscriptionExecution` is the hook to be executed in the GraphQL gateway subscription lifecycle.
* The previous hook was `preSubscriptionExecution`, the next hook will be `onSubscriptionResolution`.
*
* Because it is a gateway hook, this hook contains service metadata in the `service` parameter:
* - `name`: service name
*/
export interface preGatewaySubscriptionExecutionHookHandler<TContext = MercuriusContext> {
(
schema: GraphQLSchema,
source: DocumentNode,
context: TContext,
service: MercuriusServiceMetadata
): Promise<void> | void;
}
// ----------------------------
// Application Lifecycle hooks
// ----------------------------
/**
* `onGatewayReplaceSchema` is an application lifecycle hook. When the Gateway service obtains new versions of federated schemas within a defined polling interval, the `onGatewayReplaceSchema` hook will be triggered every time a new schema is built. It is called just before the old schema is replaced with the new one.
* This hook will only be triggered in gateway mode. It has the following parameters:
* - `instance` - The gateway server `FastifyInstance` (this contains the old schema).
* - `schema` - The new schema that has been built from the gateway refresh.
*/
export interface onGatewayReplaceSchemaHookHandler {
(
instance: FastifyInstance,
schema: GraphQLSchema
): Promise<void> | void;
}