带有 JSON 补丁的 GraphQL 突变
GraphQL Mutation with JSON Patch
GraphQL 中是否有任何数据类型可用于描述 JSON 补丁操作?
一个JSON补丁操作的结构如下
{ "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
可以是任何东西。
如果你需要 return JSON 类型,那么 graphql 有 scalar JSON
return 任意 JSON 键入你想要 return 的地方。
这是架构
`
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
}
`
在解析器中,您可以 return 任何 json 格式的密钥名称 "value"
//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)
}
},
你唯一需要专门 return "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"
}
}
}
GraphQL 中是否有任何数据类型可用于描述 JSON 补丁操作?
一个JSON补丁操作的结构如下
{ "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
可以是任何东西。
如果你需要 return JSON 类型,那么 graphql 有 scalar JSON
return 任意 JSON 键入你想要 return 的地方。
这是架构
`
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
}
`
在解析器中,您可以 return 任何 json 格式的密钥名称 "value"
//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)
}
},
你唯一需要专门 return "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"
}
}
}