GraphQL中是否有任何可用于描述JSON Patch操作的数据类型?
JSON Patch操作的结构如下。
{ "op": "add|replace|remove", "path": "/hello", "value": ["world"] }
value
可以是任何有效的JSON文字或对象,例如。
"value": { "name": "michael" }
"value": "hello, world"
"value": 42
"value": ["a", "b", "c"]
op
和path
总是简单的字符串,value
可以是任何东西。
答案 0 :(得分:1)
如果您需要返回JSON类型,则graphql具有scalar JSON
它将返回您要返回的任何JSON类型。
这是架构
`
scalar JSON
type Response {
status: Boolean
message: String
data: JSON
}
type Test {
value: JSON
}
type Query {
getTest: Test
}
type Mutation {
//If you want to mutation then pass data as `JSON.stringify` or json formate
updateTest(value: JSON): Response
}
`
在解析器中,您可以返回键名称为"value"
的json格式的任何内容
//Query resolver
getTest: async (_, {}, { context }) => {
// return { "value": "hello, world" }
// return { "value": 42 }
// return { "value": ["a", "b", "c"] }
// return anything in json or string
return { "value": { "name": "michael" } }
},
// Mutation resolver
async updateTest(_, { value }, { }) {
// Pass data in JSON.stringify
// value : "\"hello, world\""
// value : "132456"
// value : "[\"a\", \"b\", \"c\"]"
// value : "{ \"name\": \"michael\" }"
console.log( JSON.parse(value) )
//JSON.parse return formated required data
return { status: true,
message: 'Test updated successfully!',
data: JSON.parse(value)
}
},
您唯一需要专门返回“值”键来识别以获取查询和变异的东西 查询
{
getTest {
value
}
}
// Which return
{
"data": {
"getTest": {
"value": {
"name": "michael"
}
}
}
}
突变
mutation {
updateTest(value: "{ \"name\": \"michael\" }") {
data
status
message
}
}
// Which return
{
"data": {
"updateTest": {
"data": null,
"status": true,
"message": "success"
}
}
}