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

Make 'extensions' non-optional in schema types #3279

Merged
merged 1 commit into from Sep 29, 2021
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
8 changes: 4 additions & 4 deletions integrationTests/ts/index.ts
Expand Up @@ -61,12 +61,12 @@ const schema: GraphQLSchema = new GraphQLSchema({

function checkExtensionTypes(_test: SomeExtension | null | undefined) {}

checkExtensionTypes(queryType?.extensions?.someObjectExtension);
checkExtensionTypes(queryType.extensions.someObjectExtension);

const sayHiField = queryType?.getFields()?.sayHi;
checkExtensionTypes(sayHiField?.extensions?.someFieldExtension);
const sayHiField = queryType.getFields().sayHi;
checkExtensionTypes(sayHiField.extensions.someFieldExtension);

checkExtensionTypes(sayHiField?.args?.[0]?.extensions?.someArgumentExtension);
checkExtensionTypes(sayHiField.args[0].extensions.someArgumentExtension);

const result: ExecutionResult = graphqlSync({
schema,
Expand Down
12 changes: 12 additions & 0 deletions src/jsutils/__tests__/toObjMap-test.ts
Expand Up @@ -8,6 +8,18 @@ import { toObjMap } from '../toObjMap';
const __proto__ = '__proto__';

describe('toObjMap', () => {
it('convert undefined to ObjMap', () => {
const result = toObjMap(undefined);
expect(result).to.deep.equal({});
expect(Object.getPrototypeOf(result)).to.equal(null);
});

it('convert null to ObjMap', () => {
const result = toObjMap(null);
expect(result).to.deep.equal({});
expect(Object.getPrototypeOf(result)).to.equal(null);
});

it('convert empty object to ObjMap', () => {
const result = toObjMap({});
expect(result).to.deep.equal({});
Expand Down
18 changes: 9 additions & 9 deletions src/jsutils/toObjMap.ts
@@ -1,13 +1,13 @@
import type {
ObjMap,
ObjMapLike,
ReadOnlyObjMap,
ReadOnlyObjMapLike,
} from './ObjMap';
import type { Maybe } from './Maybe';
import type { ReadOnlyObjMap, ReadOnlyObjMapLike } from './ObjMap';

export function toObjMap<T>(
obj: Maybe<ReadOnlyObjMapLike<T>>,
): ReadOnlyObjMap<T> {
if (obj == null) {
return Object.create(null);
}

export function toObjMap<T>(obj: ObjMapLike<T>): ObjMap<T>;
export function toObjMap<T>(obj: ReadOnlyObjMapLike<T>): ReadOnlyObjMap<T>;
export function toObjMap<T>(obj: ObjMapLike<T> | ReadOnlyObjMapLike<T>) {
if (Object.getPrototypeOf(obj) === null) {
return obj;
}
Expand Down
16 changes: 8 additions & 8 deletions src/type/__tests__/definition-test.ts
Expand Up @@ -250,7 +250,7 @@ describe('Type System: Objects', () => {
resolve: undefined,
subscribe: undefined,
deprecationReason: undefined,
extensions: undefined,
extensions: {},
astNode: undefined,
},
});
Expand Down Expand Up @@ -280,14 +280,14 @@ describe('Type System: Objects', () => {
type: ScalarType,
defaultValue: undefined,
deprecationReason: undefined,
extensions: undefined,
extensions: {},
astNode: undefined,
},
],
resolve: undefined,
subscribe: undefined,
deprecationReason: undefined,
extensions: undefined,
extensions: {},
astNode: undefined,
},
});
Expand Down Expand Up @@ -628,23 +628,23 @@ describe('Type System: Enums', () => {
description: undefined,
value: null,
deprecationReason: undefined,
extensions: undefined,
extensions: {},
astNode: undefined,
},
{
name: 'NAN',
description: undefined,
value: NaN,
deprecationReason: undefined,
extensions: undefined,
extensions: {},
astNode: undefined,
},
{
name: 'NO_CUSTOM_VALUE',
description: undefined,
value: 'NO_CUSTOM_VALUE',
deprecationReason: undefined,
extensions: undefined,
extensions: {},
astNode: undefined,
},
]);
Expand Down Expand Up @@ -735,7 +735,7 @@ describe('Type System: Input Objects', () => {
type: ScalarType,
defaultValue: undefined,
deprecationReason: undefined,
extensions: undefined,
extensions: {},
astNode: undefined,
},
});
Expand All @@ -754,7 +754,7 @@ describe('Type System: Input Objects', () => {
description: undefined,
type: ScalarType,
defaultValue: undefined,
extensions: undefined,
extensions: {},
deprecationReason: undefined,
astNode: undefined,
},
Expand Down
4 changes: 2 additions & 2 deletions src/type/__tests__/directive-test.ts
Expand Up @@ -38,7 +38,7 @@ describe('Type System: Directive', () => {
type: GraphQLString,
defaultValue: undefined,
deprecationReason: undefined,
extensions: undefined,
extensions: {},
astNode: undefined,
},
{
Expand All @@ -47,7 +47,7 @@ describe('Type System: Directive', () => {
type: GraphQLInt,
defaultValue: undefined,
deprecationReason: undefined,
extensions: undefined,
extensions: {},
astNode: undefined,
},
],
Expand Down
4 changes: 2 additions & 2 deletions src/type/__tests__/enumType-test.ts
Expand Up @@ -348,15 +348,15 @@ describe('Type System: Enum Values', () => {
description: undefined,
value: Complex1,
deprecationReason: undefined,
extensions: undefined,
extensions: {},
astNode: undefined,
},
{
name: 'TWO',
description: undefined,
value: Complex2,
deprecationReason: undefined,
extensions: undefined,
extensions: {},
astNode: undefined,
},
]);
Expand Down
60 changes: 30 additions & 30 deletions src/type/__tests__/extensions-test.ts
Expand Up @@ -26,10 +26,10 @@ describe('Type System: Extensions', () => {
describe('GraphQLScalarType', () => {
it('without extensions', () => {
const someScalar = new GraphQLScalarType({ name: 'SomeScalar' });
expect(someScalar.extensions).to.equal(undefined);
expect(someScalar.extensions).to.deep.equal({});

const config = someScalar.toConfig();
expect(config.extensions).to.equal(undefined);
expect(config.extensions).to.deep.equal({});
});

it('with extensions', () => {
Expand Down Expand Up @@ -62,19 +62,19 @@ describe('Type System: Extensions', () => {
},
});

expect(someObject.extensions).to.equal(undefined);
expect(someObject.extensions).to.deep.equal({});
const someField = someObject.getFields().someField;
expect(someField.extensions).to.equal(undefined);
expect(someField.extensions).to.deep.equal({});
const someArg = someField.args[0];
expect(someArg.extensions).to.equal(undefined);
expect(someArg.extensions).to.deep.equal({});

const config = someObject.toConfig();
expect(config.extensions).to.equal(undefined);
expect(config.extensions).to.deep.equal({});
const someFieldConfig = config.fields.someField;
expect(someFieldConfig.extensions).to.equal(undefined);
expect(someFieldConfig.extensions).to.deep.equal({});
invariant(someFieldConfig.args);
const someArgConfig = someFieldConfig.args.someArg;
expect(someArgConfig.extensions).to.equal(undefined);
expect(someArgConfig.extensions).to.deep.equal({});
});

it('with extensions', () => {
Expand Down Expand Up @@ -131,19 +131,19 @@ describe('Type System: Extensions', () => {
},
});

expect(someInterface.extensions).to.equal(undefined);
expect(someInterface.extensions).to.deep.equal({});
const someField = someInterface.getFields().someField;
expect(someField.extensions).to.equal(undefined);
expect(someField.extensions).to.deep.equal({});
const someArg = someField.args[0];
expect(someArg.extensions).to.equal(undefined);
expect(someArg.extensions).to.deep.equal({});

const config = someInterface.toConfig();
expect(config.extensions).to.equal(undefined);
expect(config.extensions).to.deep.equal({});
const someFieldConfig = config.fields.someField;
expect(someFieldConfig.extensions).to.equal(undefined);
expect(someFieldConfig.extensions).to.deep.equal({});
invariant(someFieldConfig.args);
const someArgConfig = someFieldConfig.args.someArg;
expect(someArgConfig.extensions).to.equal(undefined);
expect(someArgConfig.extensions).to.deep.equal({});
});

it('with extensions', () => {
Expand Down Expand Up @@ -193,10 +193,10 @@ describe('Type System: Extensions', () => {
types: [],
});

expect(someUnion.extensions).to.equal(undefined);
expect(someUnion.extensions).to.deep.equal({});

const config = someUnion.toConfig();
expect(config.extensions).to.equal(undefined);
expect(config.extensions).to.deep.equal({});
});

it('with extensions', () => {
Expand Down Expand Up @@ -224,14 +224,14 @@ describe('Type System: Extensions', () => {
},
});

expect(someEnum.extensions).to.equal(undefined);
expect(someEnum.extensions).to.deep.equal({});
const someValue = someEnum.getValues()[0];
expect(someValue.extensions).to.equal(undefined);
expect(someValue.extensions).to.deep.equal({});

const config = someEnum.toConfig();
expect(config.extensions).to.equal(undefined);
expect(config.extensions).to.deep.equal({});
const someValueConfig = config.values.SOME_VALUE;
expect(someValueConfig.extensions).to.equal(undefined);
expect(someValueConfig.extensions).to.deep.equal({});
});

it('with extensions', () => {
Expand Down Expand Up @@ -270,14 +270,14 @@ describe('Type System: Extensions', () => {
},
});

expect(someInputObject.extensions).to.equal(undefined);
expect(someInputObject.extensions).to.deep.equal({});
const someInputField = someInputObject.getFields().someInputField;
expect(someInputField.extensions).to.equal(undefined);
expect(someInputField.extensions).to.deep.equal({});

const config = someInputObject.toConfig();
expect(config.extensions).to.equal(undefined);
expect(config.extensions).to.deep.equal({});
const someInputFieldConfig = config.fields.someInputField;
expect(someInputFieldConfig.extensions).to.equal(undefined);
expect(someInputFieldConfig.extensions).to.deep.equal({});
});

it('with extensions', () => {
Expand Down Expand Up @@ -328,14 +328,14 @@ describe('Type System: Extensions', () => {
locations: [],
});

expect(someDirective.extensions).to.equal(undefined);
expect(someDirective.extensions).to.deep.equal({});
const someArg = someDirective.args[0];
expect(someArg.extensions).to.equal(undefined);
expect(someArg.extensions).to.deep.equal({});

const config = someDirective.toConfig();
expect(config.extensions).to.equal(undefined);
expect(config.extensions).to.deep.equal({});
const someArgConfig = config.args.someArg;
expect(someArgConfig.extensions).to.equal(undefined);
expect(someArgConfig.extensions).to.deep.equal({});
});

it('with extensions', () => {
Expand Down Expand Up @@ -371,10 +371,10 @@ describe('Type System: Extensions', () => {
it('without extensions', () => {
const schema = new GraphQLSchema({});

expect(schema.extensions).to.equal(undefined);
expect(schema.extensions).to.deep.equal({});

const config = schema.toConfig();
expect(config.extensions).to.equal(undefined);
expect(config.extensions).to.deep.equal({});
});

it('with extensions', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/type/__tests__/predicate-test.ts
Expand Up @@ -573,7 +573,7 @@ describe('Type predicates', () => {
description: undefined,
defaultValue: config.defaultValue,
deprecationReason: null,
extensions: undefined,
extensions: Object.create(null),
astNode: undefined,
};
}
Expand Down Expand Up @@ -621,7 +621,7 @@ describe('Type predicates', () => {
description: undefined,
defaultValue: config.defaultValue,
deprecationReason: null,
extensions: undefined,
extensions: Object.create(null),
astNode: undefined,
};
}
Expand Down