Skip to content

Commit 9f9f6fe

Browse files
Fix Hive Transform and additional resolvers import issues (#8007)
* fix(hive-transform): usage reporting * No autodISPOSE * Fix Information * Improvements on importing * Fix arguments node * lets go * chore(dependencies): updated changesets for modified dependencies * Go * Go * Fix artifacts * Fix integrity * Fix * Fix TS issue * Use the given schema --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent a24859f commit 9f9f6fe

File tree

15 files changed

+138
-33
lines changed

15 files changed

+138
-33
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@graphql-mesh/include": patch
3+
---
4+
dependencies updates:
5+
- Added dependency [`@graphql-mesh/utils@^0.103.4` ↗︎](https://www.npmjs.com/package/@graphql-mesh/utils/v/0.103.4) (to `dependencies`)

.changeset/cuddly-spies-argue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-mesh/transform-hive': patch
3+
---
4+
5+
Do not break the request even if the operation is not able to process

.changeset/fresh-insects-give.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@omnigraph/json-schema': patch
3+
'@omnigraph/openapi': patch
4+
'@omnigraph/raml': patch
5+
---
6+
7+
DEBUG logs were accidentially written to the output, now it correctly prints logs to the logger not
8+
the output

.changeset/silver-days-sort.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@graphql-mesh/cli': patch
3+
'@graphql-mesh/include': patch
4+
---
5+
6+
Improvements on imports

.changeset/thick-goats-type.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-mesh/utils': patch
3+
---
4+
5+
Always pass a valid info

packages/fusion/composition/tests/loaders.spec.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
import { OperationTypeNode } from 'graphql';
22
import { createGatewayRuntime, useCustomFetch } from '@graphql-hive/gateway-runtime';
3+
import type { Logger } from '@graphql-mesh/types';
34
import { loadJSONSchemaSubgraph } from '@omnigraph/json-schema';
45
import { composeSubgraphs } from '../src/compose';
56

67
describe('Loaders', () => {
8+
const logger: Logger = {
9+
log: jest.fn(),
10+
debug: jest.fn(),
11+
info: jest.fn(),
12+
warn: jest.fn(),
13+
error: jest.fn(),
14+
child: () => logger,
15+
};
716
it('works', async () => {
817
const loadedSubgraph = loadJSONSchemaSubgraph('TEST', {
918
endpoint: 'http://localhost/my-test-api',
@@ -24,6 +33,7 @@ describe('Loaders', () => {
2433
})({
2534
fetch,
2635
cwd: process.cwd(),
36+
logger,
2737
});
2838
const { supergraphSdl } = composeSubgraphs([
2939
{

packages/include/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
},
4747
"typings": "dist/typings/index.d.ts",
4848
"dependencies": {
49+
"@graphql-mesh/utils": "^0.103.4",
4950
"dotenv": "^16.3.1",
5051
"get-tsconfig": "^4.7.6",
5152
"jiti": "^2.0.0",

packages/include/src/index.ts

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
// eslint-disable-next-line import/no-nodejs-modules
22
import Module from 'node:module';
33
import { createPathsMatcher, getTsconfig } from 'get-tsconfig';
4-
import createJITI from 'jiti';
4+
import { createJiti } from 'jiti';
5+
import { defaultImportFn } from '@graphql-mesh/utils';
56

6-
const jiti = createJITI(
7+
const jiti = createJiti(
78
/**
89
* We intentionally provide an empty string here and let jiti handle the base URL.
910
*
1011
* This is because `import.meta.url` is not available in CJS (and cant even be in the syntax)
1112
* and `__filename` is not available in ESM.
1213
*/
1314
'',
15+
{
16+
debug: !!process.env.DEBUG,
17+
},
1418
);
1519

1620
/**
@@ -20,18 +24,20 @@ const jiti = createJITI(
2024
*
2125
* If the module at {@link path} is not found, `null` will be returned.
2226
*/
23-
export async function include<T = any>(path: string, nativeImport?: boolean): Promise<T> {
24-
const module = await (nativeImport ? import(path) : jiti.import(path, {}));
25-
if (!module) {
26-
throw new Error('Included module is empty');
27-
}
28-
if (typeof module !== 'object') {
29-
throw new Error(`Included module is not an object, is instead "${typeof module}"`);
30-
}
31-
if ('default' in module) {
32-
return module.default as T;
27+
export async function include<T = any>(path: string): Promise<T> {
28+
try {
29+
// JITI's tryNative tries native at first but with \`import\`
30+
// So in CJS, this becomes \`require\`, but it still satisfies JITI's native import
31+
return await defaultImportFn(path);
32+
} catch {
33+
const mod = await jiti.import<T>(path, {
34+
default: true,
35+
});
36+
if (!mod) {
37+
throw new Error(`Module at path "${path}" not found`);
38+
}
39+
return mod;
3340
}
34-
return module as T;
3541
}
3642

3743
export interface RegisterTsconfigPathsOptions {

packages/legacy/cli/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export async function graphqlMesh(
124124
configName: cliParams.configName,
125125
additionalPackagePrefixes: cliParams.additionalPackagePrefixes,
126126
initialLoggerPrefix: cliParams.initialLoggerPrefix,
127+
importFn: include,
127128
});
128129
logger = meshConfig.logger;
129130

packages/legacy/transforms/hive/src/index.ts

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import type { ExecutionResult, GraphQLSchema } from 'graphql';
1+
import { isSchema, Kind, visit, type ExecutionResult, type GraphQLSchema } from 'graphql';
22
import type { HiveClient, HivePluginOptions } from '@graphql-hive/core';
33
import { createHive } from '@graphql-hive/yoga';
44
import { process } from '@graphql-mesh/cross-helpers';
55
import { stringInterpolator } from '@graphql-mesh/string-interpolation';
66
import type { MeshTransform, MeshTransformOptions, YamlConfig } from '@graphql-mesh/types';
77
import type { DelegationContext } from '@graphql-tools/delegate';
8-
import type { ExecutionRequest } from '@graphql-tools/utils';
8+
import { mapMaybePromise, type ExecutionRequest } from '@graphql-tools/utils';
99

1010
interface TransformationContext {
11-
collectUsageCallback: ReturnType<HiveClient['collectUsage']>;
11+
collectUsageCallback?: ReturnType<HiveClient['collectUsage']>;
1212
request: ExecutionRequest;
1313
}
1414

1515
export default class HiveTransform implements MeshTransform {
1616
private hiveClient: HiveClient;
1717
private logger: MeshTransformOptions<YamlConfig.HivePlugin>['logger'];
18+
private schema: GraphQLSchema;
1819
constructor({ config, pubsub, logger }: MeshTransformOptions<YamlConfig.HivePlugin>) {
1920
this.logger = logger;
2021
const enabled =
@@ -76,19 +77,31 @@ export default class HiveTransform implements MeshTransform {
7677
agent,
7778
usage,
7879
reporting,
79-
autoDispose: ['SIGINT', 'SIGTERM'],
80+
autoDispose: false,
8081
selfHosting: config.selfHosting,
8182
});
8283
const id = pubsub.subscribe('destroy', () => {
83-
this.hiveClient
84-
.dispose()
85-
.catch(e => logger.error(`Hive client failed to dispose`, e))
86-
.finally(() => pubsub.unsubscribe(id));
84+
try {
85+
mapMaybePromise(
86+
this.hiveClient.dispose(),
87+
() => {
88+
pubsub.unsubscribe(id);
89+
},
90+
e => {
91+
logger.error(`Hive client failed to dispose`, e);
92+
pubsub.unsubscribe(id);
93+
},
94+
);
95+
} catch (e) {
96+
logger.error(`Failed to dispose hive client`, e);
97+
pubsub.unsubscribe(id);
98+
}
8799
});
88100
}
89101

90102
transformSchema(schema: GraphQLSchema) {
91103
this.hiveClient.reportSchema({ schema });
104+
this.schema = schema;
92105
return schema;
93106
}
94107

@@ -97,8 +110,12 @@ export default class HiveTransform implements MeshTransform {
97110
delegationContext: DelegationContext,
98111
transformationContext: TransformationContext,
99112
) {
100-
transformationContext.collectUsageCallback = this.hiveClient.collectUsage();
101-
transformationContext.request = request;
113+
try {
114+
transformationContext.collectUsageCallback = this.hiveClient.collectUsage();
115+
transformationContext.request = request;
116+
} catch (e) {
117+
this.logger.error(`Failed to collect usage`, e);
118+
}
102119
return request;
103120
}
104121

@@ -110,18 +127,27 @@ export default class HiveTransform implements MeshTransform {
110127
// eslint-disable-next-line @typescript-eslint/no-floating-promises -- we dont really care about usage reporting result
111128
try {
112129
transformationContext
113-
.collectUsageCallback(
130+
.collectUsageCallback?.(
114131
{
115-
schema: delegationContext.transformedSchema,
116-
document: transformationContext.request.document,
132+
schema: this.schema,
133+
document: visit(transformationContext.request.document, {
134+
[Kind.FIELD](node) {
135+
if (!node.arguments) {
136+
return {
137+
...node,
138+
arguments: [],
139+
};
140+
}
141+
},
142+
}),
117143
rootValue: transformationContext.request.rootValue,
118144
contextValue: transformationContext.request.context,
119145
variableValues: transformationContext.request.variables,
120146
operationName: transformationContext.request.operationName,
121147
},
122148
result,
123149
)
124-
.catch(e => {
150+
?.catch(e => {
125151
this.logger.error(`Failed to report usage`, e);
126152
});
127153
} catch (e) {

0 commit comments

Comments
 (0)