grails / gorm-graphql

An automatic GraphQL schema generator for GORM
Apache License 2.0
80 stars 23 forks source link

Unable to register custom output object type #57

Closed jamesdh closed 3 years ago

jamesdh commented 3 years ago

Working through the docs on Type Conversion and Creation I thought it would be fairly straight forward to register a custom output object (non-scalar) type that isn't a persistent entity. However, I'm not sure it's possible given how the Typed trait resolves output types.

https://github.com/grails/gorm-graphql/blob/4143ecd83f6e13f50f81a0e0354ceacde0ea1df2/core/src/main/groovy/org/grails/gorm/graphql/entity/dsl/helpers/Typed.groovy#L55-L72

As you can see in line 55/56, it tries to retrieve it and typecast it as a GraphQLInputType, resulting in...

GroovyCastException: Cannot cast object 'GraphQLObjectType{...}' with class 'graphql.schema.GraphQLObjectType' to class 'graphql.schema.GraphQLInputType'

Mostly just creating this as a reminder to myself to submit a fix, but would appreciate any insights others might have in case I'm missing something obvious.

jameskleeh commented 3 years ago

You would have to show what the code that is causing this is in order for us to determine the issue

diffcunha commented 3 years ago

@jameskleeh I'm facing the same issue. The resolveType method in Typed trait is assuming all types held by typeManager to be of type GraphQLInputType

else if (typeManager.hasType(type)) { 
  graphQLType = (GraphQLInputType)typeManager.getType(type, nullable) 
}

I believe there's no apparent need to cast to GraphQLInputType. Because of that cast, if I do this:

  1. Register a MyType custom type:
    void doWith(GraphQLTypeManager typeManager) {
    typeManager.registerType(MyType, GraphQLObjectType
    .newObject()
    .name('MyType'))
    .fields([
      // ...
    ])
    .build()
    }
  2. Create a custom mutation
    mutation('someMutation', MyType) {
    argument('id', Long)
    // ...
    }
  3. Exception is thrown at load time
    Cannot cast object 'GraphQLObjectType{name='MyType', ... }' with class 'graphql.schema.GraphQLObjectType' to class 'graphql.schema.GraphQLInputType

I hope that's clear

jamesdh commented 3 years ago

@diffcunha this should be fixed via #62

diffcunha commented 3 years ago

@jamesdh That is awesome! Let me take a look a your PR

jamesdh commented 3 years ago

@diffcunha if you're in need of it now, I've got this fix (and a few other additions) in my own published package at https://github.com/jamesdh/gorm-graphql/packages/542850. Feel free to use that, just be aware that your gradle build file needs to be configured with a GitHub Personal Access Token in order to access public packages in the GitHub Package Repo.

repositories {
    mavenLocal()
    jcenter()
    maven { url "https://repo.grails.org/grails/core" }
    maven {
        name = "GitHubPackages"
        url = uri("https://maven.pkg.github.com/jamesdh/gorm-graphql")
        credentials {
            username = project.findProperty("gpr.user") ?: System.getenv("GPR_USER")
            password = project.findProperty("gpr.token") ?: System.getenv("GPR_TOKEN")
        }
    }
}