[英]Hello world with GraphQLObjectType
I am executing the following code and the result is: 我正在执行以下代码,结果是:
{TestPojo={id=null, name=null}}
I was expecting the result to be {TestPojo={id="1", name="Jack"}}
. 我期望的结果是
{TestPojo={id="1", name="Jack"}}
。 What am I missing? 我想念什么?
import static graphql.Scalars.GraphQLString;
import static graphql.schema.GraphQLFieldDefinition.newFieldDefinition;
import static graphql.schema.GraphQLObjectType.newObject;
import java.util.Map;
import graphql.GraphQL;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
public class HelloWorld {
public static void main(String[] args) {
// 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().name(testPojo.getName())
.type(testPojo)
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment arg0) {
Object a = new GrapgQLSampleController()
.greeting2();
return a;
}
})
.build())
.build();
GraphQLSchema schema = GraphQLSchema.newSchema().query(queryType).build();
Map<String, Object> result = (Map<String, Object>) new GraphQL(schema).execute("{TestPojo {id,name}}")
.getData();
System.out.println(result);
// Prints: {TestPojo={id=null, name=null}}
}
/**
* service method 2
*
* @return
*/
public TestPojo greeting2() {
return new TestPojo("1", "Jack");
}
// inner pojo
class TestPojo {
public String id;
public String name;
TestPojo(String id, String name) {
this.id = id;
this.name = name;
}
}
}
Try adding getter methods on your TestPojo
class as these will be called when declared on the class. 尝试在
TestPojo
类上添加getter方法,因为这些方法在类上声明时将被调用。 Otherwise define your own DataFetcher
on the field. 否则,请在字段上定义自己的
DataFetcher
。
class TestPojo {
public final String id;
public final String name;
TestPojo(String id, String name) {
this.id = id;
this.name = name;
}
String getId() {
return id;
}
String getName() {
return name;
}
}
Trying to be pedagogical by fixing the code. 通过修复代码尝试进行教学。 Note that static content comes from whatever is in .staticValue("X")
请注意,静态内容来自.staticValue(“ X”)中的任何内容
GraphQLObjectType testPojo = newObject().name("TestPojo")
.description("This is a test POJO")
.field(newFieldDefinition().type(GraphQLString).name("id").staticValue("Test1"))
.field(newFieldDefinition().type(GraphQLString).name("name").staticValue("pojo1"))
.build();
GraphQLSchema schema = GraphQLSchema.newSchema().query(testPojo).build();
Map<String, Object> result = (Map<String, Object>) new GraphQL(schema).execute("{id,name}")
.getData();
System.out.println(result);
Will print {id=Test1, name=pojo1} 将显示{id = Test1,name = pojo1}
Can update the example using DataFetcher when I have time 有空时可以使用DataFetcher更新示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.