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

[RFC] Expand GraphQL language #163

Merged
merged 1 commit into from
Aug 31, 2015
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/execution/__tests__/executor.js
Original file line number Diff line number Diff line change
Expand Up @@ -647,4 +647,32 @@ describe('Execute: Handles basic execution tasks', () => {
]);
});

it('fails to execute a query containing a type definition', async () => {
var query = parse(`
{ foo }

type Query { foo: String }
`);

var schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
foo: { type: GraphQLString }
}
})
});

var caughtError;
try {
await execute(schema, query);
} catch (error) {
caughtError = error;
}

expect(caughtError).to.deep.equal({
message: 'GraphQL cannot execute a request containing a ObjectDefinition.'
});
});

});
4 changes: 4 additions & 0 deletions src/execution/execute.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ function buildExecutionContext(
case Kind.FRAGMENT_DEFINITION:
fragments[statement.name.value] = statement;
break;
default: throw new GraphQLError(
`GraphQL cannot execute a request containing a ${statement.kind}.`,
statement
);
}
});
if (!operationName && Object.keys(operations).length !== 1) {
Expand Down
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 { parseSchemaIntoAST } from '../parser';
import { parse } from '../parser';

function createLocFn(body) {
return (start, end) => ({
Expand Down Expand Up @@ -80,13 +80,13 @@ describe('Schema Parser', () => {
type Hello {
world: String
}`;
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'TypeDefinition',
kind: 'ObjectDefinition',
name: nameNode('Hello', loc(6, 11)),
interfaces: [],
fields: [
Expand All @@ -110,12 +110,12 @@ type Hello {
world: String!
}`;
var loc = createLocFn(body);
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'TypeDefinition',
kind: 'ObjectDefinition',
name: nameNode('Hello', loc(6, 11)),
interfaces: [],
fields: [
Expand All @@ -141,12 +141,12 @@ type Hello {
it('Simple type inheriting interface', () => {
var body = `type Hello implements World { }`;
var loc = createLocFn(body);
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'TypeDefinition',
kind: 'ObjectDefinition',
name: nameNode('Hello', loc(5, 10)),
interfaces: [ typeNode('World', loc(22, 27)) ],
fields: [],
Expand All @@ -161,12 +161,12 @@ type Hello {
it('Simple type inheriting multiple interfaces', () => {
var body = `type Hello implements Wo, rld { }`;
var loc = createLocFn(body);
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'TypeDefinition',
kind: 'ObjectDefinition',
name: nameNode('Hello', loc(5, 10)),
interfaces: [
typeNode('Wo', loc(22, 24)),
Expand All @@ -184,9 +184,9 @@ type Hello {
it('Single value enum', () => {
var body = `enum Hello { WORLD }`;
var loc = createLocFn(body);
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'EnumDefinition',
Expand All @@ -203,9 +203,9 @@ type Hello {
it('Double value enum', () => {
var body = `enum Hello { WO, RLD }`;
var loc = createLocFn(body);
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'EnumDefinition',
Expand All @@ -227,10 +227,10 @@ type Hello {
interface Hello {
world: String
}`;
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'InterfaceDefinition',
Expand All @@ -255,13 +255,13 @@ interface Hello {
type Hello {
world(flag: Boolean): String
}`;
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'TypeDefinition',
kind: 'ObjectDefinition',
name: nameNode('Hello', loc(6, 11)),
interfaces: [],
fields: [
Expand Down Expand Up @@ -292,13 +292,13 @@ type Hello {
type Hello {
world(flag: Boolean = true): String
}`;
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'TypeDefinition',
kind: 'ObjectDefinition',
name: nameNode('Hello', loc(6, 11)),
interfaces: [],
fields: [
Expand Down Expand Up @@ -333,13 +333,13 @@ type Hello {
type Hello {
world(things: [String]): String
}`;
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'TypeDefinition',
kind: 'ObjectDefinition',
name: nameNode('Hello', loc(6, 11)),
interfaces: [],
fields: [
Expand Down Expand Up @@ -374,13 +374,13 @@ type Hello {
type Hello {
world(argOne: Boolean, argTwo: Int): String
}`;
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'TypeDefinition',
kind: 'ObjectDefinition',
name: nameNode('Hello', loc(6, 11)),
interfaces: [],
fields: [
Expand Down Expand Up @@ -414,10 +414,10 @@ type Hello {

it('Simple union', () => {
var body = `union Hello = World`;
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'UnionDefinition',
Expand All @@ -433,10 +433,10 @@ type Hello {

it('Union with two types', () => {
var body = `union Hello = Wo | Rld`;
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'UnionDefinition',
Expand All @@ -455,10 +455,10 @@ type Hello {

it('Scalar', () => {
var body = `scalar Hello`;
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'ScalarDefinition',
Expand All @@ -476,10 +476,10 @@ type Hello {
input Hello {
world: String
}`;
var doc = parseSchemaIntoAST(body);
var doc = parse(body);
var loc = createLocFn(body);
var expected = {
kind: 'SchemaDocument',
kind: 'Document',
definitions: [
{
kind: 'InputObjectDefinition',
Expand All @@ -505,16 +505,7 @@ input Hello {
input Hello {
world(foo: Int): String
}`;
expect(() => parseSchemaIntoAST(body)).to.throw('Error');
expect(() => parse(body)).to.throw('Error');
});

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

it('Reject query shorthand', () => {
var body = `{ field }`;
expect(() => parseSchemaIntoAST(body)).to.throw('Error');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { expect } from 'chai';
import { describe, it } from 'mocha';
import { readFileSync } from 'fs';
import { join } from 'path';
import { parseSchemaIntoAST } from '../parser';
import { printSchema } from '../printer';
import { parse } from '../parser';
import { print } from '../printer';

describe('Printer', () => {

Expand All @@ -21,12 +21,12 @@ describe('Printer', () => {
kind: 'ScalarDefinition',
name: { kind: 'Name', value: 'foo' }
};
expect(printSchema(ast)).to.equal('scalar foo');
expect(print(ast)).to.equal('scalar foo');
});

it('produces helpful error messages', () => {
var badAst1 = { random: 'Data' };
expect(() => printSchema(badAst1)).to.throw(
expect(() => print(badAst1)).to.throw(
'Invalid AST Node: {"random":"Data"}'
);
});
Expand All @@ -37,17 +37,17 @@ describe('Printer', () => {
);

it('does not alter ast', () => {
var ast = parseSchemaIntoAST(kitchenSink);
var ast = parse(kitchenSink);
var astCopy = JSON.parse(JSON.stringify(ast));
printSchema(ast);
print(ast);
expect(ast).to.deep.equal(astCopy);
});

it('prints kitchen sink', () => {

var ast = parseSchemaIntoAST(kitchenSink);
var ast = parse(kitchenSink);

var printed = printSchema(ast);
var printed = print(ast);

expect(printed).to.equal(
`type Foo implements Bar {
Expand Down