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

Add parser support for string literal aliases #4023

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 10 additions & 2 deletions src/language/__tests__/parser-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { kitchenSinkQuery } from '../../__testUtils__/kitchenSinkQuery.js';
import { inspect } from '../../jsutils/inspect.js';

import { Kind } from '../kinds.js';
import type { ParseOptions } from '../parser.js';
import { parse, parseConstValue, parseType, parseValue } from '../parser.js';
import { Source } from '../source.js';
import { TokenKind } from '../tokenKind.js';
Expand All @@ -19,8 +20,8 @@ function parseCCN(source: string) {
return parse(source, { experimentalClientControlledNullability: true });
}

function expectSyntaxError(text: string) {
return expectToThrowJSON(() => parse(text));
function expectSyntaxError(text: string, options?: ParseOptions | undefined) {
return expectToThrowJSON(() => parse(text, options));
}

describe('Parser', () => {
Expand Down Expand Up @@ -73,6 +74,13 @@ describe('Parser', () => {
message: 'Syntax Error: Expected Name, found String "".',
locations: [{ line: 1, column: 3 }],
});

expectSyntaxError('{ ""', {
experimentalParseStringLiteralAliases: true,
}).to.deep.include({
message: 'Syntax Error: Expected ":", found <EOF>.',
locations: [{ line: 1, column: 5 }],
});
});

it('parse provides useful error when using source', () => {
Expand Down
45 changes: 41 additions & 4 deletions src/language/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,23 @@ export interface ParseOptions {
* future.
*/
experimentalClientControlledNullability?: boolean | undefined;

/**
* EXPERIMENTAL:
*
* If enabled, the parser will understand and parse StringValues as aliases.
*
* The syntax looks like the following:
*
* ```graphql
* {
* "alias": field
* }
* ```
* Note: this feature is experimental and may change or be removed in the
* future.
*/
experimentalParseStringLiteralAliases?: boolean | undefined;
}

/**
Expand Down Expand Up @@ -236,6 +253,17 @@ export class Parser {
});
}

/**
* Converts a string lex token into a name parse node.
*/
parseStringLiteralAlias(): NameNode {
const token = this.expectToken(TokenKind.STRING);
return this.node<NameNode>(token, {
kind: Kind.NAME,
value: token.value,
});
}

// Implements the parsing rules in the Document section.

/**
Expand Down Expand Up @@ -454,14 +482,23 @@ export class Parser {
parseField(): FieldNode {
const start = this._lexer.token;

const nameOrAlias = this.parseName();
let alias;
let name;
if (this.expectOptionalToken(TokenKind.COLON)) {
alias = nameOrAlias;
if (
this._options.experimentalParseStringLiteralAliases &&
this.peek(TokenKind.STRING)
) {
alias = this.parseStringLiteralAlias();
this.expectToken(TokenKind.COLON);
name = this.parseName();
} else {
name = nameOrAlias;
const nameOrAlias = this.parseName();
if (this.expectOptionalToken(TokenKind.COLON)) {
alias = nameOrAlias;
name = this.parseName();
} else {
name = nameOrAlias;
}
}

return this.node<FieldNode>(start, {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { describe, it } from 'mocha';

import type { ParseOptions } from '../../language/parser.js';

import type { GraphQLSchema } from '../../type/schema.js';

import { buildSchema } from '../../utilities/buildASTSchema.js';
Expand All @@ -11,12 +13,16 @@ import {
expectValidationErrorsWithSchema,
} from './harness.js';

function expectErrors(queryStr: string) {
return expectValidationErrors(OverlappingFieldsCanBeMergedRule, queryStr);
function expectErrors(queryStr: string, options?: ParseOptions | undefined) {
return expectValidationErrors(
OverlappingFieldsCanBeMergedRule,
queryStr,
options,
);
}

function expectValid(queryStr: string) {
expectErrors(queryStr).toDeepEqual([]);
function expectValid(queryStr: string, options?: ParseOptions | undefined) {
expectErrors(queryStr, options).toDeepEqual([]);
}

function expectErrorsWithSchema(schema: GraphQLSchema, queryStr: string) {
Expand Down Expand Up @@ -242,6 +248,39 @@ describe('Validate: Overlapping fields can be merged', () => {
]);
});

it('Same literal aliases allowed on same field targets', () => {
expectValid(
`
fragment sameLiteralAliasesWithSameFieldTargets on Dog {
"fido": name
fido: name
}
`,
{ experimentalParseStringLiteralAliases: true },
);
});

it('Same literal aliases with different field targets', () => {
expectErrors(
`
fragment sameLiteralAliasesWithDifferentFieldTargets on Dog {
"fido": name
fido: nickname
}
`,
{ experimentalParseStringLiteralAliases: true },
).toDeepEqual([
{
message:
'Fields "fido" conflict because "name" and "nickname" are different fields. Use different aliases on the fields to fetch both if this was intentional.',
locations: [
{ line: 3, column: 9 },
{ line: 4, column: 9 },
],
},
]);
});

it('Same aliases allowed on non-overlapping fields', () => {
// This is valid since no object can be both a "Dog" and a "Cat", thus
// these fields can never overlap.
Expand Down
7 changes: 5 additions & 2 deletions src/validation/__tests__/harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { expectJSON } from '../../__testUtils__/expectJSON.js';

import type { Maybe } from '../../jsutils/Maybe.js';

import type { ParseOptions } from '../../language/parser.js';
import { parse } from '../../language/parser.js';

import type { GraphQLSchema } from '../../type/schema.js';
Expand Down Expand Up @@ -128,17 +129,19 @@ export function expectValidationErrorsWithSchema(
schema: GraphQLSchema,
rule: ValidationRule,
queryStr: string,
options?: ParseOptions | undefined,
): any {
const doc = parse(queryStr);
const doc = parse(queryStr, options);
const errors = validate(schema, doc, [rule]);
return expectJSON(errors);
}

export function expectValidationErrors(
rule: ValidationRule,
queryStr: string,
options?: ParseOptions | undefined,
): any {
return expectValidationErrorsWithSchema(testSchema, rule, queryStr);
return expectValidationErrorsWithSchema(testSchema, rule, queryStr, options);
}

export function expectSDLValidationErrors(
Expand Down