Skip to content

Instantly share code, notes, and snippets.

@leapar
Last active April 1, 2016 07:52
Show Gist options
  • Save leapar/5d732f9069d12450d20ec31ea2c6f578 to your computer and use it in GitHub Desktop.
Save leapar/5d732f9069d12450d20ec31ea2c6f578 to your computer and use it in GitHub Desktop.
private Map<String, Object> testMutation() {
String mutation_query = "mutation M {\r\n"
+ "first: changeGoodsName(id:\"K201511030000008\",name: \"name1\") {\r\n"
+ "name\r\n"
+ "updateTime\r\n"
+ "}\r\n"
+ "}";
GraphQLObjectType goodsType = GraphQLObjectType.newObject()
.name("NumberHolder")
.field(newFieldDefinition()
.name("name")
.type(GraphQLString)
.build())
.field(newFieldDefinition()
.name("updateTime")
.description("The updateTime of the goods.")
.type(GraphQLString)
.build())
.build();
GraphQLObjectType queryType = GraphQLObjectType.newObject()
.name("queryType")
.field(newFieldDefinition()
.name("numberHolder")
.type(goodsType)
.build())
.build();
GraphQLObjectType mutationType = GraphQLObjectType.newObject()
.name("updateGoodsName")
.field(newFieldDefinition()
.name("changeGoodsName")
.type(goodsType)
.argument(newArgument()
.name("id")
.type(GraphQLString)
.build())
.argument(newArgument()
.name("name")
.type(GraphQLString)
.build())
.dataFetcher(new DataFetcher() {
@Override
public Object get(DataFetchingEnvironment environment) {
String id = environment.getArgument("id");
String name = environment.getArgument("name");
System.out.println(name);
System.out.println(id);
GoodsInfoDto info = new GoodsInfoDto();
info.setGoodsId(id);
info.setName(name);
goodsInfoService.modifyGoodsInfo(info);
return info;
}
})
.build())
.build();
GraphQLSchema schema = GraphQLSchema.newSchema()
.query(queryType)
.mutation(mutationType).build();
Map<String, Object> result = (Map<String, Object>) new GraphQL(schema).execute(mutation_query).getData();
return result;
}
private Map<String, Object> testQuery() {
GraphQLObjectType goodsInfoType = newObject()
.name("GoodsInfoType")
.description("type of goods info detail")
.field(newFieldDefinition()
.name("name")
.description("The name of the goods.")
.type(GraphQLString)
.build())
.field(newFieldDefinition()
.name("goodsId")
.description("The Id of the goods.")
.type(GraphQLString)
.build())
.field(newFieldDefinition()
.name("updateTime")
.description("The updateTime of the goods.")
.type(GraphQLString)
.build())
.build();
DataFetcher goodsInfoDataFetcher = new DataFetcher() {
@Override
public
Object get(DataFetchingEnvironment environment) {
String id = (String) environment.getArguments().get("id");
Map<String,Object> map = new HashMap<String,Object>();
map.put("goodsId", id);
map.put("languageId", languageId);
map.put("baseUrl", SystemConf.getServerRootDirUrl());
map.put("checkPhoneUserId", null );
GoodsInfoDto info = goodsInfoService.queryGoodsInfoDetail(map);
return info;
}
};
GraphQLObjectType queryType = newObject()
.name("GoodsInfoQuery")
.field(newFieldDefinition()
.name("goodsInfo")
.type(goodsInfoType)
//.staticValue(info)
.argument(newArgument()
.name("id")
.description("id of the goods")
.type(new GraphQLNonNull(GraphQLString))
.build())
.dataFetcher(goodsInfoDataFetcher)
.build()
)
.build();
GraphQLSchema schema = GraphQLSchema.newSchema().query(queryType).build();
String query = "{goodsInfo(id:\"K201511030000008\"){"
+ "name\r\n"
+ "updateTime\r\n"
+ "goodsId}}";
Map<String, Object> result = (Map<String, Object>) new GraphQL(schema).execute(query).getData();
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment