Skip to content

Commit

Permalink
GraphQL 11 should work too
Browse files Browse the repository at this point in the history
  • Loading branch information
freiksenet committed Dec 18, 2017
1 parent 177099c commit 6f66a95
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 11 deletions.
4 changes: 0 additions & 4 deletions src/declarations.d.ts

This file was deleted.

63 changes: 57 additions & 6 deletions src/stitching/typeFromAST.ts
Expand Up @@ -23,15 +23,11 @@ import {
UnionTypeDefinitionNode,
valueFromAST,
} from 'graphql';

//
// TODO put back import once PR is merged
// https://github.com/graphql/graphql-js/pull/1165
// import { getDescription } from 'graphql/utilities/buildASTSchema';

// TODO remove import once PR is merged
// https://github.com/graphql/graphql-js/pull/1165
import blockStringValue from 'graphql/language/blockStringValue';

const backcompatOptions = { commentDescriptions: true };

import resolveFromParentType from './resolveFromParentTypename';
Expand Down Expand Up @@ -190,7 +186,6 @@ function resolveType(typeRegistry: TypeRegistry, node: TypeNode): GraphQLType {
}
}


// Code below temporarily copied from graphql/graphql-js pending PR
// https://github.com/graphql/graphql-js/pull/1165

Expand Down Expand Up @@ -248,3 +243,59 @@ function getLeadingCommentBlock(node: any): void | string {
}
return comments.reverse().join('\n');
}

/**
* Produces the value of a block string from its parsed raw value, similar to
* Coffeescript's block string, Python's docstring trim or Ruby's strip_heredoc.
*
* This implements the GraphQL spec's BlockStringValue() static algorithm.
*/
function blockStringValue(rawString: string): string {
// Expand a block string's raw value into independent lines.
const lines = rawString.split(/\r\n|[\n\r]/g);

// Remove common indentation from all lines but first.
let commonIndent = null;
for (let i = 1; i < lines.length; i++) {
const line = lines[i];
const indent = leadingWhitespace(line);
if (
indent < line.length &&
(commonIndent === null || indent < commonIndent)
) {
commonIndent = indent;
if (commonIndent === 0) {
break;
}
}
}

if (commonIndent) {
for (let i = 1; i < lines.length; i++) {
lines[i] = lines[i].slice(commonIndent);
}
}

// Remove leading and trailing blank lines.
while (lines.length > 0 && isBlank(lines[0])) {
lines.shift();
}
while (lines.length > 0 && isBlank(lines[lines.length - 1])) {
lines.pop();
}

// Return a string of the lines joined with U+000A.
return lines.join('\n');
}

function leadingWhitespace(str: string): number {
let i = 0;
while (i < str.length && (str[i] === ' ' || str[i] === '\t')) {
i++;
}
return i;
}

function isBlank(str: string): boolean {
return leadingWhitespace(str) === str.length;
}
7 changes: 6 additions & 1 deletion src/test/testMergeSchemas.ts
Expand Up @@ -69,6 +69,11 @@ type Query {
}
`;

let graphql11compat = '';
if (process.env.GRAPHQL_VERSION === '^0.11') {
graphql11compat = '{}';
}

const linkSchema = `
# A new type linking the Property type.
type LinkType {
Expand Down Expand Up @@ -104,7 +109,7 @@ const linkSchema = `
nodes: [Node]
}
extend type Customer implements Node
extend type Customer implements Node ${graphql11compat}
`;

testCombinations.forEach(async combination => {
Expand Down

0 comments on commit 6f66a95

Please sign in to comment.