From 05632c5cf437719fbd2a544c972a342898eaabc4 Mon Sep 17 00:00:00 2001 From: Alessia Bellisario Date: Wed, 10 Apr 2024 11:40:55 -0400 Subject: [PATCH 1/5] feat: call createMockSchema inside createTestSchema --- .api-reports/api-report-testing.md | 50 +++++----- .api-reports/api-report-testing_core.md | 45 +++++---- src/__tests__/__snapshots__/exports.ts.snap | 1 - .../core/__tests__/createTestSchema.test.tsx | 96 ++++++++++--------- src/testing/core/createTestSchema.ts | 57 ++++++----- src/testing/index.ts | 1 - 6 files changed, 142 insertions(+), 108 deletions(-) diff --git a/.api-reports/api-report-testing.md b/.api-reports/api-report-testing.md index e2734983594..6ee0fe52a61 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) + mocks?: { + [key: string]: any; + }; + // (undocumented) + resolvers: Resolvers; +} + // @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..aea8eb306d8 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) + mocks?: { + [key: string]: any; + }; + // (undocumented) + resolvers: Resolvers; +} + // @public (undocumented) export function tick(): Promise; 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..14c81cd22ed 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"); }, }, + mocks: { + 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"); }, }, + mocks: { + 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..b508e42973e 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; + mocks?: { [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 `mocks` 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", + }), + } + }, + mocks: { + 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.mocks ?? {}), 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, + mocks: options.mocks, + }); }, 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"; From 227c7cbe368e8e4337241a379275b36526b3f07b Mon Sep 17 00:00:00 2001 From: Alessia Bellisario Date: Wed, 10 Apr 2024 11:41:27 -0400 Subject: [PATCH 2/5] chore: add changeset --- .changeset/spotty-garlics-knock.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/spotty-garlics-knock.md 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`. From 8199455654e82af516a0a205c26001fc902183a6 Mon Sep 17 00:00:00 2001 From: Alessia Bellisario Date: Fri, 12 Apr 2024 08:13:44 -0400 Subject: [PATCH 3/5] fix: rename mocks to scalars --- src/testing/core/__tests__/createTestSchema.test.tsx | 4 ++-- src/testing/core/createTestSchema.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/testing/core/__tests__/createTestSchema.test.tsx b/src/testing/core/__tests__/createTestSchema.test.tsx index 14c81cd22ed..d205fe3d6db 100644 --- a/src/testing/core/__tests__/createTestSchema.test.tsx +++ b/src/testing/core/__tests__/createTestSchema.test.tsx @@ -162,7 +162,7 @@ describe("schema proxy", () => { }, }, }, - mocks: { + scalars: { ID: () => "1", Int: () => 42, String: () => "String", @@ -872,7 +872,7 @@ describe("schema proxy", () => { }, }, }, - mocks: { + scalars: { ID: () => "1", Int: () => 42, String: () => "String", diff --git a/src/testing/core/createTestSchema.ts b/src/testing/core/createTestSchema.ts index b508e42973e..8e381136bf2 100644 --- a/src/testing/core/createTestSchema.ts +++ b/src/testing/core/createTestSchema.ts @@ -14,7 +14,7 @@ interface TestSchemaFns { interface TestSchemaOptions { resolvers: Resolvers; - mocks?: { [key: string]: any }; + scalars?: { [key: string]: any }; } /** @@ -57,7 +57,7 @@ const createTestSchema = ( ): ProxiedSchema => { let targetResolvers = { ...options.resolvers }; let targetSchema = addResolversToSchema({ - schema: createMockSchema(schemaWithTypeDefs, options.mocks ?? {}), + schema: createMockSchema(schemaWithTypeDefs, options.scalars ?? {}), resolvers: targetResolvers, }); @@ -75,7 +75,7 @@ const createTestSchema = ( fork: ({ resolvers: newResolvers } = {}) => { return createTestSchema(targetSchema, { resolvers: newResolvers ?? targetResolvers, - mocks: options.mocks, + scalars: options.scalars, }); }, From 5100f64faab6a606f8f931b2bda69941ed452bc5 Mon Sep 17 00:00:00 2001 From: alessbell Date: Fri, 12 Apr 2024 12:15:54 +0000 Subject: [PATCH 4/5] Clean up Prettier, Size-limit, and Api-Extractor --- .api-reports/api-report-testing.md | 6 +++--- .api-reports/api-report-testing_core.md | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/.api-reports/api-report-testing.md b/.api-reports/api-report-testing.md index 6ee0fe52a61..b85108ace73 100644 --- a/.api-reports/api-report-testing.md +++ b/.api-reports/api-report-testing.md @@ -1680,11 +1680,11 @@ interface TestSchemaFns { // @public (undocumented) interface TestSchemaOptions { // (undocumented) - mocks?: { + resolvers: Resolvers; + // (undocumented) + scalars?: { [key: string]: any; }; - // (undocumented) - resolvers: Resolvers; } // @public (undocumented) diff --git a/.api-reports/api-report-testing_core.md b/.api-reports/api-report-testing_core.md index aea8eb306d8..6199767a19f 100644 --- a/.api-reports/api-report-testing_core.md +++ b/.api-reports/api-report-testing_core.md @@ -1637,11 +1637,11 @@ interface TestSchemaFns { // @public (undocumented) interface TestSchemaOptions { // (undocumented) - mocks?: { + resolvers: Resolvers; + // (undocumented) + scalars?: { [key: string]: any; }; - // (undocumented) - resolvers: Resolvers; } // @public (undocumented) From 409a1924c1d60727cd3b3051c31fc9ab0c3ea737 Mon Sep 17 00:00:00 2001 From: Alessia Bellisario Date: Fri, 12 Apr 2024 08:17:18 -0400 Subject: [PATCH 5/5] chore: update doc block --- src/testing/core/createTestSchema.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/testing/core/createTestSchema.ts b/src/testing/core/createTestSchema.ts index 8e381136bf2..a294223440e 100644 --- a/src/testing/core/createTestSchema.ts +++ b/src/testing/core/createTestSchema.ts @@ -26,7 +26,7 @@ interface TestSchemaOptions { * resolvers to the original proxied schema. * * @param schema - A `GraphQLSchema`. - * @param options - An `options` object that accepts `mocks` and `resolvers` objects. + * @param options - An `options` object that accepts `scalars` and `resolvers` objects. * @returns A `ProxiedSchema` with `add`, `fork` and `reset` methods. * * @example @@ -40,7 +40,7 @@ interface TestSchemaOptions { }), } }, - mocks: { + scalars: { ID: () => "1", Int: () => 36, String: () => "String",