diff --git a/packages/graphqlgen/src/generators/ts-generator.ts b/packages/graphqlgen/src/generators/ts-generator.ts index 4eef63d5..dda038d3 100644 --- a/packages/graphqlgen/src/generators/ts-generator.ts +++ b/packages/graphqlgen/src/generators/ts-generator.ts @@ -14,7 +14,6 @@ import { renderEnums, groupModelsNameByImportPath, } from './common' -import { TypeAliasDefinition } from '../introspection/types' import { upperFirst } from '../utils' export function format(code: string, options: prettier.Options = {}) { @@ -68,7 +67,7 @@ export function generate(args: GenerateArgs): string { return `\ ${renderHeader(args)} - + ${renderEnums(args)} ${renderNamespaces(args, typeToInputTypeAssociation, inputTypesMap)} @@ -80,14 +79,6 @@ export function generate(args: GenerateArgs): string { function renderHeader(args: GenerateArgs): string { const modelsToImport = Object.keys(args.modelMap) - .filter(modelName => { - const modelDef = args.modelMap[modelName].definition - - return !( - modelDef.kind === 'TypeAliasDefinition' && - (modelDef as TypeAliasDefinition).isEnum - ) - }) .map(modelName => args.modelMap[modelName]) const modelsByImportPaths = groupModelsNameByImportPath(modelsToImport) diff --git a/packages/graphqlgen/src/tests/fixtures/enum/schema.graphql b/packages/graphqlgen/src/tests/fixtures/enum/schema.graphql index d10b3e9e..5a850c8d 100644 --- a/packages/graphqlgen/src/tests/fixtures/enum/schema.graphql +++ b/packages/graphqlgen/src/tests/fixtures/enum/schema.graphql @@ -1,21 +1,26 @@ type User { id: ID! name: String! - enumAnnotation: EnumAnnotation! - enumAsUnionType: EnumAsUnionType! + color: Color! + role: Role! + permissions: Permissions! } -enum EnumAnnotation { +type Permissions { + isAdmin: Boolean +} + +enum Role { EDITOR COLLABORATOR } -enum EnumAsUnionType { +enum Color { RED GREEN BLUE } type Query { - createUser(name: String!, type: EnumAnnotation!): User + createUser(name: String!, type: Role!): User } diff --git a/packages/graphqlgen/src/tests/fixtures/enum/types-flow.js b/packages/graphqlgen/src/tests/fixtures/enum/types-flow.js index bcae1e87..ed6d0718 100644 --- a/packages/graphqlgen/src/tests/fixtures/enum/types-flow.js +++ b/packages/graphqlgen/src/tests/fixtures/enum/types-flow.js @@ -3,9 +3,10 @@ export interface User { id: string, name: string, - enumAnnotation: EnumAnnotation, - enumAsUnionType: EnumAsUnionType, + color: Color, + role: Permissions, + permissions: Permissions, } -type EnumAnnotation = 'ADMIN' | 'EDITOR' | 'COLLABORATOR' -type EnumAsUnionType = 'RED' | 'GREEN' | 'BLUE' +export type Permissions = 'ADMIN' | 'EDITOR' | 'COLLABORATOR' +export type Color = 'RED' | 'GREEN' | 'BLUE' diff --git a/packages/graphqlgen/src/tests/fixtures/enum/types.ts b/packages/graphqlgen/src/tests/fixtures/enum/types.ts index 091a2f02..fa24ca85 100644 --- a/packages/graphqlgen/src/tests/fixtures/enum/types.ts +++ b/packages/graphqlgen/src/tests/fixtures/enum/types.ts @@ -1,14 +1,15 @@ export interface User { id: string name: string - enumAnnotation: EnumAnnotation - enumAsUnionType: EnumAsUnionType + color: Color + role: Permissions + permissions: Permissions } -enum EnumAnnotation { +export enum Permissions { ADMIN, EDITOR, COLLABORATOR, } -type EnumAsUnionType = 'RED' | 'GREEN' | 'BLUE' +export type Color = 'RED' | 'GREEN' | 'BLUE' diff --git a/packages/graphqlgen/src/tests/flow/__snapshots__/basic.test.ts.snap b/packages/graphqlgen/src/tests/flow/__snapshots__/basic.test.ts.snap index 124e009d..5d220d56 100644 --- a/packages/graphqlgen/src/tests/flow/__snapshots__/basic.test.ts.snap +++ b/packages/graphqlgen/src/tests/flow/__snapshots__/basic.test.ts.snap @@ -5,18 +5,18 @@ exports[`basic enum 1`] = ` // Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import type { GraphQLResolveInfo } from \\"graphql\\"; -import type { User } from \\"../../../fixtures/enum/types-flow\\"; +import type { User, Permissions } from \\"../../../fixtures/enum/types-flow\\"; type Context = any; -type EnumAnnotation = \\"EDITOR\\" | \\"COLLABORATOR\\"; -type EnumAsUnionType = \\"RED\\" | \\"GREEN\\" | \\"BLUE\\"; +type Role = \\"EDITOR\\" | \\"COLLABORATOR\\"; +type Color = \\"RED\\" | \\"GREEN\\" | \\"BLUE\\"; // Types for Query export const Query_defaultResolvers = {}; export interface Query_Args_CreateUser { name: string; - type: EnumAnnotation; + type: Role; } export type Query_CreateUser_Resolver = ( @@ -39,7 +39,8 @@ export interface Query_Resolvers { export const User_defaultResolvers = { id: (parent: User) => parent.id, name: (parent: User) => parent.name, - enumAsUnionType: (parent: User) => parent.enumAsUnionType + color: (parent: User) => parent.color, + permissions: (parent: User) => parent.permissions }; export type User_Id_Resolver = ( @@ -56,19 +57,26 @@ export type User_Name_Resolver = ( info: GraphQLResolveInfo ) => string | Promise; -export type User_EnumAnnotation_Resolver = ( +export type User_Color_Resolver = ( parent: User, args: {}, ctx: Context, info: GraphQLResolveInfo -) => EnumAnnotation | Promise; +) => Color | Promise; -export type User_EnumAsUnionType_Resolver = ( +export type User_Role_Resolver = ( parent: User, args: {}, ctx: Context, info: GraphQLResolveInfo -) => EnumAsUnionType | Promise; +) => Role | Promise; + +export type User_Permissions_Resolver = ( + parent: User, + args: {}, + ctx: Context, + info: GraphQLResolveInfo +) => Permissions | Promise; export interface User_Resolvers { id: ( @@ -85,24 +93,51 @@ export interface User_Resolvers { info: GraphQLResolveInfo ) => string | Promise; - enumAnnotation: ( + color: ( parent: User, args: {}, ctx: Context, info: GraphQLResolveInfo - ) => EnumAnnotation | Promise; + ) => Color | Promise; - enumAsUnionType: ( + role: ( parent: User, args: {}, ctx: Context, info: GraphQLResolveInfo - ) => EnumAsUnionType | Promise; + ) => Role | Promise; + + permissions: ( + parent: User, + args: {}, + ctx: Context, + info: GraphQLResolveInfo + ) => Permissions | Promise; +} + +// Types for Permissions +export const Permissions_defaultResolvers = {}; + +export type Permissions_IsAdmin_Resolver = ( + parent: Permissions, + args: {}, + ctx: Context, + info: GraphQLResolveInfo +) => boolean | null | Promise; + +export interface Permissions_Resolvers { + isAdmin: ( + parent: Permissions, + args: {}, + ctx: Context, + info: GraphQLResolveInfo + ) => boolean | null | Promise; } export interface Resolvers { Query: Query_Resolvers; User: User_Resolvers; + Permissions: Permissions_Resolvers; } " `; @@ -117,7 +152,7 @@ import type { User_Resolvers } from \\"[TEMPLATE-INTERFACES-PATH]\\"; export const User: User_Resolvers = { ...User_defaultResolvers, - enumAnnotation: (parent, args, ctx, info) => { + role: (parent, args, ctx, info) => { throw new Error(\\"Resolver not implemented\\"); } }; @@ -127,6 +162,22 @@ export const User: User_Resolvers = { }, Object { "code": "/* @flow */ +import { Permissions_defaultResolvers } from \\"[TEMPLATE-INTERFACES-PATH]\\"; +import type { Permissions_Resolvers } from \\"[TEMPLATE-INTERFACES-PATH]\\"; + +export const Permissions: Permissions_Resolvers = { + ...Permissions_defaultResolvers, + + isAdmin: (parent, args, ctx, info) => { + throw new Error(\\"Resolver not implemented\\"); + } +}; +", + "force": false, + "path": "Permissions.js", + }, + Object { + "code": "/* @flow */ import type { Query_Resolvers } from \\"[TEMPLATE-INTERFACES-PATH]\\"; export const Query: Query_Resolvers = { @@ -147,10 +198,12 @@ import type { Resolvers } from \\"[TEMPLATE-INTERFACES-PATH]\\"; import { Query } from \\"./Query\\"; import { User } from \\"./User\\"; +import { Permissions } from \\"./Permissions\\"; export const resolvers: Resolvers = { Query, - User + User, + Permissions }; ", "force": false, diff --git a/packages/graphqlgen/src/tests/typescript/__snapshots__/basic.test.ts.snap b/packages/graphqlgen/src/tests/typescript/__snapshots__/basic.test.ts.snap index 58d267cd..efd63bdc 100644 --- a/packages/graphqlgen/src/tests/typescript/__snapshots__/basic.test.ts.snap +++ b/packages/graphqlgen/src/tests/typescript/__snapshots__/basic.test.ts.snap @@ -4,18 +4,18 @@ exports[`basic enum 1`] = ` "// Code generated by github.com/prisma/graphqlgen, DO NOT EDIT. import { GraphQLResolveInfo } from \\"graphql\\"; -import { User } from \\"../../../fixtures/enum/types\\"; +import { User, Permissions } from \\"../../../fixtures/enum/types\\"; type Context = any; -type EnumAnnotation = \\"EDITOR\\" | \\"COLLABORATOR\\"; -type EnumAsUnionType = \\"RED\\" | \\"GREEN\\" | \\"BLUE\\"; +type Role = \\"EDITOR\\" | \\"COLLABORATOR\\"; +type Color = \\"RED\\" | \\"GREEN\\" | \\"BLUE\\"; export namespace QueryResolvers { export const defaultResolvers = {}; export interface ArgsCreateUser { name: string; - type: EnumAnnotation; + type: Role; } export type CreateUserResolver = ( @@ -39,7 +39,8 @@ export namespace UserResolvers { export const defaultResolvers = { id: (parent: User) => parent.id, name: (parent: User) => parent.name, - enumAsUnionType: (parent: User) => parent.enumAsUnionType + color: (parent: User) => parent.color, + permissions: (parent: User) => parent.permissions }; export type IdResolver = ( @@ -56,19 +57,26 @@ export namespace UserResolvers { info: GraphQLResolveInfo ) => string | Promise; - export type EnumAnnotationResolver = ( + export type ColorResolver = ( parent: User, args: {}, ctx: Context, info: GraphQLResolveInfo - ) => EnumAnnotation | Promise; + ) => Color | Promise; - export type EnumAsUnionTypeResolver = ( + export type RoleResolver = ( parent: User, args: {}, ctx: Context, info: GraphQLResolveInfo - ) => EnumAsUnionType | Promise; + ) => Role | Promise; + + export type PermissionsResolver = ( + parent: User, + args: {}, + ctx: Context, + info: GraphQLResolveInfo + ) => Permissions | Promise; export interface Type { id: ( @@ -85,25 +93,53 @@ export namespace UserResolvers { info: GraphQLResolveInfo ) => string | Promise; - enumAnnotation: ( + color: ( + parent: User, + args: {}, + ctx: Context, + info: GraphQLResolveInfo + ) => Color | Promise; + + role: ( parent: User, args: {}, ctx: Context, info: GraphQLResolveInfo - ) => EnumAnnotation | Promise; + ) => Role | Promise; - enumAsUnionType: ( + permissions: ( parent: User, args: {}, ctx: Context, info: GraphQLResolveInfo - ) => EnumAsUnionType | Promise; + ) => Permissions | Promise; + } +} + +export namespace PermissionsResolvers { + export const defaultResolvers = {}; + + export type IsAdminResolver = ( + parent: Permissions, + args: {}, + ctx: Context, + info: GraphQLResolveInfo + ) => boolean | null | Promise; + + export interface Type { + isAdmin: ( + parent: Permissions, + args: {}, + ctx: Context, + info: GraphQLResolveInfo + ) => boolean | null | Promise; } } export interface Resolvers { Query: QueryResolvers.Type; User: UserResolvers.Type; + Permissions: PermissionsResolvers.Type; } " `; @@ -119,7 +155,7 @@ import { UserResolvers } from \\"[TEMPLATE-INTERFACES-PATH]\\"; export const User: UserResolvers.Type = { ...UserResolvers.defaultResolvers, - enumAnnotation: (parent, args, ctx) => { + role: (parent, args, ctx) => { throw new Error(\\"Resolver not implemented\\"); } }; @@ -131,6 +167,23 @@ export const User: UserResolvers.Type = { "code": "// This resolver file was scaffolded by github.com/prisma/graphqlgen, DO NOT EDIT. // Please do not import this file directly but copy & paste to your application code. +import { PermissionsResolvers } from \\"[TEMPLATE-INTERFACES-PATH]\\"; + +export const Permissions: PermissionsResolvers.Type = { + ...PermissionsResolvers.defaultResolvers, + + isAdmin: (parent, args, ctx) => { + throw new Error(\\"Resolver not implemented\\"); + } +}; +", + "force": false, + "path": "Permissions.ts", + }, + Object { + "code": "// This resolver file was scaffolded by github.com/prisma/graphqlgen, DO NOT EDIT. +// Please do not import this file directly but copy & paste to your application code. + import { QueryResolvers } from \\"[TEMPLATE-INTERFACES-PATH]\\"; export const Query: QueryResolvers.Type = { @@ -151,10 +204,12 @@ import { Resolvers } from \\"[TEMPLATE-INTERFACES-PATH]\\"; import { Query } from \\"./Query\\"; import { User } from \\"./User\\"; +import { Permissions } from \\"./Permissions\\"; export const resolvers: Resolvers = { Query, - User + User, + Permissions }; ", "force": false,