Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Avoid bundling graphql/execution/execute into apollo-link-schema. (#968)
Browse files Browse the repository at this point in the history
Importing graphql/execution/execute adds about 30KB to the minified
footprint of a typical application (6.8KB after gzip). This estimate was
obtained using the apollographql/react-apollo#2839
example application, which is bundled with Rollup.

This is a significant pill to swallow under any circumstances, but before
this commit apollo-link-schema was bundling all of that code into itself,
preventing other packages from sharing the execution code, which very
likely led to its duplication in some applications.

With this commit, the self-size of apollo-link-schema falls from 19.3 KB
to just 398 bytes (minified+gzip), which means we can finally stop
excluding it from the filesize check! 🙈
  • Loading branch information
benjamn authored Mar 5, 2019
1 parent 70cb667 commit 3ecd543
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 72 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@
"path": "./packages/apollo-link-retry/lib/bundle.min.js",
"maxSize": "1.15 Kb"
},
{
"name": "apollo-link-schema",
"path": "./packages/apollo-link-schema/lib/bundle.min.js",
"maxSize": "400 B"
},
{
"name": "apollo-link-ws",
"path": "./packages/apollo-link-ws/lib/bundle.min.js",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-link-schema/src/__tests__/schemaLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { print, graphql } from 'graphql';
import { makeExecutableSchema } from 'graphql-tools';
import gql from 'graphql-tag';

import { SchemaLink } from '../schemaLink';
import { SchemaLink } from '../';

const sampleQuery = gql`
query SampleQuery {
Expand Down
71 changes: 70 additions & 1 deletion packages/apollo-link-schema/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
export * from './schemaLink';
import { ApolloLink, Operation, FetchResult, Observable } from 'apollo-link';
import { execute } from 'graphql/execution/execute';
import { GraphQLSchema } from 'graphql/type/schema';

export namespace SchemaLink {
export type ResolverContextFunction = (
operation: Operation,
) => Record<string, any>;

export interface Options {
/**
* The schema to generate responses from.
*/
schema: GraphQLSchema;

/**
* The root value to use when generating responses.
*/
rootValue?: any;

/**
* A context to provide to resolvers declared within the schema.
*/
context?: ResolverContextFunction | Record<string, any>;
}
}

export class SchemaLink extends ApolloLink {
public schema: GraphQLSchema;
public rootValue: any;
public context: SchemaLink.ResolverContextFunction | any;

constructor({ schema, rootValue, context }: SchemaLink.Options) {
super();

this.schema = schema;
this.rootValue = rootValue;
this.context = context;
}

public request(operation: Operation): Observable<FetchResult> | null {
return new Observable<FetchResult>(observer => {
Promise.resolve(
execute(
this.schema,
operation.query,
this.rootValue,
typeof this.context === 'function'
? this.context(operation)
: this.context,
operation.variables,
operation.operationName,
),
)
.then(data => {
if (!observer.closed) {
observer.next(data);
observer.complete();
}
})
.catch(error => {
if (!observer.closed) {
observer.error(error);
}
});
});
}
}

export default SchemaLink;
69 changes: 0 additions & 69 deletions packages/apollo-link-schema/src/schemaLink.ts

This file was deleted.

3 changes: 2 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export const globals = {
'subscriptions-transport-ws': 'subscriptions-transport-ws',

//GraphQL
'graphql/language/printer': 'printer',
'graphql/language/printer': 'graphql.printer',
'graphql/execution/execute': 'graphql.execute',

// TypeScript
'tslib': 'tslib',
Expand Down

0 comments on commit 3ecd543

Please sign in to comment.