diff --git a/.changeset/@graphql-cli_codegen-8556-dependencies.md b/.changeset/@graphql-cli_codegen-8556-dependencies.md new file mode 100644 index 00000000000..5b0feb96585 --- /dev/null +++ b/.changeset/@graphql-cli_codegen-8556-dependencies.md @@ -0,0 +1,5 @@ +--- +"@graphql-cli/codegen": patch +--- +dependencies updates: + - Updated dependency [`@graphql-codegen/cli@2.13.10` ↗︎](https://www.npmjs.com/package/@graphql-codegen/cli/v/2.13.10) (from `2.13.9`, in `dependencies`) diff --git a/.changeset/@graphql-codegen_cli-8556-dependencies.md b/.changeset/@graphql-codegen_cli-8556-dependencies.md new file mode 100644 index 00000000000..15bf2e19ab0 --- /dev/null +++ b/.changeset/@graphql-codegen_cli-8556-dependencies.md @@ -0,0 +1,5 @@ +--- +"@graphql-codegen/cli": patch +--- +dependencies updates: + - Updated dependency [`@graphql-codegen/core@2.6.4` ↗︎](https://www.npmjs.com/package/@graphql-codegen/core/v/2.6.4) (from `2.6.3`, in `dependencies`) diff --git a/.changeset/@graphql-codegen_core-8556-dependencies.md b/.changeset/@graphql-codegen_core-8556-dependencies.md new file mode 100644 index 00000000000..4a13c9717f2 --- /dev/null +++ b/.changeset/@graphql-codegen_core-8556-dependencies.md @@ -0,0 +1,5 @@ +--- +"@graphql-codegen/core": patch +--- +dependencies updates: + - Updated dependency [`@graphql-tools/utils@9.0.0` ↗︎](https://www.npmjs.com/package/@graphql-tools/utils/v/9.0.0) (from `^8.8.0`, in `dependencies`) diff --git a/.changeset/config.json b/.changeset/config.json index fadac42b598..402435abbf4 100644 --- a/.changeset/config.json +++ b/.changeset/config.json @@ -5,8 +5,13 @@ "access": "restricted", "baseBranch": "master", "updateInternalDependencies": "patch", - "ignore": ["@graphql-codegen/testing", "website", "example-*"], - "changelog": ["@changesets/changelog-github", { "repo": "dotansimha/graphql-code-generator" }], + "ignore": ["website", "example-*"], + "changelog": [ + "@changesets/changelog-github", + { + "repo": "dotansimha/graphql-code-generator" + } + ], "snapshot": { "useCalculatedVersion": true, "prereleaseTemplate": "{tag}-{datetime}-{commit}" diff --git a/.changeset/metal-ravens-juggle.md b/.changeset/metal-ravens-juggle.md new file mode 100644 index 00000000000..b1e0aa406a2 --- /dev/null +++ b/.changeset/metal-ravens-juggle.md @@ -0,0 +1,5 @@ +--- +'@graphql-codegen/testing': minor +--- + +`mockGraphQLServer`, resolvers common testing helpers and fixtures, typing updates diff --git a/packages/plugins/flow/resolvers/tests/flow-resolvers.spec.ts b/packages/plugins/flow/resolvers/tests/flow-resolvers.spec.ts index 36cb0f0828f..c62d126d86d 100644 --- a/packages/plugins/flow/resolvers/tests/flow-resolvers.spec.ts +++ b/packages/plugins/flow/resolvers/tests/flow-resolvers.spec.ts @@ -1,7 +1,7 @@ import '@graphql-codegen/testing'; import { buildSchema } from 'graphql'; import { plugin } from '../src/index.js'; -import { schema } from '../../../typescript/resolvers/tests/common.js'; +import { resolversTestingSchema } from '@graphql-codegen/testing'; import { Types, mergeOutputs } from '@graphql-codegen/plugin-helpers'; import { ENUM_RESOLVERS_SIGNATURE } from '../src/visitor.js'; @@ -132,7 +132,7 @@ describe('Flow Resolvers Plugin', () => { }); it('Should generate basic type resolvers', () => { - const result = plugin(schema, [], {}, { outputFile: '' }); + const result = plugin(resolversTestingSchema, [], {}, { outputFile: '' }); expect(result).toMatchSnapshot(); }); @@ -156,7 +156,7 @@ describe('Flow Resolvers Plugin', () => { }); it('Should generate ResolversParentTypes', () => { - const result = plugin(schema, [], {}, { outputFile: '' }) as Types.ComplexPluginOutput; + const result = plugin(resolversTestingSchema, [], {}, { outputFile: '' }) as Types.ComplexPluginOutput; expect(result.content).toBeSimilarStringTo(` /** Mapping between all available schema types and the resolvers parents */ diff --git a/packages/plugins/flow/resolvers/tests/mapping.spec.ts b/packages/plugins/flow/resolvers/tests/mapping.spec.ts index baae3bbd254..a0f9fc821de 100644 --- a/packages/plugins/flow/resolvers/tests/mapping.spec.ts +++ b/packages/plugins/flow/resolvers/tests/mapping.spec.ts @@ -1,5 +1,5 @@ import '@graphql-codegen/testing'; -import { schema } from '../../../typescript/resolvers/tests/common.js'; +import { resolversTestingSchema } from '@graphql-codegen/testing'; import { plugin } from '../src/index.js'; import { buildSchema } from 'graphql'; import { validateFlow as validate } from '../../flow/tests/validate-flow.js'; @@ -7,7 +7,7 @@ import { Types, mergeOutputs } from '@graphql-codegen/plugin-helpers'; describe('ResolversTypes', () => { it('Should build ResolversTypes object when there are no mappers', async () => { - const result = await plugin(schema, [], {}, { outputFile: '' }); + const result = await plugin(resolversTestingSchema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type ResolversTypes = { @@ -30,7 +30,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with simple mappers', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { mappers: { @@ -62,7 +62,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with defaultMapper set', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { mappers: { @@ -95,7 +95,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with external mappers', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { mappers: { @@ -201,7 +201,7 @@ describe('ResolversTypes', () => { it('Should generate basic type resolvers with external mappers', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { mappers: { @@ -283,7 +283,7 @@ describe('ResolversTypes', () => { it('Should generate basic type resolvers with external mappers using same imported type', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { mappers: { @@ -366,7 +366,7 @@ describe('ResolversTypes', () => { it('Should generate the correct resolvers when used with mappers with interfaces', async () => { const spy = jest.spyOn(console, 'warn').mockImplementation(); const result = (await plugin( - schema, + resolversTestingSchema, [], { mappers: { @@ -448,7 +448,7 @@ describe('ResolversTypes', () => { it('Should generate basic type resolvers with defaultMapper set to any', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { defaultMapper: 'any', @@ -526,7 +526,7 @@ describe('ResolversTypes', () => { it('Should generate basic type resolvers with defaultMapper set to external identifier', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { defaultMapper: './my-file#MyBaseType', @@ -606,7 +606,7 @@ describe('ResolversTypes', () => { it('Should replace using Omit when non-mapped type is pointing to mapped type', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { mappers: { @@ -638,7 +638,7 @@ describe('ResolversTypes', () => { it('Should not replace using Omit when non-mapped type is pointing to mapped type', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { mappers: { @@ -670,7 +670,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with defaultMapper set using {T}', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { defaultMapper: '$Shape<{T}>', @@ -699,7 +699,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with defaultMapper set using {T} with external identifier', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { defaultMapper: './my-wrapper#CustomPartial<{T}>', diff --git a/packages/plugins/typescript/resolvers/tests/mapping.spec.ts b/packages/plugins/typescript/resolvers/tests/mapping.spec.ts index 326693c992a..b3f0ae4e969 100644 --- a/packages/plugins/typescript/resolvers/tests/mapping.spec.ts +++ b/packages/plugins/typescript/resolvers/tests/mapping.spec.ts @@ -1,12 +1,12 @@ import { Types, mergeOutputs } from '@graphql-codegen/plugin-helpers'; import '@graphql-codegen/testing'; -import { schema, validate } from './common.js'; +import { resolversTestingSchema, resolversTestingValidate } from '@graphql-codegen/testing'; import { plugin } from '../src/index.js'; import { buildSchema } from 'graphql'; describe('ResolversTypes', () => { it('Should build ResolversTypes object when there are no mappers', async () => { - const result = await plugin(schema, [], {}, { outputFile: '' }); + const result = await plugin(resolversTestingSchema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type ResolversTypes = { @@ -29,7 +29,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with simple mappers', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { mappers: { @@ -247,7 +247,7 @@ describe('ResolversTypes', () => { } `; - await validate( + await resolversTestingValidate( mergeOutputs([usage, result]), { scalars: { @@ -260,7 +260,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with defaultMapper set using {T}', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -290,7 +290,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with defaultMapper set using {T} with external identifier', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -321,7 +321,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with mapper set for concrete type using {T} with external identifier', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -422,12 +422,12 @@ describe('ResolversTypes', () => { } `; - await validate(mergeOutputs([result, usage]), config, testSchema); + await resolversTestingValidate(mergeOutputs([result, usage]), config, testSchema); }); it('Should build ResolversTypes with mapper set for concrete type using renamed external identifier', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -460,7 +460,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with mapper set for concrete type using renamed external identifier (with default)', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -494,7 +494,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with mapper set for concrete type using renamed external identifier (with default) and type import', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -531,7 +531,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with defaultMapper set', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -565,7 +565,7 @@ describe('ResolversTypes', () => { it('Should build ResolversTypes with external mappers', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -598,7 +598,7 @@ describe('ResolversTypes', () => { it('Should handle {T} in a mapper', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -708,7 +708,7 @@ describe('ResolversTypes', () => { it('Should generate basic type resolvers with external mappers', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -785,12 +785,12 @@ describe('ResolversTypes', () => { somethingChanged?: SubscriptionResolver, "somethingChanged", ParentType, ContextType>; }; `); - await validate(result); + await resolversTestingValidate(result); }); it('Should generate basic type resolvers with external mappers using same imported type', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -868,13 +868,13 @@ describe('ResolversTypes', () => { somethingChanged?: SubscriptionResolver, "somethingChanged", ParentType, ContextType>; }; `); - await validate(result); + await resolversTestingValidate(result); }); it('Should generate the correct resolvers when used with mappers with interfaces', async () => { const spy = jest.spyOn(console, 'warn').mockImplementation(); const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -949,14 +949,14 @@ describe('ResolversTypes', () => { somethingChanged?: SubscriptionResolver, "somethingChanged", ParentType, ContextType>; }; `); - await validate(mergeOutputs([result, `type MyNodeType = {};`])); + await resolversTestingValidate(mergeOutputs([result, `type MyNodeType = {};`])); spy.mockRestore(); }); it('Should generate basic type resolvers with defaultMapper set to any', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -1019,12 +1019,12 @@ describe('ResolversTypes', () => { somethingChanged?: SubscriptionResolver, "somethingChanged", ParentType, ContextType>; }; `); - await validate(result); + await resolversTestingValidate(result); }); it('Should generate basic type resolvers with defaultMapper set to external identifier', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -1089,12 +1089,12 @@ describe('ResolversTypes', () => { somethingChanged?: SubscriptionResolver, "somethingChanged", ParentType, ContextType>; }; `); - await validate(result); + await resolversTestingValidate(result); }); it('Should replace using Omit when non-mapped type is pointing to mapped type', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -1122,12 +1122,12 @@ describe('ResolversTypes', () => { Int: ResolverTypeWrapper; Boolean: ResolverTypeWrapper; };`); - await validate(mergeOutputs([result, 'type MyOtherTypeCustom = {};'])); + await resolversTestingValidate(mergeOutputs([result, 'type MyOtherTypeCustom = {};'])); }); it('Should not replace using Omit when non-mapped type is pointing to mapped type', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -1156,11 +1156,11 @@ describe('ResolversTypes', () => { Int: ResolverTypeWrapper; Boolean: ResolverTypeWrapper; };`); - await validate(mergeOutputs([result, `type MyTypeCustom = {}; type MyOtherTypeCustom = {};`])); + await resolversTestingValidate(mergeOutputs([result, `type MyTypeCustom = {}; type MyOtherTypeCustom = {};`])); }); it('Should build ResolversTypes object when there are no mappers', async () => { - const result = await plugin(schema, [], {}, { outputFile: '' }); + const result = await plugin(resolversTestingSchema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type ResolversTypes = { @@ -1184,7 +1184,7 @@ describe('ResolversTypes', () => { it('should support namespaces', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -1238,7 +1238,7 @@ describe('ResolversTypes', () => { it('should support namespaces in contextType', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -1256,7 +1256,7 @@ describe('ResolversTypes', () => { it('should support namespaces in defaultMapper', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { defaultMapper: './my-file#MyNamespace#MyDefaultMapper', @@ -1307,7 +1307,7 @@ describe('ResolversTypes', () => { it('should support namespaces in rootValueType', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -1359,7 +1359,7 @@ describe('ResolversTypes', () => { it('should support namespaces and {T} placeholder', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { defaultMapper: './my-file#MyNamespace#MyDefaultMapper<{T}>', diff --git a/packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts b/packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts index caabfd8a119..7ff9ae2b6e6 100644 --- a/packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts +++ b/packages/plugins/typescript/resolvers/tests/ts-resolvers.spec.ts @@ -1,8 +1,7 @@ -import { validateTs } from '@graphql-codegen/testing'; +import { validateTs, resolversTestingSchema, resolversTestingValidate } from '@graphql-codegen/testing'; import { buildSchema } from 'graphql'; import { plugin } from '../src/index.js'; import { plugin as tsPlugin } from '../../typescript/src/index.js'; -import { schema, validate } from './common.js'; import { Types, mergeOutputs } from '@graphql-codegen/plugin-helpers'; import { ENUM_RESOLVERS_SIGNATURE } from '../src/visitor.js'; @@ -72,7 +71,7 @@ describe('TypeScript Resolvers Plugin', () => { }); it('Should use StitchingResolver when its active on config', async () => { - const result = await plugin(schema, [], { noSchemaStitching: false }, { outputFile: '' }); + const result = await plugin(resolversTestingSchema, [], { noSchemaStitching: false }, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(`export type StitchingResolver`); expect(result.content).toBeSimilarStringTo(` @@ -82,7 +81,7 @@ describe('TypeScript Resolvers Plugin', () => { | StitchingResolver; `); - await validate(result); + await resolversTestingValidate(result); }); describe('Config', () => { @@ -90,8 +89,8 @@ describe('TypeScript Resolvers Plugin', () => { const config = { onlyResolveTypeForInterfaces: true, }; - const result = await plugin(schema, [], config, { outputFile: '' }); - const content = await validate(result, config, schema); + const result = await plugin(resolversTestingSchema, [], config, { outputFile: '' }); + const content = await resolversTestingValidate(result, config, resolversTestingSchema); expect(content).toBeSimilarStringTo(` export type NodeResolvers = { @@ -105,9 +104,9 @@ describe('TypeScript Resolvers Plugin', () => { useIndexSignature: true, optionalInfoArgument: true, }; - const result = await plugin(schema, [], config, { outputFile: '' }); + const result = await plugin(resolversTestingSchema, [], config, { outputFile: '' }); - const content = await validate(result, config, schema); + const content = await resolversTestingValidate(result, config, resolversTestingSchema); expect(content).not.toContain(`info: `); expect(content).toContain(`info?: `); @@ -120,12 +119,12 @@ describe('TypeScript Resolvers Plugin', () => { useIndexSignature: true, allowParentTypeOverride: true, }; - const result = await plugin(schema, [], config, { outputFile: '' }); + const result = await plugin(resolversTestingSchema, [], config, { outputFile: '' }); - const content = await validate( + const content = await resolversTestingValidate( result, config, - schema, + resolversTestingSchema, ` export const myTypeResolvers: MyTypeResolvers<{}, { parentOverride: boolean }> = { foo: (parentValue) => { @@ -148,7 +147,7 @@ describe('TypeScript Resolvers Plugin', () => { useIndexSignature: true, namespacedImportName: 'Types', }; - const result = await plugin(schema, [], config, { outputFile: '' }); + const result = await plugin(resolversTestingSchema, [], config, { outputFile: '' }); const content = mergeOutputs([result]); expect(content).toMatchSnapshot(); }); @@ -166,7 +165,7 @@ describe('TypeScript Resolvers Plugin', () => { ) => Promise | TResult;`, }, }; - const result = await plugin(schema, [], config, { outputFile: '' }); + const result = await plugin(resolversTestingSchema, [], config, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type ResolverFnAuthenticated = ( @@ -193,7 +192,7 @@ export type MyTypeResolvers { - const result = await plugin(schema, [], { makeResolverTypeCallable: true }, { outputFile: '' }); + const result = await plugin(resolversTestingSchema, [], { makeResolverTypeCallable: true }, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type Resolver = @@ -206,11 +205,11 @@ export type MyTypeResolvers; `); - await validate(result); + await resolversTestingValidate(result); }); it('makeResolverTypeCallable - adds ResolverWithResolve type to resolver union when set to false', async () => { - const result = await plugin(schema, [], { makeResolverTypeCallable: false }, { outputFile: '' }); + const result = await plugin(resolversTestingSchema, [], { makeResolverTypeCallable: false }, { outputFile: '' }); expect(result.content).not.toBeSimilarStringTo(` export type Resolver = @@ -223,7 +222,7 @@ export type MyTypeResolvers; `); - await validate(result); + await resolversTestingValidate(result); }); }); @@ -234,7 +233,7 @@ export type MyTypeResolvers; noSchemaStitching: true, }; const result = await plugin(testSchema, [], config, { outputFile: '' }); - const mergedOutput = await validate( + const mergedOutput = await resolversTestingValidate( result, config, testSchema, @@ -314,7 +313,7 @@ __isTypeOf?: IsTypeOfResolverFn; }; const result = await plugin(testSchema, [], config, { outputFile: '' }); - const mergedOutput = await validate( + const mergedOutput = await resolversTestingValidate( result, config, testSchema, @@ -357,7 +356,7 @@ __isTypeOf?: IsTypeOfResolverFn; }; const result = await plugin(testSchema, [], config, { outputFile: '' }); - const mergedOutput = await validate( + const mergedOutput = await resolversTestingValidate( result, config, testSchema, @@ -408,7 +407,7 @@ __isTypeOf?: IsTypeOfResolverFn; }; const result = await plugin(testSchema, [], config, { outputFile: '' }); - const mergedOutput = await validate( + const mergedOutput = await resolversTestingValidate( result, config, testSchema, @@ -457,7 +456,7 @@ __isTypeOf?: IsTypeOfResolverFn; }; const result = await plugin(testSchema, [], config, { outputFile: '' }); - const mergedOutput = await validate( + const mergedOutput = await resolversTestingValidate( result, config, testSchema, @@ -493,7 +492,7 @@ __isTypeOf?: IsTypeOfResolverFn; it('Should allow to override ResolverTypeWrapper signature', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -507,7 +506,7 @@ __isTypeOf?: IsTypeOfResolverFn; it('Should have default value for ResolverTypeWrapper signature', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -520,18 +519,18 @@ __isTypeOf?: IsTypeOfResolverFn; it('Should not warn when noSchemaStitching is not defined', async () => { const spy = jest.spyOn(console, 'warn').mockImplementation(); - const result = await plugin(schema, [], {}, { outputFile: '' }); + const result = await plugin(resolversTestingSchema, [], {}, { outputFile: '' }); expect(spy).not.toHaveBeenCalled(); spy.mockRestore(); - await validate(result); + await resolversTestingValidate(result); }); it('Should disable StitchingResolver on demand', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { noSchemaStitching: true, @@ -557,7 +556,7 @@ __isTypeOf?: IsTypeOfResolverFn; ResolverFn | ResolverWithResolve; `); - await validate(result); + await resolversTestingValidate(result); }); it('Default values of args and compatibility with typescript plugin', async () => { @@ -630,7 +629,7 @@ __isTypeOf?: IsTypeOfResolverFn; it('Should allow to generate optional __resolveType', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { optionalResolveType: true }, { outputFile: '' } @@ -651,7 +650,7 @@ __isTypeOf?: IsTypeOfResolverFn; }); it('Should generate basic type resolvers', async () => { - const result = await plugin(schema, [], {}, { outputFile: '' }); + const result = await plugin(resolversTestingSchema, [], {}, { outputFile: '' }); expect(result.content).toBeSimilarStringTo(` export type MyDirectiveDirectiveArgs = { @@ -718,12 +717,12 @@ __isTypeOf?: IsTypeOfResolverFn; }; `); - await validate(result); + await resolversTestingValidate(result); }); it('Should generate basic type resolvers with avoidOptionals', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { avoidOptionals: true }, { outputFile: '' } @@ -793,12 +792,12 @@ __isTypeOf?: IsTypeOfResolverFn; }; `); - await validate(result); + await resolversTestingValidate(result); }); it('Should allow to override context with simple identifier', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { contextType: 'MyCustomCtx', @@ -866,12 +865,12 @@ __isTypeOf?: IsTypeOfResolverFn; }; `); - await validate(mergeOutputs([result, `type MyCustomCtx = {};`])); + await resolversTestingValidate(mergeOutputs([result, `type MyCustomCtx = {};`])); }); it('Should with correctly with addUnderscoreToArgsType set to true', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { addUnderscoreToArgsType: true, @@ -882,12 +881,12 @@ __isTypeOf?: IsTypeOfResolverFn; expect(result.content).toContain('MyType_WithArgsArgs'); expect(result.content).not.toContain('MyTypeWithArgsArgs'); - await validate(mergeOutputs([result])); + await resolversTestingValidate(mergeOutputs([result])); }); it('Should allow to override context with mapped context type', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { contextType: './my-file#MyCustomCtx', @@ -957,12 +956,12 @@ __isTypeOf?: IsTypeOfResolverFn; }; `); - await validate(result); + await resolversTestingValidate(result); }); it('Should allow to override context with mapped context type', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { contextType: './my-file#MyCustomCtx', @@ -1032,11 +1031,11 @@ __isTypeOf?: IsTypeOfResolverFn; }; `); - await validate(result); + await resolversTestingValidate(result); }); it('Should allow to override context with mapped context type as default export', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { contextType: './my-file#default', @@ -1106,11 +1105,11 @@ __isTypeOf?: IsTypeOfResolverFn; }; `); - await validate(result); + await resolversTestingValidate(result); }); it('Should allow to override context with mapped context type as default export with type import', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { contextType: './my-file#default', @@ -1181,12 +1180,12 @@ __isTypeOf?: IsTypeOfResolverFn; }; `); - await validate(result); + await resolversTestingValidate(result); }); it('should generate named custom field level context type', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { fieldContextTypes: [ @@ -1219,7 +1218,7 @@ __isTypeOf?: IsTypeOfResolverFn; it('should generate named custom field level context type for field with directive', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { directiveContextTypes: ['authenticated#./my-file#AuthenticatedContext'], @@ -1242,7 +1241,7 @@ __isTypeOf?: IsTypeOfResolverFn; it('should generate named custom field level context type for field with directive and context type', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { directiveContextTypes: ['authenticated#./my-file#AuthenticatedContext'], @@ -1266,7 +1265,7 @@ __isTypeOf?: IsTypeOfResolverFn; it('should generate named custom field level context type for field with directive and field context type', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { directiveContextTypes: ['authenticated#./my-file#AuthenticatedContext'], @@ -1295,7 +1294,7 @@ __isTypeOf?: IsTypeOfResolverFn; expect(result.prepend).toContain( `import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';` ); - await validate(result, {}, schema); + await resolversTestingValidate(result, {}, resolversTestingSchema); }); it('Should generate the correct imports when schema has no scalars', async () => { @@ -1303,7 +1302,7 @@ __isTypeOf?: IsTypeOfResolverFn; const result = await plugin(testSchema, [], {}, { outputFile: '' }); expect(result.prepend).not.toContain(`import { GraphQLResolveInfo, GraphQLScalarTypeConfig } from 'graphql';`); - await validate(result, {}, testSchema); + await resolversTestingValidate(result, {}, testSchema); }); it('Should generate the correct imports when customResolveInfo defined in config', async () => { @@ -1319,7 +1318,7 @@ __isTypeOf?: IsTypeOfResolverFn; expect(result.prepend).toContain(`import { GraphQLScalarType, GraphQLScalarTypeConfig } from 'graphql';`); expect(result.prepend).toContain(`import { MyGraphQLResolveInfo as GraphQLResolveInfo } from './my-type';`); - await validate(result, {}, testSchema); + await resolversTestingValidate(result, {}, testSchema); }); describe('Should generate the correct imports when customResolverFn defined in config', () => { @@ -1336,7 +1335,7 @@ __isTypeOf?: IsTypeOfResolverFn; expect(result.prepend).toContain(`import { MyResolverFn as ResolverFn } from './my-type';`); expect(result.prepend).toContain(`export { ResolverFn };`); - await validate(result, {}, testSchema); + await resolversTestingValidate(result, {}, testSchema); }); it('./my-type#ResolverFn', async () => { @@ -1352,7 +1351,7 @@ __isTypeOf?: IsTypeOfResolverFn; expect(result.prepend).toContain(`import { ResolverFn } from './my-type';`); expect(result.prepend).toContain(`export { ResolverFn };`); - await validate(result, {}, testSchema); + await resolversTestingValidate(result, {}, testSchema); }); it(`definition directly`, async () => { @@ -1374,7 +1373,7 @@ __isTypeOf?: IsTypeOfResolverFn; )) as Types.ComplexPluginOutput; expect(result.prepend).toContain(`export type ResolverFn = ${fnDefinition}`); - await validate(result, {}, testSchema); + await resolversTestingValidate(result, {}, testSchema); }); it(`ok with default`, async () => { @@ -1389,7 +1388,7 @@ export type ResolverFn = ( const result = await plugin(testSchema, [], {}, { outputFile: '' }); expect(result.content).toContain(defaultResolverFn); - await validate(result, {}, testSchema); + await resolversTestingValidate(result, {}, testSchema); }); }); @@ -1427,7 +1426,7 @@ export type ResolverFn = ( const testSchema = buildSchema(`type MyType { f(a: String): String }`); const config = { typesPrefix: 'T' }; const result = await plugin(testSchema, [], config, { outputFile: '' }); - const o = await validate(result, config, testSchema); + const o = await resolversTestingValidate(result, config, testSchema); expect(o).toContain( `f?: Resolver, ParentType, ContextType, Partial>;` @@ -1442,7 +1441,7 @@ export type ResolverFn = ( expect(result.content).toBeSimilarStringTo( `f?: Resolver, ParentType, ContextType, Partial>;` ); - await validate(result, {}, testSchema); + await resolversTestingValidate(result, {}, testSchema); }); // dotansimha/graphql-code-generator#3322 @@ -1453,7 +1452,7 @@ export type ResolverFn = ( expect(result.content).toBeSimilarStringTo( `f?: Resolver, ParentType, ContextType>;` ); - await validate(result, {}, testSchema); + await resolversTestingValidate(result, {}, testSchema); }); it('should generate Resolvers interface', async () => { @@ -2222,7 +2221,7 @@ export type ResolverFn = ( typesSuffix: 'QL', }; const output = await plugin(testSchema, [], config, { outputFile: 'graphql.ts' }); - const o = await validate(output, config, testSchema); + const o = await resolversTestingValidate(output, config, testSchema); expect(o).not.toContain( `user?: Resolver, ParentType, ContextType, RequireFields>;` ); @@ -2254,7 +2253,7 @@ export type ResolverFn = ( constEnums: true, }; const output = await plugin(testSchema, [], config, { outputFile: 'graphql.ts' }); - const o = await validate(output, config, testSchema); + const o = await resolversTestingValidate(output, config, testSchema); expect(o).toBeSimilarStringTo(` export const enum Test { @@ -2410,7 +2409,7 @@ export type ResolverFn = ( it('Should generate resolvers with replaced internalResolversPrefix if specified', async () => { const result = (await plugin( - schema, + resolversTestingSchema, [], { internalResolversPrefix: '' }, { outputFile: '' } @@ -2434,7 +2433,7 @@ export type ResolverFn = ( }; `); - await validate(result); + await resolversTestingValidate(result); }); it('#7005 - avoidOptionals should preserve optional resolvers', async () => { diff --git a/packages/utils/graphql-codegen-testing/src/index.ts b/packages/utils/graphql-codegen-testing/src/index.ts index 88d32776365..1f3dff89134 100644 --- a/packages/utils/graphql-codegen-testing/src/index.ts +++ b/packages/utils/graphql-codegen-testing/src/index.ts @@ -92,3 +92,4 @@ export function useMonorepo({ dirname }: { dirname: string }) { export * from './typescript.js'; export * from './mock-graphql-server.js'; +export * from './resolvers-common.js'; diff --git a/packages/plugins/typescript/resolvers/tests/common.ts b/packages/utils/graphql-codegen-testing/src/resolvers-common.ts similarity index 88% rename from packages/plugins/typescript/resolvers/tests/common.ts rename to packages/utils/graphql-codegen-testing/src/resolvers-common.ts index c3cf68278f9..46fb82d9fdb 100644 --- a/packages/plugins/typescript/resolvers/tests/common.ts +++ b/packages/utils/graphql-codegen-testing/src/resolvers-common.ts @@ -3,7 +3,7 @@ import { Types, mergeOutputs } from '@graphql-codegen/plugin-helpers'; import { buildSchema } from 'graphql'; import { plugin as tsPlugin } from '@graphql-codegen/typescript'; -export const schema = buildSchema(/* GraphQL */ ` +export const resolversTestingSchema = buildSchema(/* GraphQL */ ` type MyType { foo: String! @authenticated otherType: MyOtherType @@ -46,10 +46,10 @@ export const schema = buildSchema(/* GraphQL */ ` directive @authenticated on FIELD_DEFINITION `); -export const validate = async ( +export const resolversTestingValidate = async ( content: Types.PluginOutput, config: any = {}, - pluginSchema = schema, + pluginSchema = resolversTestingSchema, additionalCode = '' ) => { const mergedContent = mergeOutputs([