Skip to content

Commit

Permalink
Allow referencing backing types via rootTypings in codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
tgriesser committed Jun 11, 2019
1 parent f127e48 commit c141ce2
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 25 deletions.
5 changes: 5 additions & 0 deletions examples/githunt-api/yarn.lock
Expand Up @@ -4858,6 +4858,11 @@ trim-right@^1.0.1:
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"
integrity sha1-yy4SAwZ+DI3h9hQJS5/kVwTqYAM=

tslib@^1.9.3:
version "1.10.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==

type-is@^1.6.16, type-is@~1.6.15, type-is@~1.6.16:
version "1.6.16"
resolved "https://registry.yarnpkg.com/type-is/-/type-is-1.6.16.tgz#f89ce341541c672b25ee7ae3c73dee3b2be50194"
Expand Down
5 changes: 5 additions & 0 deletions examples/star-wars/yarn.lock
Expand Up @@ -4568,6 +4568,11 @@ tsconfig@^7.0.0:
strip-bom "^3.0.0"
strip-json-comments "^2.0.0"

tslib@^1.9.3:
version "1.10.0"
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.10.0.tgz#c3c19f95973fb0a62973fb09d90d961ee43e5c8a"
integrity sha512-qOebF53frne81cf0S9B41ByenJ3/IuH8yJKngAX35CmiZySA0khhkovshKK+jGCaMnVomla7gVlIcc3EvKPbTQ==

