Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
梦境迷离
Graphql Java Codegen
提交
768eaa24
G
Graphql Java Codegen
项目概览
梦境迷离
/
Graphql Java Codegen
9 个月 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Graphql Java Codegen
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
768eaa24
编写于
4月 26, 2021
作者:
B
Bogdan Kobylynskyi
提交者:
GitHub
4月 26, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
GraphQL serialization of java.util.Map #668 (#669)
上级
2cb4ae93
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
79 addition
and
18 deletion
+79
-18
src/main/java/com/kobylynskyi/graphql/codegen/model/graphql/GraphQLRequestSerializer.java
...aphql/codegen/model/graphql/GraphQLRequestSerializer.java
+39
-18
src/test/java/com/kobylynskyi/graphql/codegen/model/graphql/GraphQLRequestSerializerTest.java
...l/codegen/model/graphql/GraphQLRequestSerializerTest.java
+29
-0
src/test/java/com/kobylynskyi/graphql/codegen/model/graphql/data/EventsByIdsQueryRequest.java
...l/codegen/model/graphql/data/EventsByIdsQueryRequest.java
+11
-0
未找到文件。
src/main/java/com/kobylynskyi/graphql/codegen/model/graphql/GraphQLRequestSerializer.java
浏览文件 @
768eaa24
...
...
@@ -145,7 +145,7 @@ public class GraphQLRequestSerializer {
/**
* Serialize object to a string
*
* @param input can be any object or collection of objects.
* @param input can be any object or collection
/map
of objects.
* @param useObjectMapper if true, then use Jackson's ObjectMapper to convert from object to string,
* otherwise use toString
* @return serialized object
...
...
@@ -154,19 +154,16 @@ public class GraphQLRequestSerializer {
public
static
String
getEntry
(
Object
input
,
boolean
useObjectMapper
)
{
if
(
input
==
null
)
{
return
null
;
}
if
(
input
instanceof
Collection
<?>)
{
StringJoiner
joiner
=
new
StringJoiner
(
", "
,
"[ "
,
" ]"
);
for
(
Object
collectionEntry
:
(
Collection
<?>)
input
)
{
joiner
.
add
(
getEntry
(
collectionEntry
,
useObjectMapper
));
}
return
joiner
.
toString
();
}
if
(
useObjectMapper
)
{
}
else
if
(
useObjectMapper
)
{
return
objectMapperWriteValueAsString
(
input
);
}
if
(
input
instanceof
Enum
<?>)
{
return
input
.
toString
();
}
else
if
(
input
instanceof
Collection
<?>)
{
return
serializeCollection
((
Collection
<?>)
input
,
useObjectMapper
);
}
else
if
(
input
instanceof
Map
<?,
?>)
{
return
serializeMap
((
Map
<?,
?>)
input
,
useObjectMapper
);
}
else
if
(
input
instanceof
Map
.
Entry
<?,
?>)
{
return
serializeMapEntry
((
Map
.
Entry
<?,
?>)
input
,
useObjectMapper
);
}
else
if
(
input
instanceof
Enum
<?>)
{
return
serializeEnum
((
Enum
<?>)
input
);
}
else
if
(
input
instanceof
String
)
{
return
escapeJsonString
(
input
.
toString
());
}
else
if
(
input
.
getClass
().
getName
().
equals
(
"scala.Some"
))
{
// TODO: move to Scala Serializer
...
...
@@ -180,13 +177,29 @@ public class GraphQLRequestSerializer {
}
}
public
static
String
serializeCollection
(
Collection
<?>
input
,
boolean
useObjectMapper
)
{
StringJoiner
joiner
=
new
StringJoiner
(
", "
,
"[ "
,
" ]"
);
for
(
Object
entry
:
input
)
{
joiner
.
add
(
getEntry
(
entry
,
useObjectMapper
));
}
return
joiner
.
toString
();
}
public
static
String
objectMapperWriteValueAsString
(
Object
input
)
{
try
{
return
OBJECT_MAPPER
.
writeValueAsString
(
input
);
}
catch
(
JsonProcessingException
e
)
{
throw
new
UnableToBuildJsonQueryException
(
e
);
public
static
String
serializeMap
(
Map
<?,
?>
input
,
boolean
useObjectMapper
)
{
StringJoiner
joiner
=
new
StringJoiner
(
", "
,
"{ "
,
" }"
);
for
(
Map
.
Entry
<?,
?>
entry
:
input
.
entrySet
())
{
joiner
.
add
(
getEntry
(
entry
,
useObjectMapper
));
}
return
joiner
.
toString
();
}
public
static
String
serializeMapEntry
(
Map
.
Entry
<?,
?>
input
,
boolean
useObjectMapper
)
{
// no need to quote String key
return
input
.
getKey
()
+
": "
+
getEntry
(
input
.
getValue
(),
useObjectMapper
);
}
public
static
String
serializeEnum
(
Enum
<?>
input
)
{
return
input
.
toString
();
}
/**
...
...
@@ -231,4 +244,12 @@ public class GraphQLRequestSerializer {
return
sb
.
toString
();
}
public
static
String
objectMapperWriteValueAsString
(
Object
input
)
{
try
{
return
OBJECT_MAPPER
.
writeValueAsString
(
input
);
}
catch
(
JsonProcessingException
e
)
{
throw
new
UnableToBuildJsonQueryException
(
e
);
}
}
}
src/test/java/com/kobylynskyi/graphql/codegen/model/graphql/GraphQLRequestSerializerTest.java
浏览文件 @
768eaa24
...
...
@@ -26,6 +26,8 @@ import org.junit.jupiter.params.provider.MethodSource;
import
java.time.ZoneOffset
;
import
java.time.ZonedDateTime
;
import
java.util.Arrays
;
import
java.util.HashMap
;
import
java.util.Map
;
import
java.util.function.Function
;
import
java.util.stream.Stream
;
...
...
@@ -422,6 +424,7 @@ class GraphQLRequestSerializerTest {
.
setContextId
(
"something"
)
.
setIds
(
null
)
.
setTranslated
(
false
)
.
setEntries
(
null
)
.
build
();
GraphQLRequest
graphQLRequest
=
new
GraphQLRequest
(
request
,
new
EventResponseProjection
()
...
...
@@ -433,6 +436,32 @@ class GraphQLRequestSerializerTest {
assertEquals
(
expectedQueryDecorator
.
apply
(
expectedQueryStr
),
serializedQuery
);
}
@ParameterizedTest
(
name
=
"{0}"
)
@MethodSource
(
"provideAllSerializers"
)
void
serialize_requestWithMap
(
String
name
,
Function
<
GraphQLRequest
,
String
>
serializer
,
Function
<
String
,
String
>
expectedQueryDecorator
)
{
Map
<
Object
,
Object
>
entries
=
new
HashMap
<>();
entries
.
put
(
"entryInt"
,
1
);
entries
.
put
(
"entryString"
,
"2"
);
entries
.
put
(
"entryFloat"
,
3.3
);
entries
.
put
(
"entryNull"
,
null
);
EventsByIdsQueryRequest
request
=
new
EventsByIdsQueryRequest
.
Builder
()
.
setContextId
(
"something"
)
.
setIds
(
null
)
.
setTranslated
(
false
)
.
setEntries
(
entries
)
.
build
();
GraphQLRequest
graphQLRequest
=
new
GraphQLRequest
(
request
,
new
EventResponseProjection
()
.
id
()
);
String
serializedQuery
=
serializer
.
apply
(
graphQLRequest
).
replaceAll
(
" +"
,
" "
).
trim
();
String
expectedQueryStr
=
"query eventsByIds { "
+
"eventsByIds(contextId: \"something\", translated: false, entries: "
+
"{ entryNull: null, entryInt: 1, entryFloat: 3.3, entryString: \"2\" }){ id } }"
;
assertEquals
(
expectedQueryDecorator
.
apply
(
expectedQueryStr
),
serializedQuery
);
}
@ParameterizedTest
(
name
=
"{0}"
)
@MethodSource
(
"provideAllSerializers"
)
void
serialize_AllInputsNull
(
String
name
,
Function
<
GraphQLRequest
,
String
>
serializer
,
...
...
src/test/java/com/kobylynskyi/graphql/codegen/model/graphql/data/EventsByIdsQueryRequest.java
浏览文件 @
768eaa24
...
...
@@ -37,6 +37,10 @@ public class EventsByIdsQueryRequest implements GraphQLOperationRequest {
this
.
input
.
put
(
"translated"
,
translated
);
}
public
void
setEntries
(
Map
<
Object
,
Object
>
entries
)
{
this
.
input
.
put
(
"entries"
,
entries
);
}
@Override
public
GraphQLOperation
getOperationType
()
{
return
OPERATION_TYPE
;
...
...
@@ -72,6 +76,7 @@ public class EventsByIdsQueryRequest implements GraphQLOperationRequest {
private
String
contextId
;
private
Collection
<
String
>
ids
;
private
Boolean
translated
;
private
Map
<
Object
,
Object
>
entries
;
public
Builder
()
{
}
...
...
@@ -91,12 +96,18 @@ public class EventsByIdsQueryRequest implements GraphQLOperationRequest {
return
this
;
}
public
Builder
setEntries
(
Map
<
Object
,
Object
>
entries
)
{
this
.
entries
=
entries
;
return
this
;
}
public
EventsByIdsQueryRequest
build
()
{
EventsByIdsQueryRequest
obj
=
new
EventsByIdsQueryRequest
();
obj
.
setContextId
(
contextId
);
obj
.
setIds
(
ids
);
obj
.
setTranslated
(
translated
);
obj
.
setEntries
(
entries
);
return
obj
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录