gpt4 book ai didi

json - 突变中的单个 JSON 参数

转载 作者:行者123 更新时间:2023-12-02 00:52:10 25 4
gpt4 key购买 nike

下面我尝试使用一个对象参数 credentials 设置一个突变 example。我以前有过这个工作然后突然间它在 JSON 部分停止工作失败。为什么我不能通过 credentials 发送 json?

import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLInt,
GraphQLInputObjectType,
GraphQLNonNull,
graphql
} from 'graphql'


let requestType = new GraphQLInputObjectType({
name: 'Request',
fields: {
name: {type: GraphQLString},
}
})

let responseType = new GraphQLObjectType({
name: 'Response',
fields: {
name: {type: GraphQLString},
age: {type: GraphQLInt}
}
})

let schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
author: {
type: GraphQLString,
resolve: (source, args, context, info) => {
return 'Thomas Reggi'
}
}
}
}),
mutation: new GraphQLObjectType({
name: 'Mutation',
fields: {
example: {
type: responseType,
args: {
credentials: {
name: 'credentials',
type: requestType
}
},
resolve: (source, args, context, info) => {
return {
'name': 'Thomas Reggi',
'age': 26
}
}
}
}
})
})

let credentials = { name: "Thomas Reggi" }

let requestString = `
mutation {
example(credentials: ${JSON.stringify(credentials)}) {
name,
age
}
}`

graphql(schema, requestString).then(result => {
console.log(result)
})

这是错误:

{ errors: 
[ Syntax Error GraphQL request (3:25) Expected Name, found String "name: "

2: mutation {
3: example(credentials: {"name":"Thomas Reggi"}) {
^
4: name,
] }

Name 的引用从何而来?为什么会抛出错误?

最佳答案

只是通过艰难的方式发现。你不能有 {"name": "Thomas Reggi"} 因为名字在引号里。

此查询有效。

mutation {
example(credentials: {name: "Thomas Reggi"}) {
name,
age
}
}

关于json - 突变中的单个 JSON 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38862065/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com