Skip to content
This repository has been archived by the owner on Dec 8, 2021. It is now read-only.

export enums to allow implementations conforming to them #306

Merged
merged 2 commits into from
Jan 7, 2019
Merged

export enums to allow implementations conforming to them #306

merged 2 commits into from
Jan 7, 2019

Conversation

koenpunt
Copy link
Contributor

@koenpunt koenpunt commented Nov 20, 2018

Without the enums being exported, it's not possible to create an implementation that conforms to the type, without casting to any (or redeclaring the full enum).

Fixes: #315

@Weakky
Copy link
Contributor

Weakky commented Dec 18, 2018

Hey koenpunt 👋,

Could you provide a simple example where you need to import an enum from graphqlgen.ts to implement a resolver?

@koenpunt
Copy link
Contributor Author

For example, in the screenshot below the resolver is generated with UserGender as the return type, which is a generated enum. But conforming to that type is now only possible by declaring the type again outside of the generated types.

image

Also here the issue is marked as a bug: #315

@jasonkuhrt jasonkuhrt self-requested a review January 1, 2019 15:34
@jasonkuhrt
Copy link
Member

jasonkuhrt commented Jan 1, 2019

But conforming to that type is now only possible by declaring the type again outside of the generated types.

@koenpunt I am not against exporting at all, in fact generally support it, however I would like to still better understand. Could you provide a code snippet, can be made up pseudo code, showing what your implementation would look like once this feature lands?

Thanks 🙏 !

@arianon
Copy link

arianon commented Jan 5, 2019

I want this so I can do type-safe switch statements, something like this.

type Query {
  products(orderBy: Order!): [Product!]!
}

enum Order {
  BESTSELLING
  NEWEST
  OLDEST
  ALPHABETIC
  HIGHESTPRICE
  LOWESTPRICE
}
import { Product } from "../types";
import { QueryResolvers, Order } from "../generated/graphqlgen";

const Query: QueryResolvers.Type = {
  async products(parent, { orderBy }) {
    const ps = await getProducts();
    return sortProducts(ps, orderBy);
  }
};

async function sortProducts(ps: Product[], orderBy: Order) {
  switch (order) {
    case "BESTSELLING": // ...
    case "NEWEST": // ...
    case "OLDEST": // ...
    // I want TS to throw a type error if I forget a possible value.
  }
}

export default { Query };

EDIT: Whoops, changed the example to actually use the export.

@jasonkuhrt
Copy link
Member

@arianon thanks for your example!

@jasonkuhrt jasonkuhrt merged commit ab41c39 into prisma-labs:master Jan 7, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants