vadistic/graphql-extra

EnumValuesAsFields or similar mixin

GavinRay97 opened this issue · 6 comments

I just realized while working on handling codegen for enums, there isn't an equivalent API where I can sort of do the same thing with objectTypeApi().getFields().

Something like:

const myEnum = document.getEnumType(type)
const enumValues = myEnum.getValues()
// Maybe returns node.name.value and optionally node.description?

I see you are doing it manually like this in clientql:

export const printEnum = (props: CodegenProps) => (
  node: EnumTypeDefinitionNode | EnumTypeExtensionNode,
): TypescriptString => {
  const nameTs = node.name.value
  const addDescription = withDescription(props.config)

  // opting for same const & interface name for declarations merging
  if (props.config.useMapsForEnum) {
    let interTs = `export interface ${nameTs} {\n`
    let mapTs = `export const ${nameTs}: ${nameTs} = {\n`

    if (isNotEmpty(node.values)) {
      node.values.forEach(enumValue => {
        const value = enumValue.name.value

Edit: I see now that you can do this by separate use of EnumValuesApi:

case "EnumTypeDefinition":
  const enumType = enumTypeApi(astNode)
  for (let value of enumType.node.values) {
    const enumValue = enumValueApi(value)
    console.log(enumValue.getName(), enumValue.getDescription())
  }

Will try to submit a PR that adds a new property on EnumTypeApi that has array of EnumValueApi which just converts the node values this way if you'd like 👍

There is EnumValueApi but there is no - let's call it - EnumValueApiMixin for EnumTypeApi & EnumExtApi. It's should to be really simmilar to fieldDefinitions/inputValues.

Feel free to PR if you want :) Just please pull latest commit - otherwise it's going to be nightmare to merge. I've moved some stuff around...

I see you did some research on my experiments :D I did write clientqland some of simmilar stuff before this lib and my main motivation for graphql-extra is to stop rewriting literally the same graphql helpers in each project :)

I think with document operation support I could make this clientql or other generator (I'm curently flirting with something like graphql interop for https://github.com/marcj/marshal.ts) at least 2 times shorter and cleaner, so yeah - let's finish it :P

I have a code generator that can convert GraphQL schemas into typed languages + boilerplate API endpoints using entirely grapqhql-extra which currently supports conversion to Typescript, JSDoc Comments, Python Dataclasses, Kotlin, and Go Structs (C# and Java are coming soon).

To create a new language converter is ~40 lines and very easy, if you'd like I could share with you the codebase =D

Maybe we could collaborate, since you know the codebase inside and out?

I have been thinking of publishing a blogpost on how to write a type generator using graphql-extra for typed-languages in less than 100 lines, it's truly incredibly impressive what you can do with this library. Everyone needs to know about this, it's so far and above the other GQL tooling available and the potential is massive.

Here's a preview of Typescript and Python types and boilerplate API endpoints being generated from GQL schemas, press RUN:
https://repl.it/@GavinRay97/AquamarineGoldFibonacci

Should see an output like this:
image

@GavinRay97 Yeah sure! It's already quite a bit of work with lib itself - I could definitely use some help with eg. some promo and examples like you suggested. It's also very helpful to get tips what is missing or weird feature-wise. Another perspective, you know...

I'm currently slashing codebase quite liberally and this endevour to add support for operations may end up in major rewrite :)

I need first to get on top of that (few days, I think) and then I will create some issues and tiny roadmap to collab on github and ping you for opinion and to ask with what you may want to help, ok?

I'm currently slashing codebase quite liberally and this endevour to add support for operations may end up in major rewrite :)

Sounds good, just let me know what things are breaking-changes so that I can refactor after the updated. Your library is being used as the backbone for the codegen/SDL manipulation tool for Hasura (I work there and implemented this feature) which has a very large userbase:

https://github.com/hasura/graphql-engine

https://github.com/hasura/codegen-assets/blob/refactor-api/builder-kit/my-new-codegen/src/schemaTools.ts

This way we don't break anything for our users =D

As far as feature-wise, it's so comprehensive, literally the only thing that would be nice is ability to parse and manipulate queries/mutations/subscriptions and the small change to EnumTypeApi so that you could call getValues() or similar.

This library definitely deserves more exposure, it's incredibly well-written and comprehensive, there is nothing else like it 😍

Just added it: dee23f6

I've refactored whole internals, but api is the one thing that did not change - your upgrade should be minimal :) I just wanted to make code more maintanable before I added new features.

Amazing, thank you so much!