From 5dfc79fa6d974362f38361f7dffbe984a9546377 Mon Sep 17 00:00:00 2001 From: Alessia Bellisario Date: Fri, 12 Apr 2024 08:31:21 -0400 Subject: [PATCH] Call `createMockSchema` inside `createTestSchema` (#11777) --- .api-reports/api-report-testing.md | 50 +++++----- .api-reports/api-report-testing_core.md | 45 +++++---- .changeset/spotty-garlics-knock.md | 5 + src/__tests__/__snapshots__/exports.ts.snap | 1 - .../core/__tests__/createTestSchema.test.tsx | 96 ++++++++++--------- src/testing/core/createTestSchema.ts | 57 ++++++----- src/testing/index.ts | 1 - 7 files changed, 147 insertions(+), 108 deletions(-) create mode 100644 .changeset/spotty-garlics-knock.md diff --git a/.api-reports/api-report-testing.md b/.api-reports/api-report-testing.md index e2734983594..b85108ace73 100644 --- a/.api-reports/api-report-testing.md +++ b/.api-reports/api-report-testing.md @@ -449,11 +449,6 @@ type ConcastSourcesIterable = Iterable>; // @public (undocumented) export function createMockClient(data: TData, query: DocumentNode, variables?: {}): ApolloClient; -// @alpha -export const createMockSchema: (staticSchema: GraphQLSchema, mocks: { - [key: string]: any; -}) => GraphQLSchema; - // @alpha export const createSchemaFetch: (schema: GraphQLSchema, mockFetchOpts?: { validate: boolean; @@ -462,10 +457,11 @@ export const createSchemaFetch: (schema: GraphQLSchema, mockFetchOpts?: { restore: () => void; } & Disposable; +// Warning: (ae-forgotten-export) The symbol "TestSchemaOptions" needs to be exported by the entry point index.d.ts // Warning: (ae-forgotten-export) The symbol "ProxiedSchema" needs to be exported by the entry point index.d.ts // // @alpha -export const createTestSchema: (schemaWithMocks: GraphQLSchema, resolvers: Resolvers) => ProxiedSchema; +export const createTestSchema: (schemaWithTypeDefs: GraphQLSchema, options: TestSchemaOptions) => ProxiedSchema; // @public (undocumented) namespace DataProxy { @@ -1286,24 +1282,10 @@ type Path = ReadonlyArray; // @public (undocumented) type Primitive = null | undefined | string | number | boolean | symbol | bigint; -// Warning: (ae-forgotten-export) The symbol "ProxiedSchemaFns" needs to be exported by the entry point index.d.ts +// Warning: (ae-forgotten-export) The symbol "TestSchemaFns" needs to be exported by the entry point index.d.ts // // @public (undocumented) -type ProxiedSchema = GraphQLSchema & ProxiedSchemaFns; - -// @public (undocumented) -interface ProxiedSchemaFns { - // (undocumented) - add: (addOptions: { - resolvers: Resolvers; - }) => ProxiedSchema; - // (undocumented) - fork: (forkOptions?: { - resolvers?: Resolvers; - }) => ProxiedSchema; - // (undocumented) - reset: () => void; -} +type ProxiedSchema = GraphQLSchema & TestSchemaFns; // @public (undocumented) class QueryInfo { @@ -1681,6 +1663,30 @@ interface SubscriptionOptions { variables?: TVariables; } +// @public (undocumented) +interface TestSchemaFns { + // (undocumented) + add: (addOptions: { + resolvers: Resolvers; + }) => ProxiedSchema; + // (undocumented) + fork: (forkOptions?: { + resolvers?: Resolvers; + }) => ProxiedSchema; + // (undocumented) + reset: () => void; +} + +// @public (undocumented) +interface TestSchemaOptions { + // (undocumented) + resolvers: Resolvers; + // (undocumented) + scalars?: { + [key: string]: any; + }; +} + // @public (undocumented) export function tick(): Promise; diff --git a/.api-reports/api-report-testing_core.md b/.api-reports/api-report-testing_core.md index 0534bddcecb..6199767a19f 100644 --- a/.api-reports/api-report-testing_core.md +++ b/.api-reports/api-report-testing_core.md @@ -456,10 +456,11 @@ export const createSchemaFetch: (schema: GraphQLSchema, mockFetchOpts?: { restore: () => void; } & Disposable; +// Warning: (ae-forgotten-export) The symbol "TestSchemaOptions" needs to be exported by the entry point index.d.ts // Warning: (ae-forgotten-export) The symbol "ProxiedSchema" needs to be exported by the entry point index.d.ts // // @alpha -export const createTestSchema: (schemaWithMocks: GraphQLSchema, resolvers: Resolvers) => ProxiedSchema; +export const createTestSchema: (schemaWithTypeDefs: GraphQLSchema, options: TestSchemaOptions) => ProxiedSchema; // @public (undocumented) namespace DataProxy { @@ -1236,24 +1237,10 @@ type Path = ReadonlyArray; // @public (undocumented) type Primitive = null | undefined | string | number | boolean | symbol | bigint; -// Warning: (ae-forgotten-export) The symbol "ProxiedSchemaFns" needs to be exported by the entry point index.d.ts +// Warning: (ae-forgotten-export) The symbol "TestSchemaFns" needs to be exported by the entry point index.d.ts // // @public (undocumented) -type ProxiedSchema = GraphQLSchema & ProxiedSchemaFns; - -// @public (undocumented) -interface ProxiedSchemaFns { - // (undocumented) - add: (addOptions: { - resolvers: Resolvers; - }) => ProxiedSchema; - // (undocumented) - fork: (forkOptions?: { - resolvers?: Resolvers; - }) => ProxiedSchema; - // (undocumented) - reset: () => void; -} +type ProxiedSchema = GraphQLSchema & TestSchemaFns; // @public (undocumented) class QueryInfo { @@ -1633,6 +1620,30 @@ interface SubscriptionOptions { variables?: TVariables; } +// @public (undocumented) +interface TestSchemaFns { + // (undocumented) + add: (addOptions: { + resolvers: Resolvers; + }) => ProxiedSchema; + // (undocumented) + fork: (forkOptions?: { + resolvers?: Resolvers; + }) => ProxiedSchema; + // (undocumented) + reset: () => void; +} + +// @public (undocumented) +interface TestSchemaOptions { + // (undocumented) + resolvers: Resolvers; + // (undocumented) + scalars?: { + [key: string]: any; + }; +} + // @public (undocumented) export function tick(): Promise; diff --git a/.changeset/spotty-garlics-knock.md b/.changeset/spotty-garlics-knock.md new file mode 100644 index 00000000000..fa58c519668 --- /dev/null +++ b/.changeset/spotty-garlics-knock.md @@ -0,0 +1,5 @@ +--- +"@apollo/client": minor +--- + +Call `createMockSchema` inside `createTestSchema`. diff --git a/src/__tests__/__snapshots__/exports.ts.snap b/src/__tests__/__snapshots__/exports.ts.snap index bee4f75643a..11cd795a396 100644 --- a/src/__tests__/__snapshots__/exports.ts.snap +++ b/src/__tests__/__snapshots__/exports.ts.snap @@ -369,7 +369,6 @@ Array [ "MockSubscriptionLink", "MockedProvider", "createMockClient", - "createMockSchema", "createSchemaFetch", "createTestSchema", "itAsync", diff --git a/src/testing/core/__tests__/createTestSchema.test.tsx b/src/testing/core/__tests__/createTestSchema.test.tsx index 25dbe78631e..d205fe3d6db 100644 --- a/src/testing/core/__tests__/createTestSchema.test.tsx +++ b/src/testing/core/__tests__/createTestSchema.test.tsx @@ -17,7 +17,6 @@ import { createTestSchema } from "../createTestSchema.js"; import { GraphQLError, buildSchema } from "graphql"; import type { UseSuspenseQueryResult } from "../../../react/index.js"; import { useMutation, useSuspenseQuery } from "../../../react/index.js"; -import { createMockSchema } from "../../graphql-tools/utils.js"; import userEvent from "@testing-library/user-event"; import { act, screen } from "@testing-library/react"; import { createSchemaFetch } from "../createSchemaFetch.js"; @@ -140,34 +139,35 @@ interface ViewerQueryData { } describe("schema proxy", () => { - const schemaWithMocks = createMockSchema(schemaWithTypeDefs, { - ID: () => "1", - Int: () => 42, - String: () => "String", - Date: () => new Date("January 1, 2024 01:00:00").toJSON().split("T")[0], - }); - - const schema = createTestSchema(schemaWithMocks, { - Query: { - viewer: () => ({ - name: "Jane Doe", - book: { - text: "Hello World", - title: "The Book", + const schema = createTestSchema(schemaWithTypeDefs, { + resolvers: { + Query: { + viewer: () => ({ + name: "Jane Doe", + book: { + text: "Hello World", + title: "The Book", + }, + }), + }, + Book: { + __resolveType: (obj) => { + if ("text" in obj) { + return "TextBook"; + } + if ("colors" in obj) { + return "ColoringBook"; + } + throw new Error("Could not resolve type"); }, - }), - }, - Book: { - __resolveType: (obj) => { - if ("text" in obj) { - return "TextBook"; - } - if ("colors" in obj) { - return "ColoringBook"; - } - throw new Error("Could not resolve type"); }, }, + scalars: { + ID: () => "1", + Int: () => 42, + String: () => "String", + Date: () => new Date("January 1, 2024 01:00:00").toJSON().split("T")[0], + }, }); it("mocks scalars and resolvers", async () => { @@ -849,27 +849,35 @@ describe("schema proxy", () => { it("preserves resolvers from previous calls to .add on subsequent calls to .fork", async () => { let name = "Virginia"; - const schema = createTestSchema(schemaWithMocks, { - Query: { - viewer: () => ({ - name, - book: { - text: "Hello World", - title: "The Book", + const schema = createTestSchema(schemaWithTypeDefs, { + resolvers: { + Query: { + viewer: () => ({ + name, + book: { + text: "Hello World", + title: "The Book", + }, + }), + }, + Book: { + __resolveType: (obj) => { + if ("text" in obj) { + return "TextBook"; + } + if ("colors" in obj) { + return "ColoringBook"; + } + throw new Error("Could not resolve type"); }, - }), - }, - Book: { - __resolveType: (obj) => { - if ("text" in obj) { - return "TextBook"; - } - if ("colors" in obj) { - return "ColoringBook"; - } - throw new Error("Could not resolve type"); }, }, + scalars: { + ID: () => "1", + Int: () => 42, + String: () => "String", + Date: () => new Date("January 1, 2024 01:00:00").toJSON().split("T")[0], + }, }); schema.add({ diff --git a/src/testing/core/createTestSchema.ts b/src/testing/core/createTestSchema.ts index af7911d50da..a294223440e 100644 --- a/src/testing/core/createTestSchema.ts +++ b/src/testing/core/createTestSchema.ts @@ -1,16 +1,22 @@ import { addResolversToSchema } from "@graphql-tools/schema"; import type { GraphQLSchema } from "graphql"; +import { createMockSchema } from "../graphql-tools/utils.js"; import type { Resolvers } from "../../core/types.js"; -type ProxiedSchema = GraphQLSchema & ProxiedSchemaFns; +type ProxiedSchema = GraphQLSchema & TestSchemaFns; -interface ProxiedSchemaFns { +interface TestSchemaFns { add: (addOptions: { resolvers: Resolvers }) => ProxiedSchema; fork: (forkOptions?: { resolvers?: Resolvers }) => ProxiedSchema; reset: () => void; } +interface TestSchemaOptions { + resolvers: Resolvers; + scalars?: { [key: string]: any }; +} + /** * A function that creates a [Proxy object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) * around a given `schema` with `resolvers`. This proxied schema can be used to @@ -19,24 +25,26 @@ interface ProxiedSchemaFns { * can be modified independently of the original schema. `reset` will restore * resolvers to the original proxied schema. * - * @param schemaWithMocks - A `GraphQLSchema`. - * @param resolvers - `Resolvers` object. + * @param schema - A `GraphQLSchema`. + * @param options - An `options` object that accepts `scalars` and `resolvers` objects. * @returns A `ProxiedSchema` with `add`, `fork` and `reset` methods. * * @example * ```js - * const schemaWithMocks = createMockSchema(schemaWithTypeDefs, { - ID: () => "1", - Int: () => 36, - String: () => "String", - Date: () => new Date("December 10, 1815 01:00:00").toJSON().split("T")[0], - }); * - * const schema = createTestSchema(schemaWithMocks, { - Query: { - writer: () => ({ - name: "Ada Lovelace", - }), + * const schema = createTestSchema(schemaWithTypeDefs, { + * resolvers: { + Query: { + writer: () => ({ + name: "Ada Lovelace", + }), + } + }, + scalars: { + ID: () => "1", + Int: () => 36, + String: () => "String", + Date: () => new Date("December 10, 1815 01:00:00").toJSON().split("T")[0], } }); * ``` @@ -44,16 +52,16 @@ interface ProxiedSchemaFns { * @alpha */ const createTestSchema = ( - schemaWithMocks: GraphQLSchema, - resolvers: Resolvers + schemaWithTypeDefs: GraphQLSchema, + options: TestSchemaOptions ): ProxiedSchema => { - let targetResolvers = { ...resolvers }; + let targetResolvers = { ...options.resolvers }; let targetSchema = addResolversToSchema({ - schema: schemaWithMocks, + schema: createMockSchema(schemaWithTypeDefs, options.scalars ?? {}), resolvers: targetResolvers, }); - const fns: ProxiedSchemaFns = { + const fns: TestSchemaFns = { add: ({ resolvers: newResolvers }) => { targetResolvers = { ...targetResolvers, ...newResolvers }; targetSchema = addResolversToSchema({ @@ -65,13 +73,16 @@ const createTestSchema = ( }, fork: ({ resolvers: newResolvers } = {}) => { - return createTestSchema(targetSchema, newResolvers ?? targetResolvers); + return createTestSchema(targetSchema, { + resolvers: newResolvers ?? targetResolvers, + scalars: options.scalars, + }); }, reset: () => { targetSchema = addResolversToSchema({ - schema: schemaWithMocks, - resolvers, + schema: schemaWithTypeDefs, + resolvers: options.resolvers, }); }, }; diff --git a/src/testing/index.ts b/src/testing/index.ts index 2a499aa8d97..be84a5e57e5 100644 --- a/src/testing/index.ts +++ b/src/testing/index.ts @@ -2,4 +2,3 @@ import "../utilities/globals/index.js"; export type { MockedProviderProps } from "./react/MockedProvider.js"; export { MockedProvider } from "./react/MockedProvider.js"; export * from "./core/index.js"; -export { createMockSchema } from "./graphql-tools/utils.js";