Skip to content

Commit

Permalink
Re-export all public graphql-tag exports
Browse files Browse the repository at this point in the history
Make sure we're re-exporting all public `graphql-tag` exports,
while ensuring that valid types exist.
  • Loading branch information
hwillson committed Jan 27, 2020
1 parent 9b38fc2 commit 61f4192
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/config/jest/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { disableFragmentWarnings } from 'graphql-tag';
import gql from 'graphql-tag';

// Turn off warnings for repeated fragment names
disableFragmentWarnings();
gql.disableFragmentWarnings();

process.on('unhandledRejection', () => {});
27 changes: 26 additions & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,29 @@ export { Observable } from '../utilities/observables/Observable';

/* Supporting */

export { default as gql } from 'graphql-tag';
// Note that importing `gql` by itself, then destructuring
// additional properties separately before exporting, is intentional.
// Due to the way the `graphql-tag` library is setup, certain bundlers
// can't find the properties added to the exported `gql` function without
// additional guidance (e.g. Rollup - see
// https://rollupjs.org/guide/en/#error-name-is-not-exported-by-module).
// Instead of having people that are using bundlers with `@apollo/client` add
// extra bundler config to help `graphql-tag` exports be found (which would be
// awkward since they aren't importing `graphql-tag` themselves), this
// workaround of pulling the extra properties off the `gql` function,
// then re-exporting them separately, helps keeps bundlers happy without any
// additional config changes.
import gql from 'graphql-tag';
const {
resetCaches,
disableFragmentWarnings,
enableExperimentalFragmentVariables,
disableExperimentalFragmentVariables
} = gql;
export {
gql,
resetCaches,
disableFragmentWarnings,
enableExperimentalFragmentVariables,
disableExperimentalFragmentVariables
};
16 changes: 16 additions & 0 deletions src/types/graphql-tag.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// The `graphql-tag` package currently ships with types that aren't quite
// representative of how the package is setup, and its exports are handled.
// This type file is intended to better reflect how the package is setup,
// but should be considered temporary. At some point `graphql-tag` will
// be fully updated to use Typescript, and these discrepancies will be fixed.

declare module 'graphql-tag' {
function gql(literals: any, ...placeholders: any[]): any;
namespace gql {
export function resetCaches(): void;
export function disableFragmentWarnings(): void;
export function enableExperimentalFragmentVariables(): void;
export function disableExperimentalFragmentVariables(): void;
}
export default gql;
}
6 changes: 5 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@
"esModuleInterop": true,
"outDir": "./dist",
"lib": ["es2015", "esnext.asynciterable", "dom"],
"jsx": "react"
"jsx": "react",
"typeRoots": [
"src/types",
"node_modules/@types"
]
},
"include": ["src/**/*.ts"],
"exclude": [
Expand Down

0 comments on commit 61f4192

Please sign in to comment.