ValidationError FieldUndefined SPQR GraphQL
Asked Answered
O

3

7

I am getting the following and cant seem to find an answer.

Error [ValidationError{validationErrorType=FieldUndefined, queryPath=[find_by_id], message=Validation error of type FieldUndefined: Field 'find_by_id' in type 'Query' is undefined @ 'find_by_id', locations=[SourceLocation{line=1, column=2}], description='Field 'find_by_id' in type 'Query' is undefined'}]

My Code.

Query

@GraphQLQuery(name = "find_by_id")
public Event findById(@GraphQLArgument(name = "id") Long id) {

Schema Gen

@EJB
private EventFacade eventFacade; // Normal stateless bean 

GraphQLSchema guestSchema = new GraphQLSchemaGenerator()
            .withOperationsFromSingleton(eventFacade)
            .withValueMapperFactory(new JacksonValueMapperFactory())
            .withDefaults()
            .generate();

GraphQL graphQL = GraphQL.newGraphQL(guestSchema).build();

Code to Execute

String query = "{find_by_id (id: 1){eventName}}";
ExecutionResult result = graphQL.execute(query);

Using the SPQR lib

Event POJO is basic with eventName as a String and an id from the abstract (Parent) class. Entity class is in a different jar (Entity Jar). Code to execute Query and build schema are in the EJB Jar.

Any help / indication where i went wrong will be appreciated.

UPDATE Created a git issue to help solve Git Issue

Oliviaolivie answered 26/4, 2018 at 12:21 Comment(0)
P
5

I found this solution here: https://github.com/leangen/graphql-spqr/wiki/Errors#ambiguous-member-type

Reference: To treat all missing type arguments as Object or apply custom type transformation logic, supply a TypeTransformer:

This should work for you.

    GraphQLSchema schema = new GraphQLSchemaGenerator()
           .withBasePackages(basePackages)
           .withTypeTransformer(new DefaultTypeTransformer(true, true))
           .withOperationsFromSingleton(eventFacade).generate();
Passionless answered 10/5, 2018 at 8:55 Comment(2)
Is this required .withBasePackages(basePackages)?Oliviaolivie
No it is not required, also something I should have mentioned in my answer. You need to make sure .withTypeTransformer is before .withOperationsFromSingleton.Passionless
F
1

I believe you must change this String query = "{find_by_id (id: 1){eventName}}"; to String query = "\"query\": {find_by_id (id: 1){eventName}}";

Feltner answered 26/4, 2018 at 23:9 Comment(2)
The lib i am using does not require it. See example from the libOliviaolivie
Getting the following if i add it graphql.GraphQL - Query failed to parse : '"query": {find_by_id (id: 1){eventName}}'Oliviaolivie
S
1

Can you add the Even Class details, I'm guessing that's where your issue lies as the author in your URL pass a class to the query, but you are passing only ID(find_by_id) followed by {eventName} i think it should be changed to

"{Event(id: 1){eventName}}"

The Author has given an example about how to use findByID in the below examples for the Vendor Class and he calls it in a different format from yours, maybe give that a shot :) https://github.com/leangen/graphql-spqr-samples

Superphosphate answered 3/5, 2018 at 3:6 Comment(3)
Tried it and it does not work. I believe the first part is only the name of the query and not the class / typeOliviaolivie
#48969396 did you check this answer gives a better understanding of how the query should be formed??Superphosphate
im not sure that you read through the lib. It will explain most of what im trying to do. Also not building my own schema as the lib are doing it for meOliviaolivie

© 2022 - 2024 — McMap. All rights reserved.