smallrye / smallrye-graphql

Implementation for MicroProfile GraphQL
Apache License 2.0
160 stars 91 forks source link

Fire CDI event for graphql.schema.idl.TypeDefinitionRegistry for extensions #312

Open beikov opened 4 years ago

beikov commented 4 years ago

The initial discussion happened here: https://github.com/eclipse/microprofile-graphql/issues/127

I would like to be able to contribute GraphQL types, based on existing discovered types via graphql.schema.idl.TypeDefinitionRegistry. This is the way I integrate with graphql-java already: https://github.com/Blazebit/blaze-persistence/blob/master/integration/graphql/src/main/java/com/blazebit/persistence/integration/graphql/GraphQLEntityViewSupportFactory.java#L180

This allows me to add support for the Relay specification which can be used to implement a very efficient keyset based pagination approach.

phillip-kruger commented 4 years ago

Thanks @beikov :)

Let me try something and let you know once I have something. Doing the same as what I have done for the schemaBuilder, but for TypeDefinitionRegistry. Right ?

beikov commented 4 years ago

Correct

phillip-kruger commented 4 years ago

Hi @beikov . I am not sure how to do this. Maybe you can look at https://github.com/smallrye/smallrye-graphql/blob/master/server/implementation/src/main/java/io/smallrye/graphql/bootstrap/Bootstrap.java, this is where we build the schema. If you can suggest how we should get to TypeDefinitionRegistry that can help. I am not sure how...

beikov commented 4 years ago

Sure. Usually you just instantiate the TypeDefinitionRegistry or start from a schema SDL file. The support for a SDL file could be a good nice extension.

TypeDefinitionRegistry typeRegistry;
if (sdl == null) {
    typeRegistry = new TypeDefinitionRegistry();
} else {
    typeRegistry = new SchemaParser().parse(sdl);
}
// Register types as discovered by SmallRye to typeRegistry
// publish CDI event for typeRegistry

TypeRuntimeWiring.Builder queryWiringBuilder = TypeRuntimeWiring.newTypeWiring("Query");
// Wire query methods as discovered to queryWiringBuilder
// Maybe also publish a CDI event for queryWiringBuilder?

RuntimeWiring runtimeWiring = RuntimeWiring.newRuntimeWiring()
                .type(queryWiringBuilder.build());
SchemaGenerator schemaGenerator = new SchemaGenerator();
GraphQLSchema schema = schemaGenerator.makeExecutableSchema(typeRegistry, runtimeWiring);
phillip-kruger commented 4 years ago

Hi @beikov - thanks, this will be a different way than how we build the schema at the moment... I'll look at this a.s.a.p, if you need it soon, you are welcome to do a PR with your proposed solution ? That would be very cool :)

beikov commented 4 years ago

Thanks for looking into this, I don't have a rush. Sorry that I can't contribute that right now, but I'm about to finish a project and already quite busy with that. Looking forward to testing this though when you manage to try this!

beikov commented 3 years ago

I would like to point out that in the Spring world, the DGS framework is currently the leading declarative solution for GraphQL which is very advanced. DGS offers support for federation and dynamic types by allowing to provide custom TypeDefinitionRegistry: https://netflix.github.io/dgs/advanced/dynamic-schemas/