Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ function parseVariableValue(parser): Value {
return parseValue(parser, false);
}

function parseConstValue(parser): Value {
export function parseConstValue(parser): Value {
return parseValue(parser, true);
}

Expand Down
96 changes: 71 additions & 25 deletions src/language/schema/__tests__/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@ function fieldNodeWithArgs(name, type, args, loc) {
return {
kind: 'FieldDefinition',
name: name,
type: type,
arguments: args,
type: type,
loc: loc,
};
}

function enumValueNode(name, loc) {
return {
kind: 'EnumValueDefinition',
name: nameNode(name, loc),
loc: loc,
};
}
Expand Down Expand Up @@ -92,7 +100,7 @@ type Hello {
],
loc: loc(1, 31),
};
expect(printJson(expected)).to.equal(printJson(doc));
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Simple non-null type', () => {
Expand Down Expand Up @@ -125,7 +133,7 @@ type Hello {
],
loc: loc(1, 32),
};
expect(printJson(expected)).to.equal(printJson(doc));
expect(printJson(doc)).to.equal(printJson(expected));
});


Expand Down Expand Up @@ -172,14 +180,6 @@ type Hello {
expect(printJson(doc)).to.equal(printJson(expected));
});

function enumValueNode(name, loc) {
return {
kind: 'EnumValueDefinition',
name: nameNode(name, loc),
loc: loc,
};
}

it('Single value enum', () => {
var body = `enum Hello { WORLD }`;
var loc = createLocFn(body);
Expand Down Expand Up @@ -246,7 +246,7 @@ interface Hello {
],
loc: loc(1, 36),
};
expect(printJson(expected)).to.equal(printJson(doc));
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Simple field with arg', () => {
Expand All @@ -272,6 +272,7 @@ type Hello {
kind: 'ArgumentDefinition',
name: nameNode('flag', loc(22, 26)),
type: typeNode('Boolean', loc(28, 35)),
defaultValue: null,
loc: loc(22, 35),
}
],
Expand All @@ -283,7 +284,49 @@ type Hello {
],
loc: loc(1, 46),
};
expect(printJson(expected)).to.equal(printJson(doc));
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Simple field with arg with default value', () => {
var body = `
type Hello {
world(flag: Boolean = true): String
}`;
var doc = parseSchema(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
definitions: [
{
kind: 'TypeDefinition',
name: nameNode('Hello', loc(6, 11)),
interfaces: [],
fields: [
fieldNodeWithArgs(
nameNode('world', loc(16, 21)),
typeNode('String', loc(45, 51)),
[
{
kind: 'ArgumentDefinition',
name: nameNode('flag', loc(22, 26)),
type: typeNode('Boolean', loc(28, 35)),
defaultValue: {
kind: 'BooleanValue',
value: true,
loc: loc(38, 42),
},
loc: loc(22, 42),
}
],
loc(16, 51)
)
],
loc: loc(1, 53),
}
],
loc: loc(1, 53),
};
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Simple field with list arg', () => {
Expand Down Expand Up @@ -313,6 +356,7 @@ type Hello {
type: typeNode('String', loc(31, 37)),
loc: loc(30, 38)
},
defaultValue: null,
loc: loc(22, 38),
}
],
Expand All @@ -324,7 +368,7 @@ type Hello {
],
loc: loc(1, 49),
};
expect(printJson(expected)).to.equal(printJson(doc));
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Simple field with two args', () => {
Expand All @@ -350,12 +394,14 @@ type Hello {
kind: 'ArgumentDefinition',
name: nameNode('argOne', loc(22, 28)),
type: typeNode('Boolean', loc(30, 37)),
defaultValue: null,
loc: loc(22, 37),
},
{
kind: 'ArgumentDefinition',
name: nameNode('argTwo', loc(39, 45)),
type: typeNode('Int', loc(47, 50)),
defaultValue: null,
loc: loc(39, 50),
},
],
Expand All @@ -367,11 +413,11 @@ type Hello {
],
loc: loc(1, 61),
};
expect(printJson(expected)).to.equal(printJson(doc));
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Simple union', () => {
var body = `union Hello { World }`;
var body = `union Hello = World`;
var doc = parseSchema(body);
var loc = createLocFn(body);
var expected = {
Expand All @@ -381,16 +427,16 @@ type Hello {
kind: 'UnionDefinition',
name: nameNode('Hello', loc(6, 11)),
types: [typeNode('World', loc(14, 19))],
loc: loc(0, 21),
loc: loc(0, 19),
}
],
loc: loc(0, 21),
loc: loc(0, 19),
};
expect(printJson(expected)).to.equal(printJson(doc));
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Union with two types', () => {
var body = `union Hello { Wo | Rld }`;
var body = `union Hello = Wo | Rld`;
var doc = parseSchema(body);
var loc = createLocFn(body);
var expected = {
Expand All @@ -403,12 +449,12 @@ type Hello {
typeNode('Wo', loc(14, 16)),
typeNode('Rld', loc(19, 22)),
],
loc: loc(0, 24),
loc: loc(0, 22),
}
],
loc: loc(0, 24),
loc: loc(0, 22),
};
expect(printJson(expected)).to.equal(printJson(doc));
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Scalar', () => {
Expand All @@ -426,7 +472,7 @@ type Hello {
],
loc: loc(0, 12),
};
expect(printJson(expected)).to.equal(printJson(doc));
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Simple input object', () => {
Expand Down Expand Up @@ -454,7 +500,7 @@ input Hello {
],
loc: loc(1, 32),
};
expect(printJson(expected)).to.equal(printJson(doc));
expect(printJson(doc)).to.equal(printJson(expected));
});

it('Simple input object with args should fail', () => {
Expand Down
4 changes: 3 additions & 1 deletion src/language/schema/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import type {
Location,
Name,
Value,
Type,
NamedType
} from '../ast';
Expand Down Expand Up @@ -41,15 +42,16 @@ export type FieldDefinition = {
kind: 'FieldDefinition';
loc?: ?Location;
name: Name;
type: Type;
arguments: Array<ArgumentDefinition>;
type: Type;
}

export type ArgumentDefinition = {
kind: 'ArgumentDefinition';
loc?: ?Location;
name: Name;
type: Type;
defaultValue?: ?Value;
}

export type InterfaceDefinition = {
Expand Down
Loading