Skip to content

Directive for Oneof Objects and Oneof Input Objects #1

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ words:
- graphiql
- sublinks
- instanceof
- oneof

# Different names used inside tests
- Skywalker
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ export {
GraphQLSkipDirective,
GraphQLDeprecatedDirective,
GraphQLSpecifiedByDirective,
GraphQLOneOfDirective,
// "Enum" of Type Kinds
TypeKind,
// Constant Deprecation Reason
Expand Down
95 changes: 95 additions & 0 deletions src/type/__tests__/introspection-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,17 @@ describe('Introspection', () => {
isDeprecated: false,
deprecationReason: null,
},
{
name: 'oneOf',
args: [],
type: {
kind: 'SCALAR',
name: 'Boolean',
ofType: null,
},
isDeprecated: false,
deprecationReason: null,
},
],
inputFields: null,
interfaces: [],
Expand Down Expand Up @@ -989,6 +1000,12 @@ describe('Introspection', () => {
},
],
},
{
name: 'oneOf',
isRepeatable: false,
locations: ['OBJECT', 'INPUT_OBJECT'],
args: [],
},
],
},
},
Expand Down Expand Up @@ -1519,6 +1536,84 @@ describe('Introspection', () => {
});
});

it('identifies oneOf for objects', () => {
const schema = buildSchema(`
type SomeObject @oneOf {
a: String
}

type AnotherObject {
a: String
b: String
}

type Query {
someField: String
}
`);

const source = `
{
a: __type(name: "SomeObject") {
oneOf
}
b: __type(name: "AnotherObject") {
oneOf
}
}
`;

expect(graphqlSync({ schema, source })).to.deep.equal({
data: {
a: {
oneOf: true,
},
b: {
oneOf: false,
},
},
});
});

it('identifies oneOf for input objects', () => {
const schema = buildSchema(`
input SomeInputObject @oneOf {
a: String
}

input AnotherInputObject {
a: String
b: String
}

type Query {
someField(someArg: SomeInputObject): String
}
`);

const source = `
{
a: __type(name: "SomeInputObject") {
oneOf
}
b: __type(name: "AnotherInputObject") {
oneOf
}
}
`;

expect(graphqlSync({ schema, source })).to.deep.equal({
data: {
a: {
oneOf: true,
},
b: {
oneOf: false,
},
},
});
});

