Skip to content

Commit

Permalink
definitions: make constructed fields non-optional
Browse files Browse the repository at this point in the history
Reflects changes in graphql#2089
  • Loading branch information
IvanGoncharov committed Aug 15, 2019
1 parent 7782190 commit df05745
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 38 deletions.
70 changes: 40 additions & 30 deletions src/type/__tests__/predicate-test.js
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
14 changes: 7 additions & 7 deletions src/type/definition.js
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,
astNode: ?InputValueDefinitionNode,
|};
export function isRequiredArgument(arg: GraphQLArgument): boolean %checks {
Expand Down Expand Up @@ -1286,7 +1286,7 @@ export type GraphQLEnumValue /* <T> */ = {|
value: any /* T */,
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,
astNode: ?InputValueDefinitionNode,
|};

export function isRequiredInputField(
Expand Down
15 changes: 14 additions & 1 deletion src/type/introspection.js
Expand Up @@ -438,14 +438,25 @@ 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),
astNode: undefined,
},
],
resolve: (source, { name }, context, { schema }) => schema.getType(name),
deprecationReason: undefined,
astNode: undefined,
};

export const TypeNameMetaFieldDef: GraphQLField<mixed, mixed> = {
Expand All @@ -454,6 +465,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

0 comments on commit df05745

Please sign in to comment.