Skip to content

Commit be23817

Browse files
authored
Do not extract default resolvers and compose scalar configuration (#2925)
* Do not extract default resolvers and compose scalar configuration * Fix build
1 parent fda70fe commit be23817

File tree

5 files changed

+94
-96
lines changed

5 files changed

+94
-96
lines changed

.changeset/nasty-worms-hope.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/resolvers-composition': patch
3+
---
4+
5+
enhance(resolversComposition): do not compose scalar config

.changeset/witty-fireants-bake.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/utils': patch
3+
---
4+
5+
enhance(utils): do not extract default resolvers

packages/loaders/url/tests/url-loader.spec.ts

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ import http from 'http';
1515
import { SubscriptionServer } from 'subscriptions-transport-ws';
1616

1717

18-
const SHOULD_NOT_GET_HERE_ERROR = 'SHOULD_NOT_GET_HERE';
19-
2018
describe('Schema URL Loader', () => {
2119
const loader = new UrlLoader();
2220

@@ -101,43 +99,44 @@ input TestInput {
10199

102100
const testHost = `http://localhost:3000`;
103101
const testPath = '/graphql';
104-
const testPathChecker = (path: string) => {
105-
return path.startsWith(testPath);
106-
};
102+
const testPathChecker = (path: string) => path.startsWith(testPath);
107103
const testUrl = `${testHost}${testPath}`;
108104

109105
describe('handle', () => {
110106

107+
let scope: nock.Scope;
108+
afterEach(() => {
109+
if (!scope?.isDone()) {
110+
scope?.done();
111+
}
112+
})
113+
111114
it('Should throw an error when introspection is not valid', async () => {
112115
const brokenData = { data: {} };
113-
const scope = nock(testHost).post(testPathChecker).reply(200, brokenData);
116+
scope = nock(testHost).post(testPathChecker).reply(200, brokenData);
117+
118+
expect.assertions(1);
114119

115120
try {
116121
await loader.load(testUrl, {});
117-
throw new Error(SHOULD_NOT_GET_HERE_ERROR);
118-
} catch (e) {
119-
expect(e.message).not.toBe(SHOULD_NOT_GET_HERE_ERROR);
120-
expect(e.message).toBe('Could not obtain introspection result, received: ' + JSON.stringify(brokenData));
122+
} catch(e) {
123+
expect(e.message).toBe('Could not obtain introspection result, received: ' + JSON.stringify(brokenData))
121124
}
122-
123-
scope.done();
124125
});
125126

126127
it('Should return a valid schema when request is valid', async () => {
127-
const server = mockGraphQLServer({ schema: testSchema, host: testHost, path: testPathChecker });
128+
scope = mockGraphQLServer({ schema: testSchema, host: testHost, path: testPathChecker });
128129

129130
const schema = await loader.load(testUrl, {});
130131

131-
server.done();
132-
133132
expect(schema.schema).toBeDefined();
134133
expect(printSchemaWithDirectives(schema.schema)).toBeSimilarGqlDoc(testTypeDefs);
135134
});
136135

137136
it('Should pass default headers', async () => {
138137
let headers: Record<string, string | string[]> = {};
139138

140-
const server = mockGraphQLServer({
139+
scope = mockGraphQLServer({
141140
schema: testSchema,
142141
host: testHost,
143142
path: testPathChecker,
@@ -148,8 +147,6 @@ input TestInput {
148147

149148
const schema = await loader.load(testUrl, {});
150149

151-
server.done();
152-
153150
expect(schema).toBeDefined();
154151
expect(schema.schema).toBeDefined();
155152
expect(printSchemaWithDirectives(schema.schema)).toBeSimilarGqlDoc(testTypeDefs);
@@ -160,7 +157,7 @@ input TestInput {
160157

161158
it('Should pass extra headers when they are specified as object', async () => {
162159
let headers: Record<string, string | string[]> = {};
163-
const server = mockGraphQLServer({
160+
scope = mockGraphQLServer({
164161
schema: testSchema,
165162
host: testHost,
166163
path: testPathChecker,
@@ -171,8 +168,6 @@ input TestInput {
171168

172169
const schema = await loader.load(testUrl, { headers: { Auth: '1' } });
173170

174-
server.done();
175-
176171
expect(schema).toBeDefined();
177172
expect(schema.schema).toBeDefined();
178173
expect(printSchemaWithDirectives(schema.schema)).toBeSimilarGqlDoc(testTypeDefs);
@@ -184,7 +179,7 @@ input TestInput {
184179

185180
it('Should pass extra headers when they are specified as array', async () => {
186181
let headers: Record<string, string | string[]> = {};
187-
const server = mockGraphQLServer({
182+
scope = mockGraphQLServer({
188183
schema: testSchema,
189184
host: testHost,
190185
path: testPathChecker,
@@ -194,8 +189,6 @@ input TestInput {
194189
});
195190
const schema = await loader.load(testUrl, { headers: [{ A: '1' }, { B: '2', C: '3' }] });
196191

197-
server.done();
198-
199192
expect(schema).toBeDefined();
200193
expect(schema.schema).toBeDefined();
201194
expect(printSchemaWithDirectives(schema.schema)).toBeSimilarGqlDoc(testTypeDefs);
@@ -208,11 +201,9 @@ input TestInput {
208201
});
209202

210203
it('Should utilize extra introspection options', async () => {
211-
const server = mockGraphQLServer({ schema: testSchema, host: testHost, path: testPathChecker });
204+
scope = mockGraphQLServer({ schema: testSchema, host: testHost, path: testPathChecker });
212205
const source = await loader.load(testUrl, { descriptions: false });
213206

214-
server.done();
215-
216207
expect(source).toBeDefined();
217208
expect(source.schema.getQueryType().description).toBeUndefined();
218209
});
@@ -221,18 +212,18 @@ input TestInput {
221212
expect(await loader.canLoad(cwd(), {})).toBeFalsy();
222213
});
223214
it('should handle useGETForQueries correctly', async () => {
224-
const server = mockGraphQLServer({ schema: testSchema, host: testHost, path: testPathChecker, method: 'GET' });
215+
scope = mockGraphQLServer({ schema: testSchema, host: testHost, path: testPathChecker, method: 'GET' });
225216

226217
const source = await loader.load(testUrl, {
227218
descriptions: false,
228219
useGETForQueries: true,
229220
});
230221

231-
server.done();
232-
233222
const testVariableValue = 'A';
234223

235-
const server2 = mockGraphQLServer({ schema: testSchema, host: testHost, path: testPathChecker, method: 'GET' });
224+
scope.done();
225+
226+
scope = mockGraphQLServer({ schema: testSchema, host: testHost, path: testPathChecker, method: 'GET' });
236227

237228
const result = await execute({
238229
schema: source.schema,
@@ -246,7 +237,7 @@ input TestInput {
246237
},
247238
});
248239

249-
server2.done();
240+
scope.done();
250241

251242
expect(result?.errors).toBeFalsy();
252243

@@ -259,11 +250,9 @@ input TestInput {
259250
path: '/graphql',
260251
};
261252
const url = address.host + address.path;
262-
const server = mockGraphQLServer({ schema: testSchema, host: address.host, path: address.path });
253+
scope = mockGraphQLServer({ schema: testSchema, host: address.host, path: address.path });
263254
const result = await loader.load(url, {});
264255

265-
server.done();
266-
267256
expect(result.schema).toBeDefined();
268257
expect(printSchemaWithDirectives(result.schema)).toBeSimilarGqlDoc(testTypeDefs);
269258
});
@@ -274,15 +263,13 @@ input TestInput {
274263
path: '/graphql',
275264
};
276265
const url = address.host + address.path;
277-
const server = mockGraphQLServer({
266+
scope = mockGraphQLServer({
278267
schema: testSchema,
279268
host: address.host.replace('ws', 'http'),
280269
path: address.path,
281270
});
282271
const result = await loader.load(url, {});
283272

284-
server.done();
285-
286273
expect(result.schema).toBeDefined();
287274
expect(printSchemaWithDirectives(result.schema)).toBeSimilarGqlDoc(testTypeDefs);
288275
});
@@ -293,26 +280,22 @@ input TestInput {
293280
path: '/graphql',
294281
};
295282
const url = address.host + address.path;
296-
const server = mockGraphQLServer({
283+
scope = mockGraphQLServer({
297284
schema: testSchema,
298285
host: address.host.replace('wss', 'https'),
299286
path: address.path,
300287
});
301288
const result = await loader.load(url, {});
302289

303-
server.done();
304-
305290
expect(result.schema).toBeDefined();
306291
expect(printSchemaWithDirectives(result.schema)).toBeSimilarGqlDoc(testTypeDefs);
307292
});
308293
it('should handle .graphql files', async () => {
309294
const testHost = 'http://localhost:3000';
310295
const testPath = '/schema.graphql';
311-
const server = nock(testHost).get(testPath).reply(200, testTypeDefs);
296+
scope = nock(testHost).get(testPath).reply(200, testTypeDefs);
312297
const result = await loader.load(testHost + testPath, {});
313298

314-
server.done();
315-
316299
expect(result.schema).toBeDefined();
317300
expect(printSchemaWithDirectives(result.schema)).toBeSimilarGqlDoc(testTypeDefs);
318301

@@ -322,13 +305,11 @@ input TestInput {
322305
it('should handle results with handleAsSDL option even if it doesn\'t end with .graphql', async () => {
323306
const testHost = 'http://localhost:3000';
324307
const testPath = '/sdl';
325-
const server = nock(testHost).get(testPath).reply(200, testTypeDefs);
308+
scope = nock(testHost).get(testPath).reply(200, testTypeDefs);
326309
const result = await loader.load(testHost + testPath, {
327310
handleAsSDL: true,
328311
});
329312

330-
server.done();
331-
332313
expect(result.schema).toBeDefined();
333314
expect(printSchemaWithDirectives(result.schema)).toBeSimilarGqlDoc(testTypeDefs);
334315

@@ -469,15 +450,15 @@ input TestInput {
469450
httpServer.close(done);
470451
});
471452
it('should handle multipart requests', async () => {
472-
let server = mockGraphQLServer({ schema: testSchema, host: testHost, path: testPathChecker, method: 'POST' });
453+
scope = mockGraphQLServer({ schema: testSchema, host: testHost, path: testPathChecker, method: 'POST' });
473454

474455
const { schema } = await loader.load(testUrl, {
475456
multipart: true,
476457
});
477458

478-
server.done();
459+
scope.done();
479460

480-
server = mockGraphQLServer({ schema: testSchema, host: testHost, path: testPathChecker, method: 'POST' })
461+
scope = mockGraphQLServer({ schema: testSchema, host: testHost, path: testPathChecker, method: 'POST' })
481462

482463
const fileName = 'testfile.txt';
483464

@@ -500,8 +481,6 @@ input TestInput {
500481
},
501482
})
502483

503-
server.done();
504-
505484
const content = readFileSync(absoluteFilePath, 'utf8')
506485

507486
expect(result.errors).toBeFalsy();

packages/resolvers-composition/src/resolvers-composition.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { chainFunctions } from './chain-functions';
22
import { get, set, flatten } from 'lodash';
3-
import { isScalarType, GraphQLFieldResolver } from 'graphql';
3+
import { GraphQLFieldResolver, GraphQLScalarTypeConfig } from 'graphql';
44
import { asArray } from '@graphql-tools/utils';
55

6-
export type ResolversComposition<Resolver extends GraphQLFieldResolver<any, any, any> = GraphQLFieldResolver<any, any>> = (
7-
next: Resolver
8-
) => Resolver;
6+
export type ResolversComposition<
7+
Resolver extends GraphQLFieldResolver<any, any, any> = GraphQLFieldResolver<any, any>
8+
> = (next: Resolver) => Resolver;
99

1010
export type ResolversComposerMapping<Resolvers extends Record<string, any> = Record<string, any>> =
1111
| {
@@ -21,6 +21,10 @@ export type ResolversComposerMapping<Resolvers extends Record<string, any> = Rec
2121
[path: string]: ResolversComposition | ResolversComposition[];
2222
};
2323

24+
function isScalarTypeConfiguration(config: any): config is GraphQLScalarTypeConfig<any, any> {
25+
return config && 'serialize' in config && 'parseLiteral' in config;
26+
}
27+
2428
function resolveRelevantMappings<Resolvers extends Record<string, any> = Record<string, any>>(
2529
resolvers: Resolvers,
2630
path: string,
@@ -31,7 +35,7 @@ function resolveRelevantMappings<Resolvers extends Record<string, any> = Record<
3135
if (split.length === 2) {
3236
const typeName = split[0];
3337

34-
if (isScalarType(resolvers[typeName])) {
38+
if (isScalarTypeConfiguration(resolvers[typeName])) {
3539
return [];
3640
}
3741

0 commit comments

Comments
 (0)