Skip to content

Commit 9f50d92

Browse files
committed
enhance: use fetch and file: to access files
1 parent b6acbc4 commit 9f50d92

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+316
-139
lines changed

.changeset/eighty-poets-roll.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@graphql-mesh/supergraph': minor
3+
'@graphql-mesh/graphql': minor
4+
'@omnigraph/json-schema': minor
5+
'@omnigraph/openapi': minor
6+
'json-machete': minor
7+
'@graphql-mesh/utils': minor
8+
---
9+
10+
Use `fetch` with `file://` to access files

.changeset/lucky-otters-cough.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-mesh/utils': minor
3+
---
4+
5+
Use `URL.canParse` to check if it is URL

examples/json-schema-file-upload/tests/json-schema-file-upload.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ describe('JSON Schema File Uploads', () => {
1212
});
1313
mesh = await getMesh({
1414
...config,
15-
fetchFn: router.fetch as any,
15+
fetchFn: router.fetch,
1616
});
1717
});
1818
afterAll(() => {

examples/json-schema-subscriptions/api/app.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { createRouter, Response } from 'fets';
2+
import { MeshFetch } from '@graphql-mesh/types';
23
import { fetch as defaultFetch } from '@whatwg-node/fetch';
34

4-
export function createApi(fetch = defaultFetch) {
5+
export function createApi(fetch: MeshFetch = defaultFetch) {
56
let todos = [];
67

78
const app = createRouter()
@@ -20,13 +21,18 @@ export function createApi(fetch = defaultFetch) {
2021
...reqBody,
2122
};
2223
todos.push(todo);
23-
await fetch('http://127.0.0.1:4000/webhooks/todo_added', {
24-
method: 'POST',
25-
headers: {
26-
'Content-Type': 'application/json',
27-
},
28-
body: JSON.stringify(todo),
29-
}).catch(console.log);
24+
try {
25+
const res = await fetch('http://127.0.0.1:4000/webhooks/todo_added', {
26+
method: 'POST',
27+
headers: {
28+
'Content-Type': 'application/json',
29+
},
30+
body: JSON.stringify(todo),
31+
});
32+
console.log('Webhook response', await res.text());
33+
} catch (e) {
34+
console.error('Failed to send webhook', e);
35+
}
3036
return Response.json(todo);
3137
},
3238
});

examples/json-schema-subscriptions/tests/json-schema-subscriptions.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ describe('JSON Schema Subscriptions', () => {
2424
baseDir,
2525
getBuiltMesh: () => fakePromise(mesh),
2626
});
27-
const api = createApi(meshHttp.fetch as any);
27+
const api = createApi(meshHttp.fetch);
2828
});
2929
afterEach(() => {
3030
resetTodos();

packages/json-machete/src/dereferenceObject.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import JsonPointer from 'json-pointer';
22
import urlJoin from 'url-join';
3+
import type { MeshFetch } from '@graphql-mesh/types';
34
import { handleUntitledDefinitions } from './healUntitledDefinitions.js';
45

56
export const resolvePath = (path: string, root: any): any => {
@@ -61,6 +62,7 @@ export async function dereferenceObject<T extends object, TRoot = T>(
6162
obj: T,
6263
{
6364
cwd = globalThis?.process.cwd(),
65+
fetch,
6466
externalFileCache = new Map<string, any>(),
6567
refMap = new Map<string, any>(),
6668
root = obj as any,
@@ -69,11 +71,12 @@ export async function dereferenceObject<T extends object, TRoot = T>(
6971
resolvedObjects = new WeakSet(),
7072
}: {
7173
cwd?: string;
74+
fetch: MeshFetch;
7275
externalFileCache?: Map<string, any>;
7376
refMap?: Map<string, any>;
7477
root?: TRoot;
7578
debugLogFn?(message?: any): void;
76-
readFileOrUrl(path: string, opts: { cwd: string }): Promise<any> | any;
79+
readFileOrUrl(path: string, opts: { cwd: string; fetch: MeshFetch }): Promise<any> | any;
7780
resolvedObjects?: WeakSet<any>;
7881
},
7982
): Promise<T> {
@@ -91,7 +94,7 @@ export async function dereferenceObject<T extends object, TRoot = T>(
9194
let externalFile = externalFileCache.get(externalFilePath);
9295
if (!externalFile) {
9396
try {
94-
externalFile = await readFileOrUrl(externalFilePath, { cwd });
97+
externalFile = await readFileOrUrl(externalFilePath, { cwd, fetch });
9598
} catch (e) {
9699
console.error(e);
97100
throw new Error(`Unable to load ${externalRelativeFilePath} from ${cwd}`);
@@ -137,6 +140,7 @@ export async function dereferenceObject<T extends object, TRoot = T>(
137140
readFileOrUrl,
138141
root: externalFile,
139142
resolvedObjects,
143+
fetch,
140144
},
141145
);
142146
refMap.set($ref, result);
@@ -161,6 +165,7 @@ export async function dereferenceObject<T extends object, TRoot = T>(
161165
*/
162166
const result = await dereferenceObject(resolvedObj, {
163167
cwd,
168+
fetch,
164169
externalFileCache,
165170
refMap,
166171
root,
@@ -193,6 +198,7 @@ export async function dereferenceObject<T extends object, TRoot = T>(
193198
debugLogFn,
194199
readFileOrUrl,
195200
resolvedObjects,
201+
fetch,
196202
});
197203
}
198204
}

packages/legacy/handlers/graphql/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,14 +263,14 @@ export default class GraphQLHandler implements MeshHandler {
263263
source: schemaConfig,
264264
}: YamlConfig.GraphQLHandlerCodeFirstConfiguration): Promise<MeshSource> {
265265
if (schemaConfig.endsWith('.graphql')) {
266-
const rawSDL = await readFileOrUrl<string>(schemaConfig, {
266+
const rawAst = await readFileOrUrl<DocumentNode>(schemaConfig, {
267267
cwd: this.baseDir,
268268
allowUnknownExtensions: true,
269269
importFn: this.importFn,
270270
fetch: this.fetchFn,
271271
logger: this.logger,
272272
});
273-
const schema = buildSchema(rawSDL, {
273+
const schema = buildASTSchema(rawAst, {
274274
assumeValid: true,
275275
assumeValidSDL: true,
276276
});

packages/legacy/handlers/supergraph/src/index.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ export default class SupergraphHandler implements MeshHandler {
6666
fetch: this.fetchFn,
6767
logger: this.logger,
6868
}).catch(e => {
69-
throw new Error(`Failed to load supergraph SDL from ${interpolatedSource}:\n ${e.message}`);
69+
throw new Error(
70+
`Supergraph source must be a valid GraphQL SDL string or a parsed DocumentNode, but got an invalid result from ${interpolatedSource} instead.\n Got error: ${e.message}`,
71+
);
7072
});
7173
return handleSupergraphResponse(res, interpolatedSource);
7274
}
@@ -81,7 +83,9 @@ export default class SupergraphHandler implements MeshHandler {
8183
fetch: this.fetchFn,
8284
logger: this.logger,
8385
}).catch(e => {
84-
throw new Error(`Failed to load supergraph SDL from ${interpolatedSource}:\n ${e.message}`);
86+
throw new Error(
87+
`Supergraph source must be a valid GraphQL SDL string or a parsed DocumentNode, but got an invalid result from ${interpolatedSource} instead.\n Got error: ${e.message}`,
88+
);
8589
});
8690
return handleSupergraphResponse(sdlOrIntrospection, interpolatedSource);
8791
});

packages/legacy/handlers/supergraph/tests/supergraph.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,6 @@ describe('Supergraph', () => {
246246
expect(logger.error.mock.calls[0][0].toString())
247247
.toBe(`Failed to generate the schema for the source
248248
Supergraph source must be a valid GraphQL SDL string or a parsed DocumentNode, but got an invalid result from ./fixtures/supergraph-invalid.graphql instead.
249-
Got result: type Query {
250-
251249
Got error: Syntax Error: Expected Name, found <EOF>.`);
252250
});
253251
it('throws a helpful error when the source is down', async () => {
@@ -270,8 +268,8 @@ describe('Supergraph', () => {
270268
).rejects.toThrow();
271269
expect(logger.error.mock.calls[0][0].toString())
272270
.toBe(`Failed to generate the schema for the source
273-
Failed to load supergraph SDL from http://down-sdl-source.com/my-sdl.graphql:
274-
getaddrinfo ENOTFOUND down-sdl-source.com`);
271+
Supergraph source must be a valid GraphQL SDL string or a parsed DocumentNode, but got an invalid result from http://down-sdl-source.com/my-sdl.graphql instead.
272+
Got error: getaddrinfo ENOTFOUND down-sdl-source.com`);
275273
});
276274
it('configures WebSockets for subscriptions correctly', async () => {
277275
await using authorsHttpServer = await createDisposableServer(authorsServer);

packages/legacy/testing/getTestMesh.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function getTestMesh(extraOptions?: Partial<GetMeshOptions>) {
4646
validate: false,
4747
});
4848
return getMesh({
49-
fetchFn: yoga.fetch as any,
49+
fetchFn: yoga.fetch,
5050
sources: [
5151
{
5252
name: 'Yoga',

0 commit comments

Comments
 (0)