Skip to content

Commit

Permalink
Merge branch 'master' into sogko/master
Browse files Browse the repository at this point in the history
* master: (26 commits)
  0.6.0
  Validation: improving overlapping fields quality (graphql#386)
  Validation: context.getFragmentSpreads now accepts selectionSet rather than fragment AST node
  Factor out more closure functions
  Factor out closure functions to normal functions
  Deprecated directive (graphql#384)
  RFC: Directive location: schema definition (graphql#382)
  RFC: Schema Language Directives (graphql#376)
  Export introspection in public API
  Export directive definitions. (graphql#381)
  BUG: Ensure building AST schema does not exclude @Skip and @include (graphql#380)
  documentation of schema constructor
  Revert "Remove all 'instanceof GraphQLSchema' checks" (graphql#377)
  remove all 'instanceof GraphQLSchema' checks (graphql#371)
  Error logging for new interface type semantics (graphql#350)
  Nit: Missing case in grammar for TypeSystemDefinition in comment
  Bug: printer can print non-parsable value
  Factor out suggestion quoting utility
  Minor refactoring
  Minor refactoring of error messages for unknown fields
  ...
  • Loading branch information
sogko committed Jun 1, 2016
2 parents d29694c + 359ec76 commit 2e619ab
Show file tree
Hide file tree
Showing 39 changed files with 2,121 additions and 466 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql",
"version": "0.5.0",
"version": "0.6.0",
"description": "A Query Language and Runtime which can target any service.",
"contributors": [
"Lee Byron <lee@leebyron.com> (http://leebyron.com/)",
Expand Down
12 changes: 0 additions & 12 deletions src/execution/__tests__/directives-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,6 @@ describe('Execute: handles directives', () => {
b
}
}
fragment Frag on TestType {
b
}
`;
return expect(await executeTestQuery(q)).to.deep.equal({
data: { a: 'a' }
Expand All @@ -170,9 +167,6 @@ describe('Execute: handles directives', () => {
b
}
}
fragment Frag on TestType {
b
}
`;
return expect(await executeTestQuery(q)).to.deep.equal({
data: { a: 'a', b: 'b' }
Expand All @@ -186,9 +180,6 @@ describe('Execute: handles directives', () => {
b
}
}
fragment Frag on TestType {
b
}
`;
return expect(await executeTestQuery(q)).to.deep.equal({
data: { a: 'a', b: 'b' }
Expand All @@ -202,9 +193,6 @@ describe('Execute: handles directives', () => {
b
}
}
fragment Frag on TestType {
b
}
`;
return expect(await executeTestQuery(q)).to.deep.equal({
data: { a: 'a' }
Expand Down
2 changes: 1 addition & 1 deletion src/execution/values.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function getVariableValues(
export function getArgumentValues(
argDefs: ?Array<GraphQLArgument>,
argASTs: ?Array<Argument>,
variableValues: { [key: string]: mixed }
variableValues?: ?{ [key: string]: mixed }
): { [key: string]: mixed } {
if (!argDefs || !argASTs) {
return {};
Expand Down
31 changes: 31 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export {
GraphQLInputObjectType,
GraphQLList,
GraphQLNonNull,
GraphQLDirective,

// "Enum" of Type Kinds
TypeKind,

// "Enum" of Directive Locations
DirectiveLocation,

// Scalars
GraphQLInt,
Expand All @@ -59,6 +66,30 @@ export {
GraphQLBoolean,
GraphQLID,

// Built-in Directives defined by the Spec
specifiedDirectives,
GraphQLIncludeDirective,
GraphQLSkipDirective,
GraphQLDeprecatedDirective,

// Constant Deprecation Reason
DEFAULT_DEPRECATION_REASON,

// Meta-field definitions.
SchemaMetaFieldDef,
TypeMetaFieldDef,
TypeNameMetaFieldDef,

// GraphQL Types for introspection.
__Schema,
__Directive,
__DirectiveLocation,
__Type,
__Field,
__InputValue,
__EnumValue,
__TypeKind,

// Predicates
isType,
isInputType,
Expand Down
40 changes: 40 additions & 0 deletions src/jsutils/__tests__/quotedOrList-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';
import quotedOrList from '../quotedOrList';

describe('quotedOrList', () => {

it('Does not accept an empty list', () => {
expect(() => quotedOrList([])).to.throw(TypeError);
});

it('Returns single quoted item', () => {
expect(quotedOrList([ 'A' ])).to.equal('"A"');
});

it('Returns two item list', () => {
expect(quotedOrList([ 'A', 'B' ])).to.equal('"A" or "B"');
});

it('Returns comma separated many item list', () => {
expect(quotedOrList([ 'A', 'B', 'C' ])).to.equal('"A", "B", or "C"');
});

it('Limits to five items', () => {
expect(
quotedOrList([ 'A', 'B', 'C', 'D', 'E', 'F' ])
).to.equal(
'"A", "B", "C", "D", or "E"'
);
});

});
28 changes: 28 additions & 0 deletions src/jsutils/__tests__/suggestionList-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

import { expect } from 'chai';
import { describe, it } from 'mocha';
import suggestionList from '../suggestionList';

describe('suggestionList', () => {

it('Returns results when input is empty', () => {
expect(suggestionList('', [ 'a' ])).to.deep.equal([ 'a' ]);
});

it('Returns empty array when there are no options', () => {
expect(suggestionList('input', [])).to.deep.equal([]);
});

it('Returns options sorted based on similarity', () => {
expect(suggestionList('abc', [ 'a', 'ab', 'abc' ]))
.to.deep.equal([ 'abc', 'ab' ]);
});
});
26 changes: 26 additions & 0 deletions src/jsutils/quotedOrList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

const MAX_LENGTH = 5;

/**
* Given [ A, B, C ] return '"A", "B", or "C"'.
*/
export default function quotedOrList(items: Array<string>): string {
const selected = items.slice(0, MAX_LENGTH);
return selected
.map(item => `"${item}"`)
.reduce((list, quoted, index) =>
list +
(selected.length > 2 ? ', ' : ' ') +
(index === selected.length - 1 ? 'or ' : '') +
quoted
);
}
82 changes: 82 additions & 0 deletions src/jsutils/suggestionList.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/

/**
* Given an invalid input string and a list of valid options, returns a filtered
* list of valid options sorted based on their similarity with the input.
*/
export default function suggestionList(
input: string,
options: Array<string>
): Array<string> {
const optionsByDistance = Object.create(null);
const oLength = options.length;
const inputThreshold = input.length / 2;
for (let i = 0; i < oLength; i++) {
const distance = lexicalDistance(input, options[i]);
const threshold = Math.max(inputThreshold, options[i].length / 2, 1);
if (distance <= threshold) {
optionsByDistance[options[i]] = distance;
}
}
return Object.keys(optionsByDistance).sort(
(a , b) => optionsByDistance[a] - optionsByDistance[b]
);
}

/**
* Computes the lexical distance between strings A and B.
*
* The "distance" between two strings is given by counting the minimum number
* of edits needed to transform string A into string B. An edit can be an
* insertion, deletion, or substitution of a single character, or a swap of two
* adjacent characters.
*
* This distance can be useful for detecting typos in input or sorting
*
* @param {string} a
* @param {string} b
* @return {int} distance in number of edits
*/
function lexicalDistance(a, b) {
let i;
let j;
const d = [];
const aLength = a.length;
const bLength = b.length;

for (i = 0; i <= aLength; i++) {
d[i] = [ i ];
}

for (j = 1; j <= bLength; j++) {
d[0][j] = j;
}

for (i = 1; i <= aLength; i++) {
for (j = 1; j <= bLength; j++) {
const cost = a[i - 1] === b[j - 1] ? 0 : 1;

d[i][j] = Math.min(
d[i - 1][j] + 1,
d[i][j - 1] + 1,
d[i - 1][j - 1] + cost
);

if (i > 1 && j > 1 &&
a[i - 1] === b[j - 2] &&
a[i - 2] === b[j - 1]) {
d[i][j] = Math.min(d[i][j], d[i - 2][j - 2] + cost);
}
}
}

return d[aLength][bLength];
}
25 changes: 25 additions & 0 deletions src/language/__tests__/schema-kitchen-sink.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -19,29 +19,54 @@ type Foo implements Bar {
six(argument: InputType = {key: "value"}): Type
}

type AnnotatedObject @onObject(arg: "value") {
annotatedField(arg: Type = "default" @onArg): Type @onField
}

interface Bar {
one: Type
four(argument: String = "string"): String
}

interface AnnotatedInterface @onInterface {
annotatedField(arg: Type @onArg): Type @onField
}

union Feed = Story | Article | Advert

union AnnotatedUnion @onUnion = A | B

scalar CustomScalar

scalar AnnotatedScalar @onScalar

enum Site {
DESKTOP
MOBILE
}

enum AnnotatedEnum @onEnum {
ANNOTATED_VALUE @onEnumValue
OTHER_VALUE
}

input InputType {
key: String!
answer: Int = 42
}

input AnnotatedInput @onInputObjectType {
annotatedField: Type @onField
}

extend type Foo {
seven(argument: [String]): Type
}

extend type Foo @onType {}

type NoFields {}

directive @skip(if: Boolean!) on FIELD | FRAGMENT_SPREAD | INLINE_FRAGMENT

directive @include(if: Boolean!)
Expand Down
Loading

0 comments on commit 2e619ab

Please sign in to comment.