Graphql Java Hello World: failed to add child schema to parent schema
I'm trying to prototype graphql Java( https://github.com/andimarek/graphql-java )And start building it from the Hello world example I'm using graphiql to call the graphiql service with a pattern, which applies to pattern {hello1}, but not to pattern {testpojo} Please find the code I run below Someone can let me know what's wrong with the following code
static GraphQLSchema schema = null; /** * POJO to be returned for service method 2 * @author * */ private class TestPojo { String id; String name; TestPojo(String id,String name) { this.id = id; this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } /** * service method 1 * @return */ public String greeting1() { return "Hello123"; } /** * service method 2 * @return */ public TestPojo greeting2() { return new TestPojo("1","Jack"); } /** * GraphQl endpoint invoked using GraphiQl * @param query * @return */ @RequestMapping("/query") public Object testGraphQLWithQuery(@RequestParam("query") String query) { return new GraphQL(schema).execute(query).getData(); } // Schema deFinition for graphQL static { // sub schema to be added to parent schema GraphQLObjectType testPojo = newObject().name("TestPojo").description("This is a test POJO") .field(newFieldDeFinition().name("id").type(GraphQLString).build()) .field(newFieldDeFinition().name("name").type(GraphQLString).build()) .build(); // parent schema GraphQLObjectType queryType = newObject().name("helloWorldQuery") .field(newFieldDeFinition().type(GraphQLString).name("hello1").dataFetcher(new DataFetcher() { @Override public Object get(DataFetchingEnvironment arg0) { Object a = new GrapgQLSampleController().greeting1(); return a; } }).build()) .field(newFieldDeFinition().type(testPojo).name("testPojo").dataFetcher(new DataFetcher() { @Override public Object get(DataFetchingEnvironment arg0) { Object a = new GrapgQLSampleController().greeting2(); return a; } }).build()) .build(); schema = GraphQLSchema.newSchema().query(queryType).build(); }
Solution
I just tested your code and it seems to work properly You must be more specific about the mistakes you see
If your problem is the reason why the query {testpojo} is invalid, you should have read the error obtained: sub selection is required You cannot select the entire complex object in graphql. You must specify the required subfield. For example, {testpojo {ID, name}} is a valid query that can be used with your schema