Skip to content

Commit

Permalink
type definitions: Use consistent order for public fields
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Aug 14, 2019
1 parent e04d92b commit 70de26a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
49 changes: 27 additions & 22 deletions src/type/definition.js
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 @@ -958,9 +959,10 @@ export class GraphQLInterfaceType {
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,28 +1255,28 @@ 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,
|};

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,26 +1380,26 @@ 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,
|};

Expand Down
9 changes: 5 additions & 4 deletions src/type/schema.js
Expand Up @@ -156,14 +156,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 +267,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 70de26a

Please sign in to comment.