Skip to content

Commit

Permalink
Clean up gql.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Dec 11, 2020
1 parent 5c6ef7f commit 0e0ea88
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions packages/core/src/gql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ const applyDefinitions = (
const name = (source[i] as FragmentDefinitionNode).name.value;
const value = stringifyDocument(source[i]);
// Fragments will be deduplicated according to this Map
const prevValue = fragmentNames.get(name);
if (prevValue === undefined) {
if (!fragmentNames.has(name)) {
fragmentNames.set(name, value);
target.push(source[i]);
} else if (process.env.NODE_ENV !== 'production' && prevValue !== value) {
} else if (
process.env.NODE_ENV !== 'production' &&
fragmentNames.get(name) !== value
) {
// Fragments with the same names is expected to have the same contents
console.warn(
'[WARNING: Duplicate Fragment] A fragment with name `' +
Expand All @@ -51,16 +53,16 @@ function gql<Data = any, Variables = object>(
function gql(/* arguments */) {
const fragmentNames = new Map<string, string>();
const definitions: DefinitionNode[] = [];
const interpolations: DocumentNode[] = [];
const interpolations: DefinitionNode[] = [];

// Apply the entire tagged template body's definitions
let body: string = Array.isArray(arguments[0])
? arguments[0][0]
: arguments[0] || '';
for (let i = 1; i < arguments.length; i++) {
const value = arguments[i];
if (value && value.kind === Kind.DOCUMENT) {
interpolations.push(value);
if (value && value.definitions) {
interpolations.push(...value.definitions);
} else {
body += value;
}
Expand All @@ -70,10 +72,8 @@ function gql(/* arguments */) {

// Apply the tag's body definitions
applyDefinitions(fragmentNames, definitions, keyDocument(body).definitions);

// Copy over each interpolated document's definitions
for (let i = 0; i < interpolations.length; i++)
applyDefinitions(fragmentNames, definitions, interpolations[i].definitions);
applyDefinitions(fragmentNames, definitions, interpolations);

return keyDocument({
kind: Kind.DOCUMENT,
Expand Down

0 comments on commit 0e0ea88

Please sign in to comment.