Skip to content

Commit

Permalink
Remove mocking options (#6609)
Browse files Browse the repository at this point in the history
Mocking was a feature added to Apollo Server way back in the v1 days.
Its implementation was in the `graphql-tools` package, which at the time
was essentially part of Apollo Server but is now a separately maintained
project; the code now lives in `@graphql-tools/mock`. The mocking API
has changed over the years: they've added a lot of new options that
aren't accessible via Apollo Server's configuration, and the API we do
expose doesn't exactly match their docs (eg, mockEntireSchema vs
preserveResolvers).

Our philosophy these days is that Apollo Server doesn't need to bake in
hard-coded dependencies on specific versions of third-party packages
when it's just as easy for our users to use those packages directly. So
we're removing the `mock` and `mockEntireSchema` options from AS4. We'll
continue to document how to use these features by using
`@graphql-tools/mock` directly, but also encourage folks to read their
docs for more details.

Fixes #6063.
  • Loading branch information
glasser committed Jun 23, 2022
1 parent 9f365b8 commit f9799de
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 110 deletions.
6 changes: 6 additions & 0 deletions WIP/migration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ To simplify the Apollo Server 4 API, we removed the `modules` constructor option

Apollo Server 4 drops support for [`@apollo/gateway` versions](/federation/api/apollo-gateway/) below v0.35.0.

### Removed mocking

`mocks` and `mockEntireSchema` constructor options removed. These were a thin
layer around a subset of the functionality of a hard-coded version of
`@graphql-tools/mocks`, which can be used directly (TODO: show migration).

#### Dropping Redundant Gateway TypeScript types

In Apollo Server 2, the TypeScript type used for the `gateway` constructor option is called `GraphQLService`. Apollo Server 3 introduced a new TypeScript type called `GatewayInterface`.
Expand Down
10 changes: 6 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
"@apollo/utils.usagereporting": "^1.0.0",
"@apollo/utils.withrequired": "^1.0.0",
"@apollographql/graphql-playground-html": "1.6.29",
"@graphql-tools/mock": "^8.1.2",
"@graphql-tools/schema": "^8.0.0",
"@josephg/resolvable": "^1.0.0",
"@types/express-serve-static-core": "4.17.29",
Expand Down
25 changes: 1 addition & 24 deletions packages/server/src/ApolloServer.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { isNodeLike } from '@apollo/utils.isnodelike';
import type { Logger } from '@apollo/utils.logger';
import { addMocksToSchema } from '@graphql-tools/mock';
import { makeExecutableSchema } from '@graphql-tools/schema';
import resolvable, { Resolvable } from '@josephg/resolvable';
import {
Expand Down Expand Up @@ -262,10 +261,7 @@ export class ApolloServer<TContext extends BaseContext = BaseContext> {
{
phase: 'initialized',
schemaManager: new SchemaManager({
apiSchema: ApolloServer.maybeAddMocksToConstructedSchema(
ApolloServer.constructSchema(config),
config,
),
apiSchema: ApolloServer.constructSchema(config),
schemaDerivedDataProvider: (schema) =>
ApolloServer.generateSchemaDerivedData(
schema,
Expand Down Expand Up @@ -703,25 +699,6 @@ export class ApolloServer<TContext extends BaseContext = BaseContext> {
});
}

private static maybeAddMocksToConstructedSchema<TContext extends BaseContext>(
schema: GraphQLSchema,
config: ApolloServerOptionsWithStaticSchema<TContext>,
): GraphQLSchema {
const { mocks, mockEntireSchema } = config;
if (mocks === false) {
return schema;
}
if (!mocks && typeof mockEntireSchema === 'undefined') {
return schema;
}
return addMocksToSchema({
schema,
mocks: mocks === true || typeof mocks === 'undefined' ? {} : mocks,
preserveResolvers:
typeof mockEntireSchema === 'undefined' ? false : !mockEntireSchema,
});
}

private static generateSchemaDerivedData(
schema: GraphQLSchema,
// null means don't use a documentStore at all.
Expand Down
73 changes: 0 additions & 73 deletions packages/server/src/__tests__/integration/apolloServerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,79 +511,6 @@ export function defineIntegrationTestSuiteApolloServerTests(
);
}
});

it('allows mocks as boolean', async () => {
const typeDefs = gql`
type Query {
hello: String
}
`;
const uri = await createServerAndGetUrl({
typeDefs,
mocks: true,
});

const apolloFetch = createApolloFetch({ uri });
const result = await apolloFetch({ query: '{hello}' });
expect(result.data).toEqual({ hello: 'Hello World' });
expect(result.errors).toBeUndefined();
});

it('allows mocks as an object', async () => {
const typeDefs = gql`
type Query {
hello: String
}
`;
const uri = await createServerAndGetUrl({
typeDefs,
mocks: { String: () => 'mock city' },
});

const apolloFetch = createApolloFetch({ uri });
const result = await apolloFetch({ query: '{hello}' });

expect(result.data).toEqual({ hello: 'mock city' });
expect(result.errors).toBeUndefined();
});

it('allows mocks as an object without overriding the existing resolvers', async () => {
const typeDefs = gql`
type User {
first: String
last: String
}
type Query {
user: User
}
`;
const resolvers = {
Query: {
user: () => ({
first: 'James',
last: 'Heinlein',
}),
},
};
const uri = await createServerAndGetUrl({
typeDefs,
resolvers,
mocks: {
User: () => ({
last: () => 'mock city',
}),
},
});

const apolloFetch = createApolloFetch({ uri });
const result = await apolloFetch({
query: '{user{first last}}',
});
expect(result.data).toEqual({
user: { first: 'Hello World', last: 'mock city' },
});
expect(result.errors).toBeUndefined();
});
});
});

Expand Down
10 changes: 2 additions & 8 deletions packages/server/src/externalTypes/constructor.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Logger } from '@apollo/utils.logger';
import type { IMocks } from '@graphql-tools/mock';
import type { IExecutableSchemaDefinition } from '@graphql-tools/schema';
import type {
DocumentNode,
Expand Down Expand Up @@ -148,13 +147,8 @@ interface ApolloServerOptionsWithTypeDefs<TContext extends BaseContext>

// Used internally in ApolloServer.ts but not publicly exported.
export type ApolloServerOptionsWithStaticSchema<TContext extends BaseContext> =
(
| ApolloServerOptionsWithSchema<TContext>
| ApolloServerOptionsWithTypeDefs<TContext>
) & {
mocks?: boolean | IMocks;
mockEntireSchema?: boolean;
};
| ApolloServerOptionsWithSchema<TContext>
| ApolloServerOptionsWithTypeDefs<TContext>;

export type ApolloServerOptions<TContext extends BaseContext> =
| ApolloServerOptionsWithGateway<TContext>
Expand Down

0 comments on commit f9799de

Please sign in to comment.