userongithub0
userongithub0

Reputation: 11

Is it possible to change a GraphQL schema at runtime and write resolvers that handle this dynamic schema? (in Java)

I am working with GraphQL (in Java) and I would like to find a way to do the following: I need the possibility to constantly adapt the GraphQL schema at runtime without restart. In particular I need to be able to add new fields to GraphQL types. Moreover I need the possibility to be able to write resolvers which can handle this dynamic schema.

I do not have example code yet, so just think of the simplest example (one GraphQL type with several fields that can all be of different type).

My problem is that I am quite new in GraphQL and I do not have a lot of experience with it. Of course I looked for a solution on the internet, but I did not find one yet (or just did not notice that I found it due to my lacking experience with GraphQL). The only interesting discovery I made is this: exposing dynamic schemas with graphql . But I do not understand how this solution works because 1) I do not know how to reload the schema at runtime and 2) I do not know how to write the resolvers so that they can handle that dynamic schema.

So can anybody help me with my problem and/or can answer my questions regarding the link I found?

I am very thankful for every help, no matter how extensive it is. Like I told before, I am quite new in GraphQL. Therefore I would be also very thankful for links to examples (if possible), so that I can understand better.

Thank you very much in advance.

Upvotes: 0

Views: 2962

Answers (3)

bmadigan
bmadigan

Reputation: 99

Spring devtools maven plugin allows you to include resource files when changes are detected on a running spring-boot server. This is mainly a use case for development; I don't want to rebuild the server every time I modify the schema. https://docs.spring.io/spring-boot/maven-plugin/run.html

make sure you're running devtools:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

And in the plugins section of the pom.xml file:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <addResources>true</addResources>
    </configuration>
</plugin>

Upvotes: 0

innovarerz
innovarerz

Reputation: 146

@userongithub0 you may take a look at GraphQL Schema Directives And specifically on the rest directive

Upvotes: 0

Yogesh Aggarwal
Yogesh Aggarwal

Reputation: 1127

First of all, don't ever try to do that or use only if there're some very strict situations. Here's why:

  • A schema is like a contract between the front-end & backend & on change, it can lead to instability between both of them very quickly.
  • If you try to change the schema of GraphQL, it might fail to connect properly with your resolvers & consecutively with your database as well.
  • Whenever there's is a change in the schema the GraphQL server (the server handler, in general) needs to be restarted (recompile) & it will take time, hence results in high response time.

No matter what language you are using, you should always see it as a red flag. In my opinion, it will be a really bad practice.

Upvotes: -3

Related Questions