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: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ export {

// Produce and format errors.
export { GraphQLError, formatError } from './error';

export { printSchema } from './language/schema/printer';
71 changes: 37 additions & 34 deletions src/language/schema/__tests__/materializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { parseSchema } from '../parser';
import { materializeSchema } from '../materializer';
import { printSchema } from '../../../type/printer';
import { getIntrospectionResult } from '../printer';
import { parseSchemaIntoAST } from '../parser';
import { printSchema } from '../';
import { materializeSchemaAST } from '../materializer';
import { createSchemaFromDSL } from '../';

// 80+ char lines are useful in describe/it, so ignore in this file.
/*eslint-disable max-len */

function printForTest(result) {
return '\n' + printSchema(result) + '\n';
}

async function getOutput(body, queryType) {
var result = await getIntrospectionResult(body, queryType);
return await printForTest(result);
/**
* This function does a full cycle of going from a
* string with the contents of the DSL, parsed
* in a schema AST, materializing that schema ast
* into an in-memory GraphQLSchema, and then finally
* printing that GraphQL into the DSL
*/
async function cycleOutput(body, queryType) {
var schema = await createSchemaFromDSL(body, queryType);
return '\n' + await printSchema(schema) + '\n';
}

describe('Schema Materializer', () => {
Expand All @@ -35,7 +38,7 @@ type HelloScalars {
bool: Boolean
}
`;
var output = await getOutput(body, 'HelloScalars');
var output = await cycleOutput(body, 'HelloScalars');
expect(output).to.equal(body);
});

Expand All @@ -49,7 +52,7 @@ type HelloScalars {
nonNullListOfNonNullStrs: [String!]!
}
`;
var output = await getOutput(body, 'HelloScalars');
var output = await cycleOutput(body, 'HelloScalars');
expect(output).to.equal(body);
});

Expand All @@ -61,7 +64,7 @@ type Recurse {
recurse: Recurse
}
`;
var output = await getOutput(body, 'Recurse');
var output = await cycleOutput(body, 'Recurse');
expect(output).to.equal(body);
});

Expand All @@ -77,7 +80,7 @@ type TypeTwo {
typeOne: TypeOne
}
`;
var output = await getOutput(body, 'TypeOne');
var output = await cycleOutput(body, 'TypeOne');
expect(output).to.equal(body);
});

Expand All @@ -87,7 +90,7 @@ type Hello {
str(int: Int): String
}
`;
var output = await getOutput(body, 'Hello');
var output = await cycleOutput(body, 'Hello');
expect(output).to.equal(body);
});

Expand All @@ -97,7 +100,7 @@ type Hello {
str(int: Int, bool: Boolean): String
}
`;
var output = await getOutput(body, 'Hello');
var output = await cycleOutput(body, 'Hello');
expect(output).to.equal(body);
});

Expand All @@ -111,7 +114,7 @@ interface WorldInterface {
str: String
}
`;
var output = await getOutput(body, 'HelloInterface');
var output = await cycleOutput(body, 'HelloInterface');
expect(output).to.equal(body);
});

Expand All @@ -125,7 +128,7 @@ type OutputEnumRoot {
hello: Hello
}
`;
var output = await getOutput(body, 'OutputEnumRoot');
var output = await cycleOutput(body, 'OutputEnumRoot');
expect(output).to.equal(body);
});

Expand All @@ -139,7 +142,7 @@ type InputEnumRoot {
str(hello: Hello): String
}
`;
var output = await getOutput(body, 'InputEnumRoot');
var output = await cycleOutput(body, 'InputEnumRoot');
expect(output).to.equal(body);
});

Expand All @@ -154,7 +157,7 @@ type OutputEnumRoot {
hello: Hello
}
`;
var output = await getOutput(body, 'OutputEnumRoot');
var output = await cycleOutput(body, 'OutputEnumRoot');
expect(output).to.equal(body);
});

Expand All @@ -170,7 +173,7 @@ type World {
str: String
}
`;
var output = await getOutput(body, 'Root');
var output = await cycleOutput(body, 'Root');
expect(output).to.equal(body);
});

Expand All @@ -190,7 +193,7 @@ type WorldTwo {
str: String
}
`;
var output = await getOutput(body, 'Root');
var output = await cycleOutput(body, 'Root');
expect(output).to.equal(body);
});

Expand All @@ -203,7 +206,7 @@ type Root {
}
`;

var output = await getOutput(body, 'Root');
var output = await cycleOutput(body, 'Root');
expect(output).to.equal(body);
});

Expand All @@ -218,7 +221,7 @@ type Root {
}
`;

var output = await getOutput(body, 'Root');
var output = await cycleOutput(body, 'Root');
expect(output).to.equal(body);
});

Expand All @@ -228,7 +231,7 @@ type Hello {
str(int: Int = 2): String
}
`;
var output = await getOutput(body, 'Hello');
var output = await cycleOutput(body, 'Hello');
expect(output).to.equal(body);
});
});
Expand All @@ -240,25 +243,25 @@ type Hello {
bar: Bar
}
`;
var doc = parseSchema(body);
expect(() => materializeSchema(doc, 'Hello')).to.throw('Type Bar not found in document');
var doc = parseSchemaIntoAST(body);
expect(() => materializeSchemaAST(doc, 'Hello')).to.throw('Type Bar not found in document');
});

