This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid bundling graphql/execution/execute into apollo-link-schema. (#968)
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
Showing
5 changed files
with
78 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters