GithubHelp home page GithubHelp logo

Comments (5)

Balf avatar Balf commented on June 6, 2024

I think I can help you on your way here a bit, since your usecase somewhat resembles mine in Issue 475.

The GraphQLSchemaGenerator provides several methods to expand the schema with types that are not annotated. In your case you can use the withAdditionalTypes method to register the extra interfaces to the Schema.

Please note that this method takes a list of GraphQLTypes. For the interface of your external project you would have to create the GraphQLType definition yourself, using the methods provided by the GraphQL Java project on which SPQR depends.

Like so: Creating a schema programatically.

That way you can register custom types to the Schema. To automatically discover any implementations of that interface you would probably need to create your own ImplementationDiscoveryStrategy and add that to the SchemaGenerator. Here is a link to the default one provided by SPQR to discover implementations of an interface: DefaultImplementationStrategy.

However, if you're not able to annotate your data classes I'm not sure SPQR is the right solution for your project. SPQR's benefits are mostly in that area. Assuming that your data structure does not change (often) you might be better off by creating an SDL file for it and use that to define a schema.

Take a look at GraphQL Java and GraphQL Kickstart for more information on how to define a schema and attach the required DataFetchers and TypeResolvers to it.

from graphql-spqr.

niliha avatar niliha commented on June 6, 2024

Hi @Balf, I really appreciate you helping out.

The workaround you propose seems very promising, but if I understand it correctly, it would require me to create the schema manually for these interface types.
Since in the data structure we use, there are quite many and complex abstract classes, this would result in a lot of work.

I ended up with the very same approach you mentioned in the end, i.e. I hacked the annotations into the third-party dependency and imported the schema generated by SPQR into another application using standard graphql-java.

The only thing I still use from SPQR are the scalar definitions since graphql-java-extended-scalars does not cover all scalar types for me.

Thank you again :)

from graphql-spqr.

kaqqao avatar kaqqao commented on June 6, 2024

Hey @niliha, sorry for the slow response.
Are these interfaces reachable from your root types (the one you registered with GraphQLSchemaGenerator)? If so, you can easily control what gets mapped using various InterfaceMappingStrategy implementations. E.g. you can map interfaces from specific packages or something custom. Everything in SPQR is configurable without annotations, that was one of the design goals.

from graphql-spqr.

kaqqao avatar kaqqao commented on June 6, 2024

@Balf Your situation looks a bit nore complicated, but I'll give it a look. I'm pretty sure you can also get away without manually mapping things...

from graphql-spqr.

niliha avatar niliha commented on June 6, 2024

Hi @kaqqao, thank you for guiding me in the right direction!

I had a look at InterfaceMappingStrategy before, but was confused by the use of AnnotatedType, thinking it would require annotations.
However, having a look at the documentation of AnnotatedType, it's clear that the type must not be annotated.

For ayone else who might have a similar issue in the future, i.e. mapping java classes to GraphQL interfaces without using annotations, here is a more detailed description of what you need to do:

  1. Create a custom InterfaceMappingStrategy implementation, i.e. getInterfaces() must return all class types you want to register as GraphQL interface and supports() must return true for these class types. One way to implement this is store the affected class types statically in the implementation.
  2. Register this implementation using GraphQLSchemaGenerator#withInterfaceMappingStrategy()

from graphql-spqr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.