我的数据模型是续集,我正在寻找一种简单的方法来使用 from: 和 to: 参数来指定范围来查询 graphql。
有语法吗?
目前,在 GraphQL JS 实现中,没有从突变返回描述性成功消息的标准方法,例如“用户创建成功”等。我的问题有两部分:
我的想法是从 API 返回成功消息是有益的。这样,消息可以在不同客户端(例如网络和移动设备)上保持一致。客户端不必为每个 API 调用实现自定义消息。但我不确定最佳实践是什么。从突变调用返回描述性成功消息是否有任何缺点(除了增加响应大小之外)?
如果我决定这样做,graphql-js 中是否有标准方法可以做到这一点?
例如,是否可以取回如下内容:
{
data: {
mutationName: {var1:"val1"}
},
messages: {
mutationName:"User created successfully"
}
}
Run Code Online (Sandbox Code Playgroud) 我想知道为什么我的参数似乎在我的 GraphQL 解析器中发生了变化。我正在使用express-graphql。
\n\n一个解析器的示例:
\n\n getLocalDrivers: async (parent, args, ctx) => {\n console.log(ctx);\n }\nRun Code Online (Sandbox Code Playgroud)\n\n我已经编写了文档中出现的参数名称:http://graphql.org/learn/execution/
\n\n但是当我调试和检查对象时,似乎 args 对象是第一个,上下文是第二个,父/根是第三个。
\n\n家长:
\n\nObject {location: "020202"}\nRun Code Online (Sandbox Code Playgroud)\n\n参数:
\n\nIncomingMessage {_readableState: ReadableState, readable: false, domain: null, \xe2\x80\xa6}\nRun Code Online (Sandbox Code Playgroud)\n\n语境:
\n\nObject {fieldName: "getLocalDrivers", fieldNodes: ....\nRun Code Online (Sandbox Code Playgroud)\n\n一些服务器代码:
\n\napp.use(\n "/graphql",\n graphqlHTTP({\n schema,\n graphiql: true,\n rootValue: rootResolver\n })\n);\nRun Code Online (Sandbox Code Playgroud)\n\n我的根解析器:
\n\nvar rootResolver = {\n getLocalDrivers: async (obj, args, ctx) => {\n console.log(ctx);\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n架构:
\n\nvar { …Run Code Online (Sandbox Code Playgroud) 我有一个关于express-graphql.\nI\xe2\x80\x99m 的问题,试图在 GraphQL 解析器之后运行中间件。
这是我的前两次尝试:
\n\napp.use('/api', graphqlHTTP({\n schema: graphqlSchema\n })\n);\n\napp.use((req, res, next) => {\n console.log('middleware called');\n next();\n});\nRun Code Online (Sandbox Code Playgroud)\n\n和
\n\napp.use('/api', graphqlHTTP({\n schema: graphqlSchema,\n graphiql: true,\n }), () => {\n console.log('middleware called');\n }\n);\nRun Code Online (Sandbox Code Playgroud)\n\n两者都\xe2\x80\x99t 工作。\n我猜express-graphql不是next()在某个地方打电话。
消息来源似乎证实了这一点:
\n\ntype Middleware = (request: Request, response: Response) => void;\nRun Code Online (Sandbox Code Playgroud)\n\nnext不是参数。
我尝试了这个解决方法:
\n\napp.use( (req, res, next) => {\n req.on('end', () => {\n console.log('middleware called');\n });\n next();\n});\n\napp.use('/api', graphqlHTTP({\n schema: graphqlSchema\n })\n);\n …Run Code Online (Sandbox Code Playgroud) 我这里有一个简单的场景。
我有一个 People Sequelize 模型,有 5 个字段:firstName、lastName、id、city 和 zipCode。我还有一个具有相同字段的 GraphQL 对象。
每次我进行 GraphQL 查询时,例如:
{
people(id:123){
firstName
lastName
}
Run Code Online (Sandbox Code Playgroud)
Sequelize 发送如下 SQL 语句:
SELECT `firstName`, `lastName`, `id`, `city`, `zipCode` FROM `People` WHERE `People`.`id` = 123;
Run Code Online (Sandbox Code Playgroud)
我是否可以让 Sequelize 发送如下 SQL 语句:
SELECT `firstName`, `lastName` FROM `People` WHERE `People`.`id` = 123;
Run Code Online (Sandbox Code Playgroud)
基本上我想要的是 SQL 语句不会选择任何我不想要的额外列。
这是我在 GraphQL 查询对象中的解析函数:
resolve: function (root, args) {
return Db.models.People.findAll({ where: args });
}
Run Code Online (Sandbox Code Playgroud) 我有一个 Gatsby GraphQL 查询,用于获取按日期排序并按类别过滤的帖子列表。
{
posts: allContentfulPost(
sort: {fields: [date], order: DESC},
filter: {category: {slug: {eq: $slug}}}
) {
edges {
node {
title {
title
}
date
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在什么时候$slug是空字符串"",我明白了
{
"data": {
"posts": null
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法获取所有帖子?
当我使用 gql(来自“graphql-tag”)进行查询/突变/订阅时,出现错误。
\n有谁遇到过这个错误并且知道如何修复它?
\n这是我的代码:
import React from \'react\';\nimport { Mutation } from \'react-apollo\';\nimport MUTATION from \'./query.js\';\nimport gql from \'graphql-tag\';\n\nconst ADD_POST_MUTATION = gql`\n mutation addPost($content: String!, $author: String!)\xc2\xa0{\n addPost(content: $content, author: $author) {\n content\n author\n }\n }\n`;\n\nexport default class Tool extends React.Component {\n state = {\n content: \'\',\n author: localStorage.getItem(\'user\') || \'\'\n };\n\n handleSubmit = (e, mutation) => {\n console.log(mutation);\n e.preventDefault();\n mutation({ variables: { content: this.state.content, author: this.state.author }})\n .then(res => console.log(res))\n .catch(e => console.log(e));\n }\n\n render() {\n return …Run Code Online (Sandbox Code Playgroud) 我正在学习 GraphQL 的过程中,偶然发现了操作参数和GraphQL 变量之间的区别。因为 IMO,两者都提供客户端将动态数据传递给突变或查询等的设施。
有人可以启发我吗?
干杯!
在此处的中继文档中,它说:
Relay 使用通用的突变模式,其中突变类型的根字段具有单个参数(输入),并且输入和输出都包含用于协调请求和响应的客户端突变标识符。
但在他们提供的示例中,输入和输出分别如下所示:
// IntroducedShipInput
{
"input": {
"shipName": "B-Wing",
"factionId": "1"
}
}
// IntroducedShipPayload
{
"introduceShip": {
"ship": {
"id": "U2hpcDo5",
"name": "B-Wing"
},
"faction": {
"name": "Alliance to Restore the Republic"
}
}
}
Run Code Online (Sandbox Code Playgroud)
那么client mutation标识符是什么呢?为什么以及如何使用它来协调请求和响应?
在我的数据模型中,作为 id 的类型,我想生成一个以用户开头的默认值。
所以 id 会像这样user..rear33422qeqr
type User {
myId: String! @default (`user${2+2}`)
name: String!
email: String! @unique
}
Run Code Online (Sandbox Code Playgroud)
是否可以调用 javascript 函数或使用字符串插值 ( user${2+2})
graphql ×10
graphql-js ×10
contentful ×1
express ×1
gatsby ×1
javascript ×1
middleware ×1
node.js ×1
prisma ×1
react-apollo ×1
reactjs ×1
relay ×1
relayjs ×1
relaymodern ×1
sequelize.js ×1