···
import { parse, parseType, parseValue } from '../parser';
import { Kind } from '../kind';
-
describe('print', () => {
-
it('prints the kitchen sink document like graphql.js does', () => {
const sink = readFileSync(__dirname + '/../../benchmark/kitchen_sink.graphql', {
···
expect(doc).toEqual(graphql_parse(sink, { noLocation: true }));
-
it('parse provides errors', () => {
expect(() => parse('{')).toThrow();
it('parses variable inline values', () => {
···
it('creates ast', () => {
···
const result = parse('{ id }', { noLocation: true });
expect('loc' in result).toBe(false);
-
describe('parseValue', () => {
-
it('parses null value', () => {
-
const result = parseValue('null');
-
expect(result).toEqual({ kind: Kind.NULL });
-
it('parses list values', () => {
-
const result = parseValue('[123 "abc"]');
-
expect(result).toEqual({
-
it('parses block strings', () => {
-
const result = parseValue('["""long""" "short"]');
-
expect(result).toEqual({
-
it('allows variables', () => {
-
const result = parseValue('{ field: $var }');
-
expect(result).toEqual({
-
kind: Kind.OBJECT_FIELD,
-
it('correct message for incomplete variable', () => {
-
return parseValue('$');
-
it('correct message for unexpected token', () => {
-
return parseValue(':');
-
describe('parseType', () => {
-
it('parses well known types', () => {
-
const result = parseType('String');
-
expect(result).toEqual({
-
it('parses custom types', () => {
-
const result = parseType('MyType');
-
expect(result).toEqual({
-
it('parses list types', () => {
-
const result = parseType('[MyType]');
-
expect(result).toEqual({
-
it('parses non-null types', () => {
-
const result = parseType('MyType!');
-
expect(result).toEqual({
kind: Kind.NON_NULL_TYPE,
···
-
it('parses nested types', () => {
-
const result = parseType('[MyType!]');
-
expect(result).toEqual({
-
kind: Kind.NON_NULL_TYPE,
···
import { parse, parseType, parseValue } from '../parser';
import { Kind } from '../kind';
+
describe('parse', () => {
+
it('parses the kitchen sink document like graphql.js does', () => {
const sink = readFileSync(__dirname + '/../../benchmark/kitchen_sink.graphql', {
···
expect(doc).toEqual(graphql_parse(sink, { noLocation: true }));
+
it('parses basic documents', () => {
expect(() => parse('{')).toThrow();
+
expect(() => parse('{}x ')).toThrow();
+
expect(() => parse('{ field }')).not.toThrow();
+
expect(() => parse({ body: '{ field }' })).not.toThrow();
it('parses variable inline values', () => {
···
+
it('parses fragment definitions', () => {
+
expect(() => parse('fragment { test }')).toThrow();
+
expect(() => parse('fragment name { test }')).toThrow();
+
expect(() => parse('fragment name on name')).toThrow();
+
expect(() => parse('fragment Name on Type { field }')).not.toThrow();
+
it('parses fields', () => {
+
expect(() => parse('{ field: }')).toThrow();
+
expect(() => parse('{ alias: field() }')).toThrow();
+
expect(parse('{ alias: field { child } }').definitions[0]).toHaveProperty(
+
'selectionSet.selections.0',
+
kind: Kind.SELECTION_SET,
+
it('parses arguments', () => {
+
expect(() => parse('{ field() }')).toThrow();
+
expect(() => parse('{ field(name) }')).toThrow();
+
expect(() => parse('{ field(name:) }')).toThrow();
+
expect(() => parse('{ field(name: null }')).toThrow();
+
expect(parse('{ field(name: null) }').definitions[0]).toMatchObject({
+
kind: Kind.OPERATION_DEFINITION,
+
kind: Kind.SELECTION_SET,
+
it('parses directives', () => {
+
expect(() => parse('{ field @ }')).toThrow();
+
expect(() => parse('{ field @(test: null) }')).toThrow();
+
expect(parse('{ field @test(name: null) }')).toHaveProperty(
+
'definitions.0.selectionSet.selections.0.directives.0',
+
it('parses inline fragments', () => {
+
expect(() => parse('{ ... on Test }')).toThrow();
+
expect(() => parse('{ ... {} }')).toThrow();
+
expect(() => parse('{ ... }')).toThrow();
+
expect(parse('{ ... on Test { field } }')).toHaveProperty(
+
'definitions.0.selectionSet.selections.0',
+
kind: Kind.INLINE_FRAGMENT,
+
selectionSet: expect.any(Object),
+
expect(parse('{ ... { field } }')).toHaveProperty('definitions.0.selectionSet.selections.0', {
+
kind: Kind.INLINE_FRAGMENT,
+
typeCondition: undefined,
+
selectionSet: expect.any(Object),
+
it('parses variable definitions', () => {
+
expect(() => parse('query ( { test }')).toThrow();
+
expect(() => parse('query ($var) { test }')).toThrow();
+
expect(() => parse('query ($var:) { test }')).toThrow();
+
expect(() => parse('query ($var: Int =) { test }')).toThrow();
+
expect(parse('query ($var: Int = 1) { test }').definitions[0]).toMatchObject({
+
kind: Kind.OPERATION_DEFINITION,
+
selectionSet: expect.any(Object),
+
kind: Kind.VARIABLE_DEFINITION,
it('creates ast', () => {
···
const result = parse('{ id }', { noLocation: true });
expect('loc' in result).toBe(false);
+
describe('parseValue', () => {
+
it('parses basic values', () => {
+
expect(() => parseValue('')).toThrow();
+
expect(parseValue('null')).toEqual({ kind: Kind.NULL });
+
expect(parseValue({ body: 'null' })).toEqual({ kind: Kind.NULL });
+
it('parses list values', () => {
+
const result = parseValue('[123 "abc"]');
+
expect(result).toEqual({
+
it('parses integers', () => {
+
expect(parseValue('12')).toEqual({
+
expect(parseValue('-12')).toEqual({
+
it('parses floats', () => {
+
expect(parseValue('12e2')).toEqual({
+
expect(parseValue('0.2E3')).toEqual({
+
expect(parseValue('-1.2e+3')).toEqual({
+
it('parses strings', () => {
+
expect(parseValue('"test"')).toEqual({
+
expect(parseValue('"\\t\\t"')).toEqual({
+
it('parses objects', () => {
+
expect(parseValue('{}')).toEqual({
+
expect(() => parseValue('{name}')).toThrow();
+
expect(() => parseValue('{name:}')).toThrow();
+
expect(() => parseValue('{name:null')).toThrow();
+
expect(parseValue('{name:null}')).toEqual({
+
kind: Kind.OBJECT_FIELD,
+
it('parses lists', () => {
+
expect(parseValue('[]')).toEqual({
+
expect(() => parseValue('[')).toThrow();
+
expect(() => parseValue('[null')).toThrow();
+
expect(parseValue('[null]')).toEqual({
+
it('parses block strings', () => {
+
expect(parseValue('["""long""" "short"]')).toEqual({
+
expect(parseValue('"""\n\n first\n second\n"""')).toEqual({
+
value: 'first\nsecond',
+
it('allows variables', () => {
+
const result = parseValue('{ field: $var }');
+
expect(result).toEqual({
+
kind: Kind.OBJECT_FIELD,
+
it('correct message for incomplete variable', () => {
+
return parseValue('$');
+
it('correct message for unexpected token', () => {
+
return parseValue(':');
+
describe('parseType', () => {
+
it('parses basic types', () => {
+
expect(() => parseType('')).toThrow();
+
expect(() => parseType('Type')).not.toThrow();
+
expect(() => parseType({ body: 'Type' })).not.toThrow();
+
it('throws on invalid inputs', () => {
+
expect(() => parseType('!')).toThrow();
+
expect(() => parseType('[String')).toThrow();
+
expect(() => parseType('[String!')).toThrow();
+
it('parses well known types', () => {
+
const result = parseType('String');
+
expect(result).toEqual({
+
it('parses custom types', () => {
+
const result = parseType('MyType');
+
expect(result).toEqual({
+
it('parses list types', () => {
+
const result = parseType('[MyType]');
+
expect(result).toEqual({
+
it('parses non-null types', () => {
+
const result = parseType('MyType!');
+
expect(result).toEqual({
+
kind: Kind.NON_NULL_TYPE,
+
it('parses nested types', () => {
+
const result = parseType('[MyType!]');
+
expect(result).toEqual({
kind: Kind.NON_NULL_TYPE,
···