【问题标题】:Hello world with GraphQLObjectType使用 GraphQLObjectType 的世界你好
【发布时间】:2016-08-14 01:24:33
【问题描述】:

我正在执行下面的代码,结果是:

{TestPojo={id=null, name=null}}

我期待结果是{TestPojo={id="1", name="Jack"}}。我错过了什么?

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;
        }
    }
}

【问题讨论】:

    标签: java graphql graphql-java


    【解决方案1】:

    尝试通过修复代码来进行教学。请注意,静态内容来自 .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);
    

    将打印 {id=Test1, name=pojo1}

    我有时间可以使用 DataFetcher 更新示例

    【讨论】:

    • 这个答案没有回答问题
    【解决方案2】:

    尝试在 TestPojo 类上添加 getter 方法,因为在类上声明时会调用这些方法。否则请在字段中定义您自己的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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-11-19
      • 2015-01-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多