Skip to content

switch state from internal to public #415

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

switch state from internal to public #415

wants to merge 1 commit into from

Conversation

de55
Copy link
Contributor

@de55 de55 commented Oct 2, 2019

📝 Description

Switch state in SchemaGenerator (along with corresponding classes) from internal to public. I was running into the following error while trying to override the objectType function because an object was being used while being built:

Caused by: java.lang.ClassCastException: class graphql.schema.GraphQLTypeReference cannot be cast to class graphql.schema.GraphQLUnmodifiedType (graphql.schema.GraphQLTypeReference and graphql.schema.GraphQLUnmodifiedType are in unnamed module of loader 'app')

@smyrick
Copy link
Contributor

smyrick commented Oct 2, 2019

@de55 Can you provide the branch of the code that is throwing this error? This looks like a schema generation issue, that we can fix without exposing internal implementations.

If there is also something you need to override we should do that with hooks instead of the extending the class

@smyrick smyrick added the status: do not merge Do not merge until this is removed label Oct 2, 2019
@smyrick
Copy link
Contributor

smyrick commented Oct 2, 2019

Marking DNM until we can confirm this is needed

@de55
Copy link
Contributor Author

de55 commented Oct 2, 2019

This is related to #384 and #385.

Specifically this comment:

It turns out I was not able to implement it through the hooks because I needed access to the generator, which creates a circular reference. I instead implemented it by inheriting from SchemaGenerator and overriding the objectType and interfaceType functions.

The only issue with this approach is that FederatedSchemaGenerator is final, so I had to copy/paste the code from that to my custom generator. Would you mind if that is made an open class?

After using the extension function in a different package, I ran into the issue. However, I believe anyone that extends the schema generator (which has already been made open) would run into similar issues. Here is the override function (note I am currently using my fork which is why I can reference state.cache):

    override fun objectType(kClass: KClass<*>): GraphQLType {
        val type = super.objectType(kClass)
        return state.cache.buildIfNotUnderConstruction(kClass) {
            val unwrappedType = GraphQLTypeUtil.unwrapNonNull(type)
            if (unwrappedType is GraphQLObjectType) addExtensionsToObject(unwrappedType, kClass, type) else type
        }
    }

    private fun addExtensionsToObject(unwrappedType: GraphQLObjectType, kClass: KClass<*>, origType: GraphQLType): GraphQLType {
        val builder = GraphQLObjectType.newObject(unwrappedType)
        val name = kClass.getSimpleName()
       //this method just gets the extension function for this kClass
        extensionFunctionMapper.getValidExtensionFunctions(kClass)
            .forEach { builder.field(super.function(it, name, null, false)) }
        val type = builder.build()
        return if(origType is GraphQLNonNull) GraphQLNonNull.nonNull(type) else type
    }

@smyrick smyrick added status: do not merge Do not merge until this is removed and removed status: do not merge Do not merge until this is removed labels Oct 3, 2019
@de55
Copy link
Contributor Author

de55 commented Oct 4, 2019

@smyrick any update on this? Do you think there’s a different way I should implement?

@de55
Copy link
Contributor Author

de55 commented Oct 4, 2019

I was able to figure it out without exposing internals.

@de55 de55 closed this Oct 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: do not merge Do not merge until this is removed
Development

Successfully merging this pull request may close these issues.

2 participants