Skip to content

Commit

Permalink
type definitions: Use consistent order for public fields (#2090)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Aug 14, 2019
1 parent 0105d92 commit 7782190
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 31 deletions.
57 changes: 31 additions & 26 deletions src/type/definition.js
Expand Up @@ -558,9 +558,9 @@ export class GraphQLScalarType {
this.parseValue = parseValue;
this.parseLiteral =
config.parseLiteral || (node => parseValue(valueFromASTUntyped(node)));

this.astNode = config.astNode;
this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);

devAssert(typeof config.name === 'string', 'Must provide name.');
devAssert(
config.serialize == null || typeof config.serialize === 'function',
Expand Down Expand Up @@ -663,19 +663,20 @@ export type GraphQLScalarTypeConfig<TInternal, TExternal> = {|
export class GraphQLObjectType {
name: string;
description: ?string;
isTypeOf: ?GraphQLIsTypeOfFn<*, *>;
astNode: ?ObjectTypeDefinitionNode;
extensionASTNodes: ?$ReadOnlyArray<ObjectTypeExtensionNode>;
isTypeOf: ?GraphQLIsTypeOfFn<*, *>;

_fields: Thunk<GraphQLFieldMap<*, *>>;
_interfaces: Thunk<Array<GraphQLInterfaceType>>;

constructor(config: GraphQLObjectTypeConfig<*, *>): void {
this.name = config.name;
this.description = config.description;
this.isTypeOf = config.isTypeOf;
this.astNode = config.astNode;
this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
this.isTypeOf = config.isTypeOf;

this._fields = defineFieldMap.bind(undefined, config);
this._interfaces = defineInterfaces.bind(undefined, config);
devAssert(typeof config.name === 'string', 'Must provide name.');
Expand Down Expand Up @@ -709,9 +710,9 @@ export class GraphQLObjectType {
return {
name: this.name,
description: this.description,
isTypeOf: this.isTypeOf,
interfaces: this.getInterfaces(),
fields: fieldsToFieldsConfig(this.getFields()),
isTypeOf: this.isTypeOf,
astNode: this.astNode,
extensionASTNodes: this.extensionASTNodes || [],
};
Expand Down Expand Up @@ -797,12 +798,12 @@ function isPlainObj(obj) {
function fieldsToFieldsConfig(fields) {
return mapValue(fields, field => ({
description: field.description,
type: field.type,
args: argsToArgsConfig(field.args),
resolve: field.resolve,
subscribe: field.subscribe,
deprecationReason: field.deprecationReason,
description: field.description,
astNode: field.astNode,
}));
}
Expand All @@ -814,20 +815,20 @@ export function argsToArgsConfig(
args,
arg => arg.name,
arg => ({
description: arg.description,
type: arg.type,
defaultValue: arg.defaultValue,
description: arg.description,
astNode: arg.astNode,
}),
);
}
export type GraphQLObjectTypeConfig<TSource, TContext> = {|
name: string,
description?: ?string,
interfaces?: Thunk<?Array<GraphQLInterfaceType>>,
fields: Thunk<GraphQLFieldConfigMap<TSource, TContext>>,
isTypeOf?: ?GraphQLIsTypeOfFn<TSource, TContext>,
description?: ?string,
astNode?: ?ObjectTypeDefinitionNode,
extensionASTNodes?: ?$ReadOnlyArray<ObjectTypeExtensionNode>,
|};
Expand Down Expand Up @@ -874,21 +875,21 @@ export type GraphQLFieldConfig<
TContext,
TArgs = { [argument: string]: any, ... },
> = {|
description?: ?string,
type: GraphQLOutputType,
args?: GraphQLFieldConfigArgumentMap,
resolve?: GraphQLFieldResolver<TSource, TContext, TArgs>,
subscribe?: GraphQLFieldResolver<TSource, TContext, TArgs>,
deprecationReason?: ?string,
description?: ?string,
astNode?: ?FieldDefinitionNode,
|};
export type GraphQLFieldConfigArgumentMap = ObjMap<GraphQLArgumentConfig>;
export type GraphQLArgumentConfig = {|
description?: ?string,
type: GraphQLInputType,
defaultValue?: mixed,
description?: ?string,
astNode?: ?InputValueDefinitionNode,
|};
Expand All @@ -914,9 +915,9 @@ export type GraphQLField<
export type GraphQLArgument = {|
name: string,
description?: ?string,
type: GraphQLInputType,
defaultValue?: mixed,
description?: ?string,
astNode?: ?InputValueDefinitionNode,
|};
Expand Down Expand Up @@ -949,18 +950,19 @@ export type GraphQLFieldMap<TSource, TContext> = ObjMap<
export class GraphQLInterfaceType {
name: string;
description: ?string;
resolveType: ?GraphQLTypeResolver<*, *>;
astNode: ?InterfaceTypeDefinitionNode;
extensionASTNodes: ?$ReadOnlyArray<InterfaceTypeExtensionNode>;
resolveType: ?GraphQLTypeResolver<*, *>;
_fields: Thunk<GraphQLFieldMap<*, *>>;
constructor(config: GraphQLInterfaceTypeConfig<*, *>): void {
this.name = config.name;
this.description = config.description;
this.resolveType = config.resolveType;
this.astNode = config.astNode;
this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
this.resolveType = config.resolveType;
this._fields = defineFieldMap.bind(undefined, config);
devAssert(typeof config.name === 'string', 'Must provide name.');
devAssert(
Expand All @@ -985,8 +987,8 @@ export class GraphQLInterfaceType {
return {
name: this.name,
description: this.description,
resolveType: this.resolveType,
fields: fieldsToFieldsConfig(this.getFields()),
resolveType: this.resolveType,
astNode: this.astNode,
extensionASTNodes: this.extensionASTNodes || [],
};
Expand All @@ -1003,14 +1005,14 @@ defineToJSON(GraphQLInterfaceType);

export type GraphQLInterfaceTypeConfig<TSource, TContext> = {|
name: string,
description?: ?string,
fields: Thunk<GraphQLFieldConfigMap<TSource, TContext>>,
/**
* Optionally provide a custom type resolver function. If one is not provided,
* the default implementation will call `isTypeOf` on each implementing
* Object type.
*/
resolveType?: ?GraphQLTypeResolver<TSource, TContext>,
description?: ?string,
astNode?: ?InterfaceTypeDefinitionNode,
extensionASTNodes?: ?$ReadOnlyArray<InterfaceTypeExtensionNode>,
|};
Expand Down Expand Up @@ -1041,18 +1043,19 @@ export type GraphQLInterfaceTypeConfig<TSource, TContext> = {|
export class GraphQLUnionType {
name: string;
description: ?string;
resolveType: ?GraphQLTypeResolver<*, *>;
astNode: ?UnionTypeDefinitionNode;
extensionASTNodes: ?$ReadOnlyArray<UnionTypeExtensionNode>;
resolveType: ?GraphQLTypeResolver<*, *>;

_types: Thunk<Array<GraphQLObjectType>>;

constructor(config: GraphQLUnionTypeConfig<*, *>): void {
this.name = config.name;
this.description = config.description;
this.resolveType = config.resolveType;
this.astNode = config.astNode;
this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);
this.resolveType = config.resolveType;

this._types = defineTypes.bind(undefined, config);
devAssert(typeof config.name === 'string', 'Must provide name.');
devAssert(
Expand All @@ -1077,8 +1080,8 @@ export class GraphQLUnionType {
return {
name: this.name,
description: this.description,
resolveType: this.resolveType,
types: this.getTypes(),
resolveType: this.resolveType,
astNode: this.astNode,
extensionASTNodes: this.extensionASTNodes || [],
};
Expand Down Expand Up @@ -1106,14 +1109,14 @@ function defineTypes(

export type GraphQLUnionTypeConfig<TSource, TContext> = {|
name: string,
description?: ?string,
types: Thunk<Array<GraphQLObjectType>>,
/**
* Optionally provide a custom type resolver function. If one is not provided,
* the default implementation will call `isTypeOf` on each implementing
* Object type.
*/
resolveType?: ?GraphQLTypeResolver<TSource, TContext>,
description?: ?string,
astNode?: ?UnionTypeDefinitionNode,
extensionASTNodes?: ?$ReadOnlyArray<UnionTypeExtensionNode>,
|};
Expand Down Expand Up @@ -1154,6 +1157,7 @@ export class GraphQLEnumType /* <T> */ {
this.description = config.description;
this.astNode = config.astNode;
this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);

this._values = defineEnumValues(this.name, config.values);
this._valueLookup = new Map(
this._values.map(enumValue => [enumValue.value, enumValue]),
Expand Down Expand Up @@ -1251,38 +1255,38 @@ function defineEnumValues(
return {
name: valueName,
description: value.description,
value: 'value' in value ? value.value : valueName,
isDeprecated: Boolean(value.deprecationReason),
deprecationReason: value.deprecationReason,
astNode: value.astNode,
value: 'value' in value ? value.value : valueName,
};
});
}

export type GraphQLEnumTypeConfig /* <T> */ = {|
name: string,
values: GraphQLEnumValueConfigMap /* <T> */,
description?: ?string,
values: GraphQLEnumValueConfigMap /* <T> */,
astNode?: ?EnumTypeDefinitionNode,
extensionASTNodes?: ?$ReadOnlyArray<EnumTypeExtensionNode>,
|};

export type GraphQLEnumValueConfigMap /* <T> */ = ObjMap<GraphQLEnumValueConfig /* <T> */>;

export type GraphQLEnumValueConfig /* <T> */ = {|
description?: ?string,
value?: any /* T */,
deprecationReason?: ?string,
description?: ?string,
astNode?: ?EnumValueDefinitionNode,
|};

export type GraphQLEnumValue /* <T> */ = {|
name: string,
description: ?string,
value: any /* T */,
isDeprecated?: boolean,
deprecationReason: ?string,
astNode?: ?EnumValueDefinitionNode,
value: any /* T */,
|};

/**
Expand Down Expand Up @@ -1318,6 +1322,7 @@ export class GraphQLInputObjectType {
this.description = config.description;
this.astNode = config.astNode;
this.extensionASTNodes = undefineIfEmpty(config.extensionASTNodes);

this._fields = defineInputFieldMap.bind(undefined, config);
devAssert(typeof config.name === 'string', 'Must provide name.');
}
Expand Down Expand Up @@ -1375,36 +1380,36 @@ function defineInputFieldMap(

return {
name: fieldName,
description: fieldConfig.description,
type: fieldConfig.type,
defaultValue: fieldConfig.defaultValue,
description: fieldConfig.description,
astNode: fieldConfig.astNode,
};
});
}

export type GraphQLInputObjectTypeConfig = {|
name: string,
fields: Thunk<GraphQLInputFieldConfigMap>,
description?: ?string,
fields: Thunk<GraphQLInputFieldConfigMap>,
astNode?: ?InputObjectTypeDefinitionNode,
extensionASTNodes?: ?$ReadOnlyArray<InputObjectTypeExtensionNode>,
|};

export type GraphQLInputFieldConfig = {|
description?: ?string,
type: GraphQLInputType,
defaultValue?: mixed,
description?: ?string,
astNode?: ?InputValueDefinitionNode,
|};

export type GraphQLInputFieldConfigMap = ObjMap<GraphQLInputFieldConfig>;

export type GraphQLInputField = {|
name: string,
description?: ?string,
type: GraphQLInputType,
defaultValue?: mixed,
description?: ?string,
astNode?: ?InputValueDefinitionNode,
|};

Expand Down
2 changes: 1 addition & 1 deletion src/type/directives.js
Expand Up @@ -58,10 +58,10 @@ export class GraphQLDirective {
constructor(config: GraphQLDirectiveConfig): void {
this.name = config.name;
this.description = config.description;

this.locations = config.locations;
this.isRepeatable = config.isRepeatable != null && config.isRepeatable;
this.astNode = config.astNode;

devAssert(config.name, 'Directive must be named.');
devAssert(
Array.isArray(config.locations),
Expand Down
10 changes: 6 additions & 4 deletions src/type/schema.js
Expand Up @@ -117,6 +117,7 @@ export function assertSchema(schema: mixed): GraphQLSchema {
export class GraphQLSchema {
astNode: ?SchemaDefinitionNode;
extensionASTNodes: ?$ReadOnlyArray<SchemaExtensionNode>;

_queryType: ?GraphQLObjectType;
_mutationType: ?GraphQLObjectType;
_subscriptionType: ?GraphQLObjectType;
Expand Down Expand Up @@ -156,14 +157,15 @@ export class GraphQLSchema {
);
}

this.astNode = config.astNode;
this.extensionASTNodes = config.extensionASTNodes;

this.__allowedLegacyNames = config.allowedLegacyNames || [];
this._queryType = config.query;
this._mutationType = config.mutation;
this._subscriptionType = config.subscription;
// Provide specified directives (e.g. @include and @skip) by default.
this._directives = config.directives || specifiedDirectives;
this.astNode = config.astNode;
this.extensionASTNodes = config.extensionASTNodes;

// Build type map now to detect any errors within this schema.
const initialTypes: Array<?GraphQLNamedType> = [
Expand Down Expand Up @@ -266,11 +268,11 @@ export class GraphQLSchema {
allowedLegacyNames: $ReadOnlyArray<string>,
|} {
return {
types: objectValues(this.getTypeMap()),
directives: this.getDirectives().slice(),
query: this.getQueryType(),
mutation: this.getMutationType(),
subscription: this.getSubscriptionType(),
types: objectValues(this.getTypeMap()),
directives: this.getDirectives().slice(),
astNode: this.astNode,
extensionASTNodes: this.extensionASTNodes || [],
assumeValid: this.__validationErrors !== undefined,
Expand Down

0 comments on commit 7782190

Please sign in to comment.