Skip to content

Commit

Permalink
fix(scalars): fix converting object scalars to instance
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalLytek committed Apr 24, 2018
1 parent ad1e304 commit 4176ed2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
### Features
- add support for creating and attaching middlewares, guards and interceptors to fields and resolvers
- **Breaking Change**: remove deprecated decorators with `GraphQL` prefix and `{ array: true }` type option
### Fixes
- fix bug when converting object scalars to target class instance (#65)

## v0.10.0
### Features
Expand Down
6 changes: 5 additions & 1 deletion src/helpers/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,11 @@ export function convertToType(Target: any, data?: object): object | undefined {
if (data == null) {
return;
}
// skip simple types
// skip converting scalars (object scalar mostly)
if (Target instanceof GraphQLScalarType) {
return data;
}
// skip converting simple types
if (simpleTypes.includes(data.constructor)) {
return data;
}
Expand Down
20 changes: 19 additions & 1 deletion tests/functional/scalars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
GraphQLTimestamp,
} from "../../src";
import { getSchemaInfo } from "../helpers/getSchemaInfo";
import { CustomScalar, CustomType } from "../helpers/customScalar";
import { CustomScalar, CustomType, ObjectScalar } from "../helpers/customScalar";
import { getSampleObjectFieldType } from "../helpers/getSampleObjectFieldType";
import { MetadataStorage } from "../../src/metadata/metadata-storage";

Expand Down Expand Up @@ -103,6 +103,15 @@ describe("Scalars", () => {
return true;
}

@Query(returns => Boolean)
objectArgScalar(
@Arg("scalar", type => ObjectScalar)
scalar: any,
): any {
argScalar = scalar;
return true;
}

@Query(returns => Date)
returnDate(): any {
return new Date();
Expand Down Expand Up @@ -237,6 +246,15 @@ describe("Scalars", () => {

expect(argScalar!).toEqual("TypeGraphQL parseLiteral");
});

it("should properly parse scalar object", async () => {
const query = `query {
objectArgScalar(scalar: "test")
}`;
await graphql(schema, query);

expect(argScalar!).toEqual({ value: "TypeGraphQL parseLiteral" });
});
});

describe("Bulit-in scalars", () => {
Expand Down
11 changes: 11 additions & 0 deletions tests/helpers/customScalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@ export const CustomScalar = new GraphQLScalarType({
serialize: () => "TypeGraphQL serialize",
});
export class CustomType {}

export const ObjectScalar = new GraphQLScalarType({
name: "ObjectScalar",
parseLiteral: () => ({
value: "TypeGraphQL parseLiteral",
}),
parseValue: () => ({
value: "TypeGraphQL parseValue",
}),
serialize: obj => obj.value,
});

0 comments on commit 4176ed2

Please sign in to comment.