Skip to content

Commit

Permalink
fix(default-value): properly build schema default values
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Feb 28, 2021
1 parent da90e13 commit 5f864a2
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@
- allow defining directives for interface types and theirs fields, with inheritance for object types fields (#744)
### Fixes
- allow defining extension on field resolver level for fields also defined as a property of the class (#776)
- fix throwing error when schema with dynamic default value was built again (#787)
### Others
- **Breaking Change**: update `graphql-js` peer dependency to `^15.4.0`

Expand Down
30 changes: 14 additions & 16 deletions src/schema/schema-generator.ts
Expand Up @@ -168,13 +168,15 @@ export abstract class SchemaGenerator {
defaultValueFromInitializer !== undefined &&
typeOptions.defaultValue !== defaultValueFromInitializer
) {
// console.log("ConflictingDefaultValuesError", { typeOptions, defaultValueFromInitializer });
throw new ConflictingDefaultValuesError(
typeName,
fieldName,
typeOptions.defaultValue,
defaultValueFromInitializer,
);
}
// console.log("normal", { typeOptions, defaultValueFromInitializer });
return typeOptions.defaultValue !== undefined
? typeOptions.defaultValue
: defaultValueFromInitializer;
Expand Down Expand Up @@ -506,23 +508,21 @@ export abstract class SchemaGenerator {
fields: () => {
let fields = inputType.fields!.reduce<GraphQLInputFieldConfigMap>(
(fieldsMap, field) => {
field.typeOptions.defaultValue = this.getDefaultValue(
const defaultValue = this.getDefaultValue(
inputInstance,
field.typeOptions,
field.name,
inputType.name,
);

const type = this.getGraphQLInputType(
field.target,
field.name,
field.getType(),
field.typeOptions,
);
const type = this.getGraphQLInputType(field.target, field.name, field.getType(), {
...field.typeOptions,
defaultValue,
});
fieldsMap[field.schemaName] = {
description: field.description,
type,
defaultValue: field.typeOptions.defaultValue,
defaultValue,
astNode: getInputValueDefinitionNode(field.name, type, field.directives),
extensions: field.extensions,
};
Expand Down Expand Up @@ -749,21 +749,19 @@ export abstract class SchemaGenerator {
) {
const argumentInstance = new (argumentType.target as any)();
argumentType.fields!.forEach(field => {
field.typeOptions.defaultValue = this.getDefaultValue(
const defaultValue = this.getDefaultValue(
argumentInstance,
field.typeOptions,
field.name,
argumentType.name,
);
args[field.schemaName] = {
description: field.description,
type: this.getGraphQLInputType(
field.target,
field.name,
field.getType(),
field.typeOptions,
),
defaultValue: field.typeOptions.defaultValue,
type: this.getGraphQLInputType(field.target, field.name, field.getType(), {
...field.typeOptions,
defaultValue,
}),
defaultValue,
};
});
}
Expand Down
49 changes: 49 additions & 0 deletions tests/functional/default-values.ts
@@ -0,0 +1,49 @@
import "reflect-metadata";
import { IntrospectionSchema } from "graphql";
import {
Arg,
buildSchema,
ClassType,
Field,
getMetadataStorage,
InputType,
Query,
Resolver,
} from "../../src";

import { getSchemaInfo } from "../helpers/getSchemaInfo";

describe("default values", () => {
describe("dynamic default value", () => {
let schemaIntrospection: IntrospectionSchema;
let sampleResolver: ClassType;
beforeAll(async () => {
getMetadataStorage().clear();

@InputType()
class SampleInput {
@Field()
sampleField: Date = new Date();
}

@Resolver()
class SampleResolver {
@Query()
sampleQuery(@Arg("input") input: SampleInput): string {
return "sampleQuery";
}
}
sampleResolver = SampleResolver;

// get builded schema info from retrospection
const schemaInfo = await getSchemaInfo({
resolvers: [SampleResolver],
});
schemaIntrospection = schemaInfo.schemaIntrospection;
});

it("should not throw error when schema with dynamic default has been built again", async () => {
await expect(buildSchema({ resolvers: [sampleResolver] })).resolves.not.toThrowError();
});
});
});

0 comments on commit 5f864a2

Please sign in to comment.