it('fails as expected on the __type root field without an arg', () => {
const schema = buildSchema(`
type Query {
Expand Down
65 changes: 64 additions & 1 deletion src/type/__tests__/validation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ describe('Type System: A Schema must have Object root types', () => {
]);
});

it('rejects a schema extended with invalid root types', () => {
it('rejects a Schema extended with invalid root types', () => {
let schema = buildSchema(`
input SomeInputObject {
test: String
Expand Down Expand Up @@ -1663,6 +1663,69 @@ describe('Type System: Input Object fields must have input types', () => {
});
});

describe('Type System: Oneof Object fields must be nullable', () => {
it('rejects non-nullable fields', () => {
const schema = buildSchema(`
type Query {
test: SomeObject
}

type SomeObject @oneOf {
a: String
b: String!
}
`);
expectJSON(validateSchema(schema)).toDeepEqual([
{
message:
'Field SomeObject.b must be nullable as it is part of a Oneof Type.',
locations: [{ line: 8, column: 12 }],
},
]);
});
});

describe('Type System: Oneof Input Object fields must be nullable', () => {
it('rejects non-nullable fields', () => {
const schema = buildSchema(`
type Query {
test(arg: SomeInputObject): String
}

input SomeInputObject @oneOf {
a: String
b: String!
}
`);
expectJSON(validateSchema(schema)).toDeepEqual([
{
message: 'Oneof input field SomeInputObject.b must be nullable.',
locations: [{ line: 8, column: 12 }],
},
]);
});

it('rejects fields with default values', () => {
const schema = buildSchema(`
type Query {
test(arg: SomeInputObject): String
}

input SomeInputObject @oneOf {
a: String
b: String = "foo"
}
`);
expectJSON(validateSchema(schema)).toDeepEqual([
{
message:
'Oneof input field SomeInputObject.b cannot have a default value.',
locations: [{ line: 8, column: 9 }],
},
]);
});
});

describe('Objects must adhere to Interface they implement', () => {
it('accepts an Object which implements an Interface', () => {
const schema = buildSchema(`
Expand Down
7 changes: 7 additions & 0 deletions src/type/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,7 @@ export class GraphQLObjectType<TSource = any, TContext = any> {
extensions: Readonly<GraphQLObjectTypeExtensions<TSource, TContext>>;
astNode: Maybe<ObjectTypeDefinitionNode>;
extensionASTNodes: ReadonlyArray<ObjectTypeExtensionNode>;
isOneOf: boolean;

private _fields: ThunkObjMap<GraphQLField<TSource, TContext>>;
private _interfaces: ThunkReadonlyArray<GraphQLInterfaceType>;
Expand All @@ -768,6 +769,7 @@ export class GraphQLObjectType<TSource = any, TContext = any> {
this.extensions = toObjMap(config.extensions);
this.astNode = config.astNode;
this.extensionASTNodes = config.extensionASTNodes ?? [];
this.isOneOf = config.isOneOf ?? false;

this._fields = () => defineFieldMap(config);
this._interfaces = () => defineInterfaces(config);
Expand Down Expand Up @@ -936,6 +938,7 @@ export interface GraphQLObjectTypeConfig<TSource, TContext> {
extensions?: Maybe<Readonly<GraphQLObjectTypeExtensions<TSource, TContext>>>;
astNode?: Maybe<ObjectTypeDefinitionNode>;
extensionASTNodes?: Maybe<ReadonlyArray<ObjectTypeExtensionNode>>;
isOneOf?: boolean;
}

interface GraphQLObjectTypeNormalizedConfig<TSource, TContext>
Expand Down Expand Up @@ -1605,6 +1608,7 @@ export class GraphQLInputObjectType {
extensions: Readonly<GraphQLInputObjectTypeExtensions>;
astNode: Maybe<InputObjectTypeDefinitionNode>;
extensionASTNodes: ReadonlyArray<InputObjectTypeExtensionNode>;
isOneOf: boolean;

private _fields: ThunkObjMap<GraphQLInputField>;

Expand All @@ -1614,6 +1618,7 @@ export class GraphQLInputObjectType {
this.extensions = toObjMap(config.extensions);
this.astNode = config.astNode;
this.extensionASTNodes = config.extensionASTNodes ?? [];
this.isOneOf = config.isOneOf ?? false;

this._fields = defineInputFieldMap.bind(undefined, config);
}
Expand Down Expand Up @@ -1646,6 +1651,7 @@ export class GraphQLInputObjectType {
extensions: this.extensions,
astNode: this.astNode,
extensionASTNodes: this.extensionASTNodes,
isOneOf: this.isOneOf,
};
}

Expand Down Expand Up @@ -1691,6 +1697,7 @@ export interface GraphQLInputObjectTypeConfig {
extensions?: Maybe<Readonly<GraphQLInputObjectTypeExtensions>>;
astNode?: Maybe<InputObjectTypeDefinitionNode>;
extensionASTNodes?: Maybe<ReadonlyArray<InputObjectTypeExtensionNode>>;
isOneOf?: boolean;
}

interface GraphQLInputObjectTypeNormalizedConfig
Expand Down
12 changes: 12 additions & 0 deletions src/type/directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,17 @@ export const GraphQLSpecifiedByDirective: GraphQLDirective =
},
});

/**
* Used to declare an Input Object as a Oneof Input Objects and an Object as a Oneof Object.
*/
export const GraphQLOneOfDirective: GraphQLDirective = new GraphQLDirective({
name: 'oneOf',
description:
'Indicates an Object is a Oneof Object or an Input Object is a Oneof Input Object.',
locations: [DirectiveLocation.OBJECT, DirectiveLocation.INPUT_OBJECT],
args: {},
});

/**
* The full list of specified directives.
*/
Expand All @@ -218,6 +229,7 @@ export const specifiedDirectives: ReadonlyArray<GraphQLDirective> =
GraphQLSkipDirective,
GraphQLDeprecatedDirective,
GraphQLSpecifiedByDirective,
GraphQLOneOfDirective,
]);

export function isSpecifiedDirective(directive: GraphQLDirective): boolean {
Expand Down
1 change: 1 addition & 0 deletions src/type/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export {
GraphQLSkipDirective,
GraphQLDeprecatedDirective,
GraphQLSpecifiedByDirective,
GraphQLOneOfDirective,
// Constant Deprecation Reason
DEFAULT_DEPRECATION_REASON,
} from './directives';
Expand Down
10 changes: 10 additions & 0 deletions src/type/introspection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,16 @@ export const __Type: GraphQLObjectType = new GraphQLObjectType({
type: __Type,
resolve: (type) => ('ofType' in type ? type.ofType : undefined),
},
oneOf: {
type: GraphQLBoolean,
resolve: (type) => {
if (isInputObjectType(type) || isObjectType(type)) {
return type.isOneOf;
}

return null;
},
},
} as GraphQLFieldConfigMap<GraphQLType, unknown>),
});

Expand Down
42 changes: 42 additions & 0 deletions src/type/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { isEqualType, isTypeSubTypeOf } from '../utilities/typeComparators';

import type {
GraphQLEnumType,
GraphQLField,
GraphQLInputField,
GraphQLInputObjectType,
GraphQLInterfaceType,
Expand Down Expand Up @@ -308,6 +309,23 @@ function validateFields(
);
}
}

if (isObjectType(type) && type.isOneOf) {
validateOneOfObjectField(type, field, context);
}
}
}

function validateOneOfObjectField(
type: GraphQLObjectType,
field: GraphQLField<unknown, unknown, unknown>,
context: SchemaValidationContext,
): void {
if (isNonNullType(field.type)) {
context.reportError(
`Field ${type.name}.${field.name} must be nullable as it is part of a Oneof Type.`,
field.astNode?.type,
);
}
}

Expand Down Expand Up @@ -531,6 +549,30 @@ function validateInputFields(
[getDeprecatedDirectiveNode(field.astNode), field.astNode?.type],
);
}

if (inputObj.isOneOf) {
validateOneOfInputObjectField(inputObj, field, context);
}
}
}

function validateOneOfInputObjectField(
type: GraphQLInputObjectType,
field: GraphQLInputField,
context: SchemaValidationContext,
): void {
if (isNonNullType(field.type)) {
context.reportError(
`Oneof input field ${type.name}.${field.name} must be nullable.`,
field.astNode?.type,
);
}

if (field.defaultValue) {
context.reportError(
`Oneof input field ${type.name}.${field.name} cannot have a default value.`,
field.astNode,
);
}
}

Expand Down
Loading