tunnel-agent@^0.6.0:
version "0.6.0"
resolved "https://registry.yarnpkg.com/tunnel-agent/-/tunnel-agent-0.6.0.tgz#27a5dea06b36b04a0a9966774b290868f0fc40fd"
Expand Down
2 changes: 1 addition & 1 deletion src/builder.ts
Expand Up @@ -1390,7 +1390,7 @@ export function makeSchemaInternal(
* Requires at least one type be named "Query", which will be used as the
* root query type.
*/
export function makeSchema(options: SchemaConfig): GraphQLSchema {
export function makeSchema(options: SchemaConfig): NexusSchema {
const { schema } = makeSchemaInternal(options);

// Only in development envs do we want to worry about regenerating the
Expand Down
25 changes: 11 additions & 14 deletions src/typegen.ts
Expand Up @@ -32,7 +32,7 @@ import {
mapObj,
relativePathTo,
} from "./utils";
import { WrappedResolver, RootTypingImport } from "./definitions/_types";
import { WrappedResolver } from "./definitions/_types";

const SpecifiedScalars = {
ID: "string",
Expand Down Expand Up @@ -302,7 +302,7 @@ export class Typegen {
buildEnumTypeMap() {
const enumMap: TypeMapping = {};
this.groupedTypes.enum.forEach((e) => {
const backingType = this.typegenInfo.backingTypeMap[e.name];
const backingType = this.resolveBackingType(e.name);
if (backingType) {
enumMap[e.name] = backingType;
} else {
Expand Down Expand Up @@ -349,19 +349,12 @@ export class Typegen {
.concat(this.groupedTypes.scalar)
.concat(this.groupedTypes.union)
.forEach((type) => {
const rootTyping = this.extensions.rootTypings[type.name];
const rootTyping = this.resolveBackingType(type.name);
if (rootTyping) {
if (typeof rootTyping === "string") {
rootTypeMap[type.name] = rootTyping;
} else {
rootTypeMap[type.name] = this.resolveExtensionRootType(rootTyping);
}
rootTypeMap[type.name] = rootTyping;
return;
}
const backingType = this.typegenInfo.backingTypeMap[type.name];
if (typeof backingType === "string") {
rootTypeMap[type.name] = backingType;
} else if (isScalarType(type)) {
if (isScalarType(type)) {
if (isSpecifiedScalarType(type)) {
rootTypeMap[type.name] =
SpecifiedScalars[type.name as SpecifiedScalarNames];
Expand Down Expand Up @@ -401,8 +394,12 @@ export class Typegen {
return rootTypeMap;
}

resolveExtensionRootType(rootTyping: RootTypingImport) {
return rootTyping.alias || rootTyping.name;
resolveBackingType(typeName: string): string | undefined {
const rootTyping = this.extensions.rootTypings[typeName];
if (rootTyping) {
return typeof rootTyping === "string" ? rootTyping : rootTyping.name;
}
return this.typegenInfo.backingTypeMap[typeName];
}

buildAllTypesMap() {
Expand Down
84 changes: 84 additions & 0 deletions tests/__snapshots__/backingTypes.spec.ts.snap
@@ -0,0 +1,84 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`rootTypings can import enum via rootTyping 1`] = `
"/**
* This file was automatically generated by GraphQL Nexus
* Do not make changes to this file directly
*/
import { TestEnum } from \\"./tests/backingTypes.spec\\"
declare global {
interface NexusGen extends NexusGenTypes {}
}
export interface NexusGenInputs {
}
export interface NexusGenEnums {
TestEnumType: TestEnum
}
export interface NexusGenRootTypes {
Query: {};
String: string;
Int: number;
Float: number;
Boolean: boolean;
ID: string;
}
export interface NexusGenAllTypes extends NexusGenRootTypes {
TestEnumType: NexusGenEnums['TestEnumType'];
}
export interface NexusGenFieldTypes {
Query: { // field return type
ok: boolean; // Boolean!
}
}
export interface NexusGenArgTypes {
}
export interface NexusGenAbstractResolveReturnTypes {
}
export interface NexusGenInheritedFields {}
export type NexusGenObjectNames = \\"Query\\";
export type NexusGenInputNames = never;
export type NexusGenEnumNames = \\"TestEnumType\\";
export type NexusGenInterfaceNames = never;
export type NexusGenScalarNames = \\"Boolean\\" | \\"Float\\" | \\"ID\\" | \\"Int\\" | \\"String\\";
export type NexusGenUnionNames = never;
export interface NexusGenTypes {
context: any;
inputTypes: NexusGenInputs;
rootTypes: NexusGenRootTypes;
argTypes: NexusGenArgTypes;
fieldTypes: NexusGenFieldTypes;
allTypes: NexusGenAllTypes;
inheritedFields: NexusGenInheritedFields;
objectNames: NexusGenObjectNames;
inputNames: NexusGenInputNames;
enumNames: NexusGenEnumNames;
interfaceNames: NexusGenInterfaceNames;
scalarNames: NexusGenScalarNames;
unionNames: NexusGenUnionNames;
allInputTypes: NexusGenTypes['inputNames'] | NexusGenTypes['enumNames'] | NexusGenTypes['scalarNames'];
allOutputTypes: NexusGenTypes['objectNames'] | NexusGenTypes['enumNames'] | NexusGenTypes['unionNames'] | NexusGenTypes['interfaceNames'] | NexusGenTypes['scalarNames'];
allNamedTypes: NexusGenTypes['allInputTypes'] | NexusGenTypes['allOutputTypes']
abstractTypes: NexusGenTypes['interfaceNames'] | NexusGenTypes['unionNames'];
abstractResolveReturn: NexusGenAbstractResolveReturnTypes;
}"
`;
43 changes: 33 additions & 10 deletions tests/backingTypes.spec.ts
Expand Up @@ -5,6 +5,11 @@ import { NexusSchemaExtensions } from "../src/core";

const { Typegen, TypegenMetadata } = core;

export enum TestEnum {
A = "a",
B = "b",
}

function getSchemaWithNormalEnums() {
return makeSchema({
types: [
Expand Down Expand Up @@ -61,21 +66,13 @@ describe("backingTypes", () => {
});
});

schemaExtensions = {
rootTypings: {},
dynamicFields: {
dynamicInputFields: {},
dynamicOutputFields: {},
},
};

it("can match backing types to regular enums", async () => {
const schema = getSchemaWithNormalEnums();
const typegenInfo = await metadata.getTypegenInfo(schema);
const typegen = new Typegen(
schema,
{ ...typegenInfo, typegenFile: "" },
schemaExtensions
schema.extensions.nexus
);

expect(typegen.printEnumTypeMap()).toMatchInlineSnapshot(`
Expand All @@ -91,7 +88,7 @@ describe("backingTypes", () => {
const typegen = new Typegen(
schema,
{ ...typegenInfo, typegenFile: "" },
schemaExtensions
schema.extensions.nexus
);

expect(typegen.printEnumTypeMap()).toMatchInlineSnapshot(`
Expand All @@ -101,3 +98,29 @@ describe("backingTypes", () => {
`);
});
});

describe("rootTypings", () => {
it("can import enum via rootTyping", async () => {
const metadata = new TypegenMetadata({ outputs: false });
const schema = makeSchema({
types: [
enumType({
name: "TestEnumType",
members: TestEnum,
rootTyping: {
path: __filename,
name: "TestEnum",
},
}),
],
outputs: false,
});
const typegenInfo = await metadata.getTypegenInfo(schema);
const typegen = new Typegen(
schema,
{ ...typegenInfo, typegenFile: "" },
schema.extensions.nexus
);
expect(typegen.print()).toMatchSnapshot();
});
});

0 comments on commit c141ce2

Please sign in to comment.