smallrye-graphql icon indicating copy to clipboard operation
smallrye-graphql copied to clipboard

GraphQL Mutation exceptions when no setters are present.

Open zbhavyai opened this issue 2 years ago • 12 comments

Hello,

With Quarkus GraphQL, I am facing couple of exceptions with mutations when setters are not present in the model class. I made a small reproducer for this, which is available at https://github.com/zbhavyai/quarkus-graphql-nosetters-reproducer.

To explain, the Film class has no setters. The REST endpoints to add a Film works fine. The GraphQL mutations however throw exception - Cannot create instance of a class. No default constructor found. When I add a default constructor, InvalidSchemaException is thrown at build time.

The exceptions faced might be somewhat related to https://github.com/smallrye/smallrye-graphql/issues/1342, but I am not sure if the actual issue is related at all.

So, my question is - why exactly are setters required? If REST works fine without them, shouldn't GraphQL too? Is it bug or GraphQL limitation?

Thanks in advance.

zbhavyai avatar May 08 '22 02:05 zbhavyai

To be able to do a mutation you are going to need setters. Why do you not have setters ?

phillip-kruger avatar May 08 '22 02:05 phillip-kruger

Because, I am building a GraphQL wrapper on an existing service that doesn't have setters. The REST wrapper on that works without any issues, so I wondered why GraphQL doesn't work.

zbhavyai avatar May 08 '22 02:05 zbhavyai

Is there any particular reason why GraphQL mutation need setters? IMHO the parameterized constructors should be enough to create the object using the Jackson deserialization.

Thanks.

zbhavyai avatar May 08 '22 02:05 zbhavyai

If I remember correctly, the quarkus version still uses jsonb. If you don't have setters, you need to do something like this for the server input objects https://github.com/smallrye/smallrye-graphql/blob/3f7a7ab79ade1b049a73ff4a0e66128d171ee7d3/server/integration-tests-jdk16/src/test/java/io/smallrye/graphql/tests/records/RecordTest.java#L103

robp94 avatar May 08 '22 08:05 robp94

@robp94 Yeah this one works after I update my constructor with @JsonbCreator. Thanks for letting this know!

zbhavyai avatar May 08 '22 20:05 zbhavyai

Want to check, can we have Jackson support for Quarkus GraphQL?

zbhavyai avatar May 15 '22 04:05 zbhavyai

What do you mean by Jackson support?

phillip-kruger avatar May 15 '22 04:05 phillip-kruger

To put it simply, expecting GraphQL mutations to work with @JsonCreater annotated constructor rather than to rely on @JsonbCreator as robp94 suggested.

zbhavyai avatar May 15 '22 05:05 zbhavyai

Should be possible. Let's leave this open and we can look at it at some point. Or you can do a PR if you are keen.

phillip-kruger avatar May 15 '22 05:05 phillip-kruger

I think this should work with com.fasterxml.jackson.annotation.JsonCreator ( at least I see the code for it: https://github.com/smallrye/smallrye-graphql/blob/a7216b53ea69dda57fcc2d33766b6caf9b44b615/common/schema-builder/src/main/java/io/smallrye/graphql/schema/Annotations.java#L615)

phillip-kruger avatar Jul 20 '22 01:07 phillip-kruger

Not sure if you meant to do it differently (posted reproducer does uses @JsonCreator), but I see the exact same error. Tried with quarkus version 2.11.1.Final. Again, @JsonbCreator works but not @JsonCreator.

zbhavyai avatar Aug 02 '22 05:08 zbhavyai

Yes initially I thought we don't support it, but we do, as the annotations are there, so this is a bug

phillip-kruger avatar Aug 02 '22 05:08 phillip-kruger