it('Unknown type in interface list', () => {
var body = `
type Hello implements Bar { }
`;
var doc = parseSchema(body);
expect(() => materializeSchema(doc, 'Hello')).to.throw('Type Bar not found in document');
var doc = parseSchemaIntoAST(body);
expect(() => materializeSchemaAST(doc, 'Hello')).to.throw('Type Bar not found in document');
});

it('Unknown type in union list', () => {
var body = `
union TestUnion = Bar
type Hello { testUnion: TestUnion }
`;
var doc = parseSchema(body);
expect(() => materializeSchema(doc, 'Hello')).to.throw('Type Bar not found in document');
var doc = parseSchemaIntoAST(body);
expect(() => materializeSchemaAST(doc, 'Hello')).to.throw('Type Bar not found in document');
});


Expand All @@ -268,7 +271,7 @@ type Hello {
str: String
}
`;
var doc = parseSchema(body);
expect(() => materializeSchema(doc, 'Wat')).to.throw('Specified query type Wat not found in document');
var doc = parseSchemaIntoAST(body);
expect(() => materializeSchemaAST(doc, 'Wat')).to.throw('Specified query type Wat not found in document');
});
});
38 changes: 19 additions & 19 deletions src/language/schema/__tests__/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { expect } from 'chai';
import { describe, it } from 'mocha';
import { parseSchema } from '../parser';
import { parseSchemaIntoAST } from '../parser';

function createLocFn(body) {
return (start, end) => ({
Expand Down Expand Up @@ -79,7 +79,7 @@ describe('Schema Parser', () => {
type Hello {
world: String
}`;
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
Expand Down Expand Up @@ -109,7 +109,7 @@ type Hello {
world: String!
}`;
var loc = createLocFn(body);
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var expected = {
kind: 'SchemaDocument',
definitions: [
Expand Down Expand Up @@ -140,7 +140,7 @@ type Hello {
it('Simple type inheriting interface', () => {
var body = `type Hello implements World { }`;
var loc = createLocFn(body);
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var expected = {
kind: 'SchemaDocument',
definitions: [
Expand All @@ -160,7 +160,7 @@ type Hello {
it('Simple type inheriting multiple interfaces', () => {
var body = `type Hello implements Wo, rld { }`;
var loc = createLocFn(body);
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var expected = {
kind: 'SchemaDocument',
definitions: [
Expand All @@ -183,7 +183,7 @@ type Hello {
it('Single value enum', () => {
var body = `enum Hello { WORLD }`;
var loc = createLocFn(body);
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var expected = {
kind: 'SchemaDocument',
definitions: [
Expand All @@ -202,7 +202,7 @@ type Hello {
it('Double value enum', () => {
var body = `enum Hello { WO, RLD }`;
var loc = createLocFn(body);
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var expected = {
kind: 'SchemaDocument',
definitions: [
Expand All @@ -226,7 +226,7 @@ type Hello {
interface Hello {
world: String
}`;
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
Expand Down Expand Up @@ -254,7 +254,7 @@ interface Hello {
type Hello {
world(flag: Boolean): String
}`;
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
Expand Down Expand Up @@ -292,7 +292,7 @@ type Hello {
type Hello {
world(flag: Boolean = true): String
}`;
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
Expand Down Expand Up @@ -334,7 +334,7 @@ type Hello {
type Hello {
world(things: [String]): String
}`;
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
Expand Down Expand Up @@ -376,7 +376,7 @@ type Hello {
type Hello {
world(argOne: Boolean, argTwo: Int): String
}`;
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
Expand Down Expand Up @@ -418,7 +418,7 @@ type Hello {

it('Simple union', () => {
var body = `union Hello = World`;
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
Expand All @@ -437,7 +437,7 @@ type Hello {

it('Union with two types', () => {
var body = `union Hello = Wo | Rld`;
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
Expand All @@ -459,7 +459,7 @@ type Hello {

it('Scalar', () => {
var body = `scalar Hello`;
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
Expand All @@ -480,7 +480,7 @@ type Hello {
input Hello {
world: String
}`;
var doc = parseSchema(body);
var doc = parseSchemaIntoAST(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
Expand Down Expand Up @@ -508,16 +508,16 @@ input Hello {
input Hello {
world(foo: Int): String
}`;
expect(() => parseSchema(body)).to.throw('Error');
expect(() => parseSchemaIntoAST(body)).to.throw('Error');
});

it('Reject query keywords', () => {
var body = `query Foo { field }`;
expect(() => parseSchema(body)).to.throw('Error');
expect(() => parseSchemaIntoAST(body)).to.throw('Error');
});

it('Reject query shorthand', () => {
var body = `{ field }`;
expect(() => parseSchema(body)).to.throw('Error');
expect(() => parseSchemaIntoAST(body)).to.throw('Error');
});
});
Loading