Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating schema parser to more closely match current state of RFC #323

Merged
merged 1 commit into from
Mar 22, 2016
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
26 changes: 14 additions & 12 deletions src/language/ast.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ export type Document = {

export type Definition = OperationDefinition
| FragmentDefinition
| TypeDefinition
| TypeExtensionDefinition
| DirectiveDefinition
| TypeSystemDefinition

export type OperationDefinition = {
kind: 'OperationDefinition';
Expand Down Expand Up @@ -256,15 +254,25 @@ export type NonNullType = {
type: NamedType | ListType;
}

// Type Definition
// Type System Definition

export type TypeDefinition = ObjectTypeDefinition
export type TypeSystemDefinition = TypeDefinition
| TypeExtensionDefinition
| DirectiveDefinition

export type TypeDefinition = ScalarTypeDefinition
| ObjectTypeDefinition
| InterfaceTypeDefinition
| UnionTypeDefinition
| ScalarTypeDefinition
| EnumTypeDefinition
| InputObjectTypeDefinition

export type ScalarTypeDefinition = {
kind: 'ScalarTypeDefinition';
loc?: ?Location;
name: Name;
}

export type ObjectTypeDefinition = {
kind: 'ObjectTypeDefinition';
loc?: ?Location;
Expand Down Expand Up @@ -303,12 +311,6 @@ export type UnionTypeDefinition = {
types: Array<NamedType>;
}

export type ScalarTypeDefinition = {
kind: 'ScalarTypeDefinition';
loc?: ?Location;
name: Name;
}

export type EnumTypeDefinition = {
kind: 'EnumTypeDefinition';
loc?: ?Location;
Expand Down
4 changes: 3 additions & 1 deletion src/language/kinds.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,16 @@ export const NAMED_TYPE = 'NamedType';
export const LIST_TYPE = 'ListType';
export const NON_NULL_TYPE = 'NonNullType';

// Type System Definitions

// Type Definitions

export const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition';
export const OBJECT_TYPE_DEFINITION = 'ObjectTypeDefinition';
export const FIELD_DEFINITION = 'FieldDefinition';
export const INPUT_VALUE_DEFINITION = 'InputValueDefinition';
export const INTERFACE_TYPE_DEFINITION = 'InterfaceTypeDefinition';
export const UNION_TYPE_DEFINITION = 'UnionTypeDefinition';
export const SCALAR_TYPE_DEFINITION = 'ScalarTypeDefinition';
export const ENUM_TYPE_DEFINITION = 'EnumTypeDefinition';
export const ENUM_VALUE_DEFINITION = 'EnumValueDefinition';
export const INPUT_OBJECT_TYPE_DEFINITION = 'InputObjectTypeDefinition';
Expand Down
87 changes: 44 additions & 43 deletions src/language/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,14 @@ import type {
Type,
NamedType,

TypeDefinition,
TypeSystemDefinition,

ScalarTypeDefinition,
ObjectTypeDefinition,
FieldDefinition,
InputValueDefinition,
InterfaceTypeDefinition,
UnionTypeDefinition,
ScalarTypeDefinition,
EnumTypeDefinition,
EnumValueDefinition,
InputObjectTypeDefinition,
Expand Down Expand Up @@ -85,12 +86,12 @@ import {
LIST_TYPE,
NON_NULL_TYPE,

SCALAR_TYPE_DEFINITION,
OBJECT_TYPE_DEFINITION,
FIELD_DEFINITION,
INPUT_VALUE_DEFINITION,
INTERFACE_TYPE_DEFINITION,
UNION_TYPE_DEFINITION,
SCALAR_TYPE_DEFINITION,
ENUM_TYPE_DEFINITION,
ENUM_VALUE_DEFINITION,
INPUT_OBJECT_TYPE_DEFINITION,
Expand Down Expand Up @@ -185,8 +186,7 @@ function parseDocument(parser: Parser): Document {
* Definition :
* - OperationDefinition
* - FragmentDefinition
* - TypeDefinition
* - TypeExtensionDefinition
* - TypeSystemDefinition
*/
function parseDefinition(parser: Parser): Definition {
if (peek(parser, TokenKind.BRACE_L)) {
Expand All @@ -202,14 +202,15 @@ function parseDefinition(parser: Parser): Definition {

case 'fragment': return parseFragmentDefinition(parser);

// Note: the Type System IDL is an experimental non-spec addition.
case 'scalar':
case 'type':
case 'interface':
case 'union':
case 'scalar':
case 'enum':
case 'input': return parseTypeDefinition(parser);
case 'extend': return parseTypeExtensionDefinition(parser);
case 'directive': return parseDirectiveDefinition(parser);
case 'input':
case 'extend':
case 'directive': return parseTypeSystemDefinition(parser);
}
}

Expand Down Expand Up @@ -649,34 +650,48 @@ export function parseNamedType(parser: Parser): NamedType {
// Implements the parsing rules in the Type Definition section.

/**
* TypeSystemDefinition :
* - TypeDefinition
* - TypeExtensionDefinition
* - DirectiveDefinition
*
* TypeDefinition :
* - ScalarTypeDefinition
* - ObjectTypeDefinition
* - InterfaceTypeDefinition
* - UnionTypeDefinition
* - ScalarTypeDefinition
* - EnumTypeDefinition
* - InputObjectTypeDefinition
*/
function parseTypeDefinition(parser: Parser): TypeDefinition {
if (!peek(parser, TokenKind.NAME)) {
throw unexpected(parser);
}
switch (parser.token.value) {
case 'type':
return parseObjectTypeDefinition(parser);
case 'interface':
return parseInterfaceTypeDefinition(parser);
case 'union':
return parseUnionTypeDefinition(parser);
case 'scalar':
return parseScalarTypeDefinition(parser);
case 'enum':
return parseEnumTypeDefinition(parser);
case 'input':
return parseInputObjectTypeDefinition(parser);
default:
throw unexpected(parser);
function parseTypeSystemDefinition(parser: Parser): TypeSystemDefinition {
if (peek(parser, TokenKind.NAME)) {
switch (parser.token.value) {
case 'scalar': return parseScalarTypeDefinition(parser);
case 'type': return parseObjectTypeDefinition(parser);
case 'interface': return parseInterfaceTypeDefinition(parser);
case 'union': return parseUnionTypeDefinition(parser);
case 'enum': return parseEnumTypeDefinition(parser);
case 'input': return parseInputObjectTypeDefinition(parser);
case 'extend': return parseTypeExtensionDefinition(parser);
case 'directive': return parseDirectiveDefinition(parser);
}
}

throw unexpected(parser);
}

/**
* ScalarTypeDefinition : scalar Name
*/
function parseScalarTypeDefinition(parser: Parser): ScalarTypeDefinition {
const start = parser.token.start;
expectKeyword(parser, 'scalar');
const name = parseName(parser);
return {
kind: SCALAR_TYPE_DEFINITION,
name,
loc: loc(parser, start),
};
}

/**
Expand Down Expand Up @@ -816,20 +831,6 @@ function parseUnionMembers(parser: Parser): Array<NamedType> {
return members;
}

/**
* ScalarTypeDefinition : scalar Name
*/
function parseScalarTypeDefinition(parser: Parser): ScalarTypeDefinition {
const start = parser.token.start;
expectKeyword(parser, 'scalar');
const name = parseName(parser);
return {
kind: SCALAR_TYPE_DEFINITION,
name,
loc: loc(parser, start),
};
}

/**
* EnumTypeDefinition : enum Name { EnumValueDefinition+ }
*/
Expand Down
8 changes: 4 additions & 4 deletions src/language/printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,10 @@ const printDocASTReducer = {
ListType: ({ type }) => '[' + type + ']',
NonNullType: ({ type }) => type + '!',

// Type Definitions
// Type System Definitions

ScalarTypeDefinition: ({ name }) =>
`scalar ${name}`,

ObjectTypeDefinition: ({ name, interfaces, fields }) =>
'type ' + name + ' ' +
Expand All @@ -111,9 +114,6 @@ const printDocASTReducer = {
UnionTypeDefinition: ({ name, types }) =>
`union ${name} = ${join(types, ' | ')}`,

ScalarTypeDefinition: ({ name }) =>
`scalar ${name}`,

EnumTypeDefinition: ({ name, values }) =>
`enum ${name} ${block(values)}`,

Expand Down