Skip to content

Commit

Permalink
Address review feedback
Browse files Browse the repository at this point in the history
s/fieldName/memberName

Adds wrapping object around return type of resolveSchemaCoordinate to ensure it's easy to use that result.
  • Loading branch information
leebyron committed May 27, 2021
1 parent 088735c commit 1399f7c
Show file tree
Hide file tree
Showing 11 changed files with 208 additions and 110 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Expand Up @@ -182,7 +182,6 @@ export type {
GraphQLScalarSerializer,
GraphQLScalarValueParser,
GraphQLScalarLiteralParser,
GraphQLSchemaElement,
} from './type/index';

/** Parse and operate on GraphQL language source files. */
Expand Down Expand Up @@ -471,4 +470,5 @@ export type {
BreakingChange,
DangerousChange,
TypedQueryDocumentNode,
GraphQLSchemaElement,
} from './utilities/index';
10 changes: 5 additions & 5 deletions src/language/__tests__/parser-test.ts
Expand Up @@ -638,7 +638,7 @@ describe('Parser', () => {
loc: { start: 0, end: 6 },
value: 'MyType',
},
fieldName: undefined,
memberName: undefined,
argumentName: undefined,
});
});
Expand All @@ -654,7 +654,7 @@ describe('Parser', () => {
loc: { start: 0, end: 6 },
value: 'MyType',
},
fieldName: {
memberName: {
kind: Kind.NAME,
loc: { start: 7, end: 12 },
value: 'field',
Expand Down Expand Up @@ -683,7 +683,7 @@ describe('Parser', () => {
loc: { start: 0, end: 6 },
value: 'MyType',
},
fieldName: {
memberName: {
kind: Kind.NAME,
loc: { start: 7, end: 12 },
value: 'field',
Expand Down Expand Up @@ -716,7 +716,7 @@ describe('Parser', () => {
loc: { start: 1, end: 12 },
value: 'myDirective',
},
fieldName: undefined,
memberName: undefined,
argumentName: undefined,
});
});
Expand All @@ -732,7 +732,7 @@ describe('Parser', () => {
loc: { start: 1, end: 12 },
value: 'myDirective',
},
fieldName: undefined,
memberName: undefined,
argumentName: {
kind: Kind.NAME,
loc: { start: 13, end: 16 },
Expand Down
2 changes: 1 addition & 1 deletion src/language/ast.ts
Expand Up @@ -680,6 +680,6 @@ export interface SchemaCoordinateNode {
readonly loc?: Location;
readonly isDirective: boolean;
readonly name: NameNode;
readonly fieldName?: NameNode;
readonly memberName?: NameNode;
readonly argumentName?: NameNode;
}
8 changes: 4 additions & 4 deletions src/language/parser.ts
Expand Up @@ -1386,13 +1386,13 @@ export class Parser {
const start = this._lexer.token;
const isDirective = this.expectOptionalToken(TokenKind.AT);
const name = this.parseName();
let fieldName;
let memberName;
if (!isDirective && this.expectOptionalToken(TokenKind.DOT)) {
fieldName = this.parseName();
memberName = this.parseName();
}
let argumentName;
if (
(isDirective || fieldName) &&
(isDirective || memberName) &&
this.expectOptionalToken(TokenKind.PAREN_L)
) {
argumentName = this.parseName();
Expand All @@ -1403,7 +1403,7 @@ export class Parser {
kind: Kind.SCHEMA_COORDINATE,
isDirective,
name,
fieldName,
memberName,
argumentName,
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/language/printer.ts
Expand Up @@ -306,11 +306,11 @@ const printDocASTReducer: ASTReducer<string> = {
// Schema Coordinate

SchemaCoordinate: {
leave: ({ isDirective, name, fieldName, argumentName }) =>
leave: ({ isDirective, name, memberName, argumentName }) =>
join([
isDirective && '@',
name,
wrap('.', fieldName),
wrap('.', memberName),
wrap('(', argumentName, ':)'),
]),
},
Expand Down
2 changes: 1 addition & 1 deletion src/language/visitor.ts
Expand Up @@ -161,7 +161,7 @@ const QueryDocumentKeys = {
EnumTypeExtension: ['name', 'directives', 'values'],
InputObjectTypeExtension: ['name', 'directives', 'fields'],

SchemaCoordinate: ['name', 'fieldName', 'argumentName'],
SchemaCoordinate: ['name', 'memberName', 'argumentName'],
};

export const BREAK: unknown = Object.freeze({});
Expand Down
16 changes: 0 additions & 16 deletions src/type/element.ts

This file was deleted.

3 changes: 0 additions & 3 deletions src/type/index.ts
Expand Up @@ -179,6 +179,3 @@ export type {

/** Validate GraphQL schema. */
export { validateSchema, assertValidSchema } from './validate';

// Schema Element type.
export { GraphQLSchemaElement } from './element';
166 changes: 102 additions & 64 deletions src/utilities/__tests__/resolveSchemaCoordinate-test.ts
@@ -1,11 +1,18 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import type {
GraphQLEnumType,
GraphQLInputObjectType,
GraphQLObjectType,
} from '../../type/definition';
import type { GraphQLDirective } from '../../type/directives';

import { buildSchema } from '../buildASTSchema';
import { resolveSchemaCoordinate } from '../resolveSchemaCoordinate';

describe('resolveSchemaCoordinate', () => {
const schema: any = buildSchema(`
const schema = buildSchema(`
type Query {
searchBusiness(criteria: SearchCriteria!): [Business]
}
Expand All @@ -31,116 +38,147 @@ describe('resolveSchemaCoordinate', () => {
`);

it('resolves a Named Type', () => {
const expected = schema.getType('Business');
expect(expected).not.to.equal(undefined);
expect(resolveSchemaCoordinate(schema, 'Business')).to.equal(expected);
expect(resolveSchemaCoordinate(schema, 'Business')).to.deep.equal({
kind: 'NamedType',
type: schema.getType('Business'),
});

expect(resolveSchemaCoordinate(schema, 'String')).to.equal(
schema.getType('String'),
);
expect(resolveSchemaCoordinate(schema, 'String')).to.deep.equal({
kind: 'NamedType',
type: schema.getType('String'),
});

expect(resolveSchemaCoordinate(schema, 'private')).to.equal(undefined);
expect(resolveSchemaCoordinate(schema, 'private')).to.deep.equal(undefined);

expect(resolveSchemaCoordinate(schema, 'Unknown')).to.equal(undefined);
expect(resolveSchemaCoordinate(schema, 'Unknown')).to.deep.equal(undefined);
});

it('resolves a Type Field', () => {
const expected = schema.getType('Business').getFields().name;
expect(expected).not.to.equal(undefined);
expect(resolveSchemaCoordinate(schema, 'Business.name')).to.equal(expected);

expect(resolveSchemaCoordinate(schema, 'Business.unknown')).to.equal(
const type = schema.getType('Business') as GraphQLObjectType;
const field = type.getFields().name;
expect(resolveSchemaCoordinate(schema, 'Business.name')).to.deep.equal({
kind: 'Field',
type,
field,
});

expect(resolveSchemaCoordinate(schema, 'Business.unknown')).to.deep.equal(
undefined,
);

expect(resolveSchemaCoordinate(schema, 'Unknown.field')).to.equal(
expect(resolveSchemaCoordinate(schema, 'Unknown.field')).to.deep.equal(
undefined,
);

expect(resolveSchemaCoordinate(schema, 'String.field')).to.equal(undefined);
expect(resolveSchemaCoordinate(schema, 'String.field')).to.deep.equal(
undefined,
);
});

it('does not resolve meta-fields', () => {
expect(resolveSchemaCoordinate(schema, 'Business.__typename')).to.equal(
undefined,
);
expect(
resolveSchemaCoordinate(schema, 'Business.__typename'),
).to.deep.equal(undefined);
});

it('resolves a Input Field', () => {
const expected = schema.getType('SearchCriteria').getFields().filter;
expect(expected).not.to.equal(undefined);
expect(resolveSchemaCoordinate(schema, 'SearchCriteria.filter')).to.equal(
expected,
);
const type = schema.getType('SearchCriteria') as GraphQLInputObjectType;
const inputField = type.getFields().filter;
expect(
resolveSchemaCoordinate(schema, 'SearchCriteria.filter'),
).to.deep.equal({
kind: 'InputField',
type,
inputField,
});

expect(resolveSchemaCoordinate(schema, 'SearchCriteria.unknown')).to.equal(
undefined,
);
expect(
resolveSchemaCoordinate(schema, 'SearchCriteria.unknown'),
).to.deep.equal(undefined);
});

it('resolves a Enum Value', () => {
const expected = schema.getType('SearchFilter').getValue('OPEN_NOW');
expect(expected).not.to.equal(undefined);
expect(resolveSchemaCoordinate(schema, 'SearchFilter.OPEN_NOW')).to.equal(
expected,
);
const type = schema.getType('SearchFilter') as GraphQLEnumType;
const enumValue = type.getValue('OPEN_NOW');
expect(
resolveSchemaCoordinate(schema, 'SearchFilter.OPEN_NOW'),
).to.deep.equal({
kind: 'EnumValue',
type,
enumValue,
});

expect(resolveSchemaCoordinate(schema, 'SearchFilter.UNKNOWN')).to.equal(
undefined,
);
expect(
resolveSchemaCoordinate(schema, 'SearchFilter.UNKNOWN'),
).to.deep.equal(undefined);
});

it('resolves a Field Argument', () => {
const expected = schema
.getType('Query')
.getFields()
.searchBusiness.args.find((arg: any) => arg.name === 'criteria');
expect(expected).not.to.equal(undefined);
const type = schema.getType('Query') as GraphQLObjectType;
const field = type.getFields().searchBusiness;
const fieldArgument = field.args.find((arg) => arg.name === 'criteria');
expect(
resolveSchemaCoordinate(schema, 'Query.searchBusiness(criteria:)'),
).to.equal(expected);
).to.deep.equal({
kind: 'FieldArgument',
type,
field,
fieldArgument,
});

expect(resolveSchemaCoordinate(schema, 'Business.name(unknown:)')).to.equal(
undefined,
);
expect(
resolveSchemaCoordinate(schema, 'Business.name(unknown:)'),
).to.deep.equal(undefined);

expect(resolveSchemaCoordinate(schema, 'Unknown.field(arg:)')).to.equal(
undefined,
);
expect(
resolveSchemaCoordinate(schema, 'Unknown.field(arg:)'),
).to.deep.equal(undefined);

expect(resolveSchemaCoordinate(schema, 'Business.unknown(arg:)')).to.equal(
undefined,
);
expect(
resolveSchemaCoordinate(schema, 'Business.unknown(arg:)'),
).to.deep.equal(undefined);

expect(
resolveSchemaCoordinate(schema, 'SearchCriteria.name(arg:)'),
).to.equal(undefined);
).to.deep.equal(undefined);
});

it('resolves a Directive', () => {
const expected = schema.getDirective('private');
expect(expected).not.to.equal(undefined);
expect(resolveSchemaCoordinate(schema, '@private')).to.equal(expected);
expect(resolveSchemaCoordinate(schema, '@private')).to.deep.equal({
kind: 'Directive',
directive: schema.getDirective('private'),
});

expect(resolveSchemaCoordinate(schema, '@deprecated')).to.deep.equal({
kind: 'Directive',
directive: schema.getDirective('deprecated'),
});

expect(resolveSchemaCoordinate(schema, '@unknown')).to.equal(undefined);
expect(resolveSchemaCoordinate(schema, '@unknown')).to.deep.equal(
undefined,
);

expect(resolveSchemaCoordinate(schema, '@Business')).to.equal(undefined);
expect(resolveSchemaCoordinate(schema, '@Business')).to.deep.equal(
undefined,
);
});

it('resolves a Directive Argument', () => {
const expected = schema
.getDirective('private')
.args.find((arg: any) => arg.name === 'scope');
expect(expected).not.to.equal(undefined);
expect(resolveSchemaCoordinate(schema, '@private(scope:)')).to.equal(
expected,
const directive = schema.getDirective('private') as GraphQLDirective;
const directiveArgument = directive.args.find(
(arg) => arg.name === 'scope',
);
expect(resolveSchemaCoordinate(schema, '@private(scope:)')).to.deep.equal({
kind: 'DirectiveArgument',
directive,
directiveArgument,
});

expect(resolveSchemaCoordinate(schema, '@private(unknown:)')).to.equal(
expect(resolveSchemaCoordinate(schema, '@private(unknown:)')).to.deep.equal(
undefined,
);

expect(resolveSchemaCoordinate(schema, '@unknown(arg:)')).to.equal(
expect(resolveSchemaCoordinate(schema, '@unknown(arg:)')).to.deep.equal(
undefined,
);
});
Expand Down
1 change: 1 addition & 0 deletions src/utilities/index.ts
Expand Up @@ -109,6 +109,7 @@ export { TypedQueryDocumentNode } from './typedQueryDocumentNode';

/** Schema coordinates */
export {
GraphQLSchemaElement,
resolveSchemaCoordinate,
resolveASTSchemaCoordinate,
} from './resolveSchemaCoordinate';

0 comments on commit 1399f7c

Please sign in to comment.