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

definitions: make constructed fields non-optional #2091

Merged
merged 1 commit into from
Aug 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 40 additions & 30 deletions src/type/__tests__/predicate-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,77 +573,87 @@ describe('Type predicates', () => {
});

describe('isRequiredArgument', () => {
it('returns true for required arguments', () => {
const requiredArg = {
function buildArg(config) {
return {
name: 'someArg',
type: GraphQLNonNull(GraphQLString),
description: undefined,
defaultValue: undefined,
astNode: undefined,
...config,
};
}

it('returns true for required arguments', () => {
const requiredArg = buildArg({
type: GraphQLNonNull(GraphQLString),
});
expect(isRequiredArgument(requiredArg)).to.equal(true);
});

it('returns false for optional arguments', () => {
const optArg1 = {
name: 'someArg',
const optArg1 = buildArg({
type: GraphQLString,
};
});
expect(isRequiredArgument(optArg1)).to.equal(false);

const optArg2 = {
name: 'someArg',
const optArg2 = buildArg({
type: GraphQLString,
defaultValue: null,
};
});
expect(isRequiredArgument(optArg2)).to.equal(false);

const optArg3 = {
name: 'someArg',
const optArg3 = buildArg({
type: GraphQLList(GraphQLNonNull(GraphQLString)),
};
});
expect(isRequiredArgument(optArg3)).to.equal(false);

const optArg4 = {
name: 'someArg',
const optArg4 = buildArg({
type: GraphQLNonNull(GraphQLString),
defaultValue: 'default',
};
});
expect(isRequiredArgument(optArg4)).to.equal(false);
});
});

describe('isRequiredInputField', () => {
function buildInputField(config) {
return {
name: 'someInputField',
description: undefined,
defaultValue: undefined,
astNode: undefined,
...config,
};
}

it('returns true for required input field', () => {
const requiredField = {
name: 'someField',
const requiredField = buildInputField({
type: GraphQLNonNull(GraphQLString),
};
});
expect(isRequiredInputField(requiredField)).to.equal(true);
});

it('returns false for optional input field', () => {
const optField1 = {
name: 'someField',
const optField1 = buildInputField({
type: GraphQLString,
};
});
expect(isRequiredInputField(optField1)).to.equal(false);

const optField2 = {
name: 'someField',
const optField2 = buildInputField({
type: GraphQLString,
defaultValue: null,
};
});
expect(isRequiredInputField(optField2)).to.equal(false);

const optField3 = {
name: 'someField',
const optField3 = buildInputField({
type: GraphQLList(GraphQLNonNull(GraphQLString)),
};
});
expect(isRequiredInputField(optField3)).to.equal(false);

const optField4 = {
name: 'someField',
const optField4 = buildInputField({
type: GraphQLNonNull(GraphQLString),
defaultValue: 'default',
};
});
expect(isRequiredInputField(optField4)).to.equal(false);
});
});
Expand Down
20 changes: 10 additions & 10 deletions src/type/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -909,16 +909,16 @@ export type GraphQLField<
resolve?: GraphQLFieldResolver<TSource, TContext, TArgs>,
subscribe?: GraphQLFieldResolver<TSource, TContext, TArgs>,
isDeprecated?: boolean,
deprecationReason?: ?string,
astNode?: ?FieldDefinitionNode,
deprecationReason: ?string,
astNode: ?FieldDefinitionNode,
|};
export type GraphQLArgument = {|
name: string,
description?: ?string,
description: ?string,
type: GraphQLInputType,
defaultValue?: mixed,
astNode?: ?InputValueDefinitionNode,
defaultValue: mixed,
astNode: ?InputValueDefinitionNode,
|};
export function isRequiredArgument(arg: GraphQLArgument): boolean %checks {
Expand Down Expand Up @@ -1284,9 +1284,9 @@ export type GraphQLEnumValue /* <T> */ = {|
name: string,
description: ?string,
value: any /* T */,
isDeprecated?: boolean,
isDeprecated: boolean,
deprecationReason: ?string,
astNode?: ?EnumValueDefinitionNode,
astNode: ?EnumValueDefinitionNode,
|};

/**
Expand Down Expand Up @@ -1407,10 +1407,10 @@ export type GraphQLInputFieldConfigMap = ObjMap<GraphQLInputFieldConfig>;

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

export function isRequiredInputField(
Expand Down
16 changes: 15 additions & 1 deletion src/type/introspection.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,26 @@ export const SchemaMetaFieldDef: GraphQLField<mixed, mixed> = {
description: 'Access the current type schema of this server.',
args: [],
resolve: (source, args, context, { schema }) => schema,
deprecationReason: undefined,
astNode: undefined,
};

export const TypeMetaFieldDef: GraphQLField<mixed, mixed> = {
name: '__type',
type: __Type,
description: 'Request the type information of a single type.',
args: [{ name: 'name', type: GraphQLNonNull(GraphQLString) }],
args: [
{
name: 'name',
description: undefined,
type: GraphQLNonNull(GraphQLString),
defaultValue: undefined,
astNode: undefined,
},
],
resolve: (source, { name }, context, { schema }) => schema.getType(name),
deprecationReason: undefined,
astNode: undefined,
};

export const TypeNameMetaFieldDef: GraphQLField<mixed, mixed> = {
Expand All @@ -454,6 +466,8 @@ export const TypeNameMetaFieldDef: GraphQLField<mixed, mixed> = {
description: 'The name of the current Object type at runtime.',
args: [],
resolve: (source, args, context, { parentType }) => parentType.name,
deprecationReason: undefined,
astNode: undefined,
};

export const introspectionTypes = Object.freeze([
Expand Down