我在项目中使用GraphQL Java库,我试图在自定义DataFetcher中返回所需的结果。我究竟如何构建响应对象?据我所知,我不希望只是以格式编写响应字符串。应该有更好的方法将值分配给请求的字段。
Array
(
[0] => Array
(
[2016-10-07 14:10:00] => Array
(
[0] => stdClass Object
(
[pupil_id] => 48307
[fullname] => Callum Lacey
[grade_id] => 87
[meeting_id] => 1812
[slot_id] => 31306
[title] => Parent Evening 1 - 2016-10-07 14:00
[startTime] => 2016-10-07 14:10:00
[endTime] => 2016-10-07 14:20:00
)
[1] => stdClass Object
(
[pupil_id] => 79571
[fullname] => Daniel Lacey
[grade_id] => 87
[meeting_id] => 1812
[slot_id] => 31305
[title] => Parent Evening 1 - 2016-10-07 14:00
[startTime] => 2016-10-07 14:00:00
[endTime] => 2016-10-07 14:10:00
)
[2] => stdClass Object
(
[pupil_id] => 48307
[fullname] => Callum Lacey
[grade_id] => 87
[meeting_id] => 1816
[slot_id] => 31322
[title] => 2016-10-07 14:15
[startTime] => 2016-10-07 14:20:00
[endTime] => 2016-10-07 14:25:00
)
[3] => stdClass Object
(
[pupil_id] => 79571
[fullname] => Daniel Lacey
[grade_id] => 87
[meeting_id] => 1816
[slot_id] => 31323
[title] => 2016-10-07 14:15
[startTime] => 2016-10-07 14:25:00
[endTime] => 2016-10-07 14:30:00
)
[4] => stdClass Object
(
[pupil_id] => 48307
[fullname] => Callum Lacey
[grade_id] => 87
[meeting_id] => 1813
[slot_id] => 31311
[title] => Parent Evening 2 - 2016-10-07 14:00
[startTime] => 2016-10-07 14:30:00
[endTime] => 2016-10-07 14:40:00
)
[5] => stdClass Object
(
[pupil_id] => 48307
[fullname] => Callum Lacey
[grade_id] => 87
[meeting_id] => 1815
[slot_id] => 31318
[title] => Parent Evening 3 - 2016-10-07 14:00
[startTime] => 2016-10-07 14:40:00
[endTime] => 2016-10-07 14:50:00
)
[6] => stdClass Object
(
[pupil_id] => 79571
[fullname] => Daniel Lacey
[grade_id] => 87
[meeting_id] => 1815
[slot_id] => 31319
[title] => Parent Evening 3 - 2016-10-07 14:00
[startTime] => 2016-10-07 14:50:00
[endTime] => 2016-10-07 15:00:00
)
)
)
)
答案 0 :(得分:2)
get()
的返回值应该是评估该特定操作的结果的对象。
例如,给定:
private static GraphQLFieldDefinition allTodoes=
GraphQLFieldDefinition
.newFieldDefinition()
.name("allTodoes")
.type(new GraphQLList(todo))
.argument(skip)
.argument(take)
.dataFetcher(fetcher)
.build();
然后fetcher
需要返回一个List
个对象,这些对象满足GraphQLObjectType
todo
的任何合同。
就我而言,我正在复制this schema,因此我可以返回ArrayList
个ToDo
个对象:
private static class ToDo {
public int id;
public String text;
public boolean complete;
ToDo(int id, String text, boolean complete) {
this.id=id;
this.text=text;
this.complete=complete;
}
}
这是整个架构,Java风格:
/***
Copyright (c) 2016 CommonsWare, LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may
not use this file except in compliance with the License. You may obtain
a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.commonsware.agql.client.local;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
import graphql.Scalars;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLSchema;
// schema inspired by https://gist.github.com/gsans/d857b0951077bdbbabd968e0431d97fe
public class TestSchema {
private static class ToDo {
public int id;
public String text;
public boolean complete;
ToDo(int id, String text, boolean complete) {
this.id=id;
this.text=text;
this.complete=complete;
}
}
private static ArrayList<ToDo> TODOES=new ArrayList<>();
static {
TODOES.add(new ToDo(123, "get this sample working", false));
TODOES.add(new ToDo(4567, "add more test cases", false));
TODOES.add(new ToDo(89012, "add more documentation", false));
TODOES.add(new ToDo(345678, "add more todoes", false));
}
private static GraphQLFieldDefinition id=
GraphQLFieldDefinition
.newFieldDefinition()
.name("id")
.description("unique identifier")
.type(Scalars.GraphQLInt)
.build();
private static GraphQLFieldDefinition text=
GraphQLFieldDefinition
.newFieldDefinition()
.name("text")
.description("what is to be done")
.type(Scalars.GraphQLString)
.build();
private static GraphQLFieldDefinition complete=
GraphQLFieldDefinition
.newFieldDefinition()
.name("complete")
.description("are we done yet?")
.type(Scalars.GraphQLBoolean)
.build();
private static GraphQLObjectType todo=
GraphQLObjectType.newObject()
.name("Todo")
.description("Something to be done")
.field(id)
.field(text)
.field(complete)
.build();
private static GraphQLArgument skip=
GraphQLArgument
.newArgument()
.name("skip")
.description("return starting with this index")
.type(Scalars.GraphQLInt)
.build();
private static GraphQLArgument take=
GraphQLArgument
.newArgument()
.name("take")
.description("return this number of todoes")
.type(Scalars.GraphQLInt)
.build();
private static DataFetcher fetcher=new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment env) {
int startIndex=0;
int endIndex=TODOES.size()-1;
List<ToDo> result=null;
try {
if (env.containsArgument("skip")) {
Integer skip=env.getArgument("skip");
if (skip!=null) {
startIndex=skip.intValue();
}
}
if (env.containsArgument("take")) {
Integer take=env.getArgument("take");
if (take!=null) {
endIndex=startIndex+take;
}
}
result=TODOES.subList(startIndex, endIndex);
}
catch (Exception e) {
Log.e(getClass().getSimpleName(), "Exception processing request", e);
// um, how do I get this error into result?
}
return(result);
}
};
private static GraphQLFieldDefinition allTodoes=
GraphQLFieldDefinition
.newFieldDefinition()
.name("allTodoes")
.type(new GraphQLList(todo))
.argument(skip)
.argument(take)
.dataFetcher(fetcher)
.build();
private static GraphQLObjectType rootQuery=
GraphQLObjectType
.newObject()
.name("rootQuery")
.field(allTodoes)
.build();
public static GraphQLSchema schema=
GraphQLSchema.newSchema().query(rootQuery).build();
}
DataFetcher
的默认设置是使用getter或public
字段来解析值,尽管他们的库中有选项可以执行其他操作IIRC。
正如您将从一条评论中看到的那样,我还没有弄清楚如何处理错误,但我确信有一个选项。