问题标签 [graphql-codegen]
For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.
mongodb - 为 GraphQL 输出解析自动生成的 typescript-mongodb 类型
我正在使用该typescript-mongodb
插件graphql-codegen
生成 Typescript 类型,用于从 MongoDB 中提取数据并通过 Node.js 上的 GraphQL 输出。
我的输入 GraphQL 架构如下所示
生成的输出 Typescript 类型看起来正确
以及对应的DB对象
问题是当实际发送回 mongo 文档时,UserDbObject
我没有得到输出中映射的字段。我可以编写一个自定义解析器,将字段重新映射回User
类型,但这意味着我将字段映射到两个不同的地方。
即我没有像这样从解析器获取映射字段
有没有办法使用typescript-mongodb
插件只需要在模式中映射这些字段,然后使用自动生成的代码来解析它们?
reactjs - 使用 typescript + 生成的核心作为包调用 graphql-codegen 查询时,挂钩调用无效
我正在使用带有生成代码的 npm 包和使用它的移动包的 Yarn 工作区。
尝试从导入的查询中拨打电话时,我收到无效的挂钩调用。在该组件中使用任何其他钩子(即 useState)都可以正常工作。
我的移动包的 package.json 导入:
"@workspace-library/core": "1.0.0",
我在 core/src 有一个 index.js 文件,它有:
我像这样导入查询:
我可以在生成的/graphql.jsx 中看到 useArticlesQuery,以防万一:
export function useArticlesQuery(baseOptions) {
return ApolloReactHooks.useQuery(ArticlesDocument, baseOptions);
}
环境:
- 操作系统:Ubuntu
- "@graphql-codegen/cli": "^1.13.1",
- "@graphql-codegen/typescript": "^1.13.1",
- "@graphql-codegen/typescript-operations": "^1.13.1",
- "@graphql-codegen/typescript-react-apollo": "^1.13.1",
- 纱线工作区
有问题的附加上下文 错误:
typescript - 从graphql接口生成打字稿接口的graphql-codegen问题
从我的 graphQL 模式生成 TS 接口时遇到问题。我的代码:
当我在我的 graphql 操场上运行服务器时,我可以看到SignUpRequestInput
输入和SomeInter
界面,但是当我运行graphql-codegen
脚本时它只生成SignUpRequestInput
界面并省略SomeInter
我正在运行的脚本:
代码生成.yml:
有谁知道我做错了什么?谢谢
angular - 使用 strictNullChecks 从可能为 null 的属性中键入别名
我graphql-codegen/typescript-apollo-angular
用来生成打字稿的代码可以在我们的 Angular 应用程序中使用。我们决定启用strictNullChecks
唯一的问题是我们使用以下模式来指定 API 返回的内部节点的类型:
在我的情况下something
,Maybe<Array<...>>
它是正确的。当我启用strictNullChecks
上述代码时,由于某些内容也可能未定义/为空,因此不再起作用。
我无法找到有关如何从这样的类型别名中删除null
/类型的文档。undefined
也许有不同的方法可以做到这一点。
graphql-codegen - 突变后更新缓存:类型'{名称:任何;属性'名称'不存在; } | 无效的'
使用带有钩子的 React 的 GraphQL 代码生成器,我正在尝试在突变后更新 Apollo 缓存,遵循Apollo 文档中的示例:
但是,我得到Property 'names' does not exist on type '{ names: any; } | null'.
了解构查询变量。
我究竟做错了什么?
提前致谢。
typescript - 自动生成函数的类型安全包装,然后仅使用 `__typename` 作为参数动态调用。打字稿
我有 awesome 的完全类型安全的自动生成代码graphql-codgen/vue
。我通过构建一个小包装器在我的项目中使用它,这样我的用户就不必在每次调用时都执行常见的配置任务。例如定义缓存行为、自动更新缓存、解构导致正确的类型和格式。
使用 JS 和 with 的包装器工作者,any
但我也希望它是类型安全的,并且由于graphql-codegen
已经以类型安全的方式生成所有类型和方法,我认为必须有一种方法可以做到这一点。不知何故,我认为有歧视性的工会......
所以归结为示例代码我的问题是:我有这个自动生成的代码:
现在我想用最少的 DRY 像这样“动态地”使用它:
意图
我真的不想重新创建graphql-codgen所做的所有工作TypeTable
,因为我认为所有这些工作都已经由graphql-codegen完成。
我的目标是有人可以创建一个新的ExamplesList.graphql
,graphql-codegen
包装它,然后准备好使用useQueryList("example")
因此,虽然这是一个动态传递的参数,但它也必须能够通过某种方式映射所有服务函数的返回类型,然后得到返回的一个,Array<__typename>
或者我错了吗?而且我认为我必须以某种方式必须typename
通过解析__typenames
来自Service
实际上并不是我们所做的一切,我们对其进行了更多的包装和转换,但是一旦我在这里获得了正确的类型,一切都会好起来的。
typescript - 从 Typescript 中的 union 引用 Complex 类型
我graphql-codegen
用来从我的 graphQL 查询中生成类型。
结果有时非常复杂,特别unions
是在涉及时。
这是一个具体的例子
所以我想做的是能够参考工会的具体案例__typename
也许是实用程序类型?
typescript - 可以创建连接两个字段的打字稿类型吗?
我想做这样的事情:
所以得到的类型RealApple
是:
这可能吗?我该如何实施WithConcat
?原因是在与数据库对话时处理类型,其中复合排序键是从其他两个字段创建的,这些字段在架构上实际上没有该复合。
angular - 阿波罗角服务
我正在使用 grapqhl-codegen 将我的查询转换为可注入的服务,一切都像一个魅力,此外,我不能 updateQuerys 了,因为我只有服务,我不能将服务传递到 updatesQuerys 数组中。我希望 userGql 服务重新获取数据,而不是使用缓存:
angular - 带有插值的 graphql-codegen 动态字段
我正在使用graphql-codegen从 graphql 模式生成打字稿类型。我正在尝试创建一个带有动态字段的片段。
架构.ts
这是由 生成的类型graphql-codegen
。
用户模型.ts
我在整个应用程序中使用这些接口进行验证和一致性。
show.ts
这是用于生成的文件。在这里,我想使用我的existentIUserPost
和IUserProfile
接口制作一个带有动态字段的片段,以便重用这些字段并避免在片段中一一重复它们。
当我尝试使用插值传递这些字段时,出现此错误:
发现 1 个错误
× C:/Development/GogoFans/gogofans-ui/src/app/users/graphql/fragments/show.ts
GraphQLError: Syntax Error: Expected Name, found "}"。
在 Parser.expectToken (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\ language \ 的 syntaxError (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\error\syntaxError.js:15:10) parser.js:1423:40)
在 Parser.parseName (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:92:22)
在 Parser.parseField (C:\Development\GogoFans\ gogofans-ui\node_modules\graphql\language\parser.js:289:28)
在 Parser.parseSelection (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:278:81)
在 Parser .many (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:1537:26)
在 Parser.parseSelectionSet (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\ parser.js:265:24)
在 Parser.parseFragmentDefinition (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:410:26)
在 Parser.parseDefinition (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\语言\parser.js:134:23)
在 Parser.many (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:1537:26)
在 Parser.parseDocument (C:\Development\ GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:109:25)
在 Object.parse (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:36:17)
在 Object.parseGraphQLSDL (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\utils\index.cjs.js:601:28)
在 parseSDL (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\code-file-loader\index.cjs.js:239:18)
在 CodeFileLoader.load (C:\Development\GogoFans\gogofans- ui\node_modules@graphql-tools\code-file-loader\index.cjs.js:173:28)
在异步 loadFile (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\load\index.cjs .js:48:24)GraphQLError:语法错误:预期名称,找到“}”。
在 Parser.expectToken (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\ language \ 的 syntaxError (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\error\syntaxError.js:15:10) parser.js:1423:40)
在 Parser.parseName (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:92:22)
在 Parser.parseField (C:\Development\GogoFans\ gogofans-ui\node_modules\graphql\language\parser.js:289:28)
在 Parser.parseSelection (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:278:81)
在 Parser .many (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:1537:26)
在 Parser.parseSelectionSet (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\ parser.js:265:24)
在 Parser.parseFragmentDefinition (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:410:26)
在 Parser.parseDefinition (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\语言\parser.js:134:23)
在 Parser.many (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:1537:26)
在 Parser.parseDocument (C:\Development\ GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:109:25)
在 Object.parse (C:\Development\GogoFans\gogofans-ui\node_modules\graphql\language\parser.js:36:17)
在 Object.parseGraphQLSDL (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\utils\index.cjs.js:601:28)
在 parseSDL (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\code-file-loader\index.cjs.js:239:18)
在 CodeFileLoader.load (C:\Development\GogoFans\gogofans- ui\node_modules@graphql-tools\code-file-loader\index.cjs.js:173:28)
在异步 loadFile (C:\Development\GogoFans\gogofans-ui\node_modules@graphql-tools\load\index.cjs .js:48:24)出问题了
npm 错误!代码 ELIFECYCLE
npm 错误!errno 1
npm 错误!gogofans-ui@0.0.0 生成:graphql-codegen --config codegen.yml
npm ERR!退出状态 1
npm ERR!
npm 错误!gogofans-ui@0.0.0 生成脚本失败。
npm 错误!这可能不是 npm 的问题。上面可能有额外的日志输出。npm 错误!可以在以下位置找到此运行的完整日志:
npm ERR!C:\Users\Fidel\AppData\Roaming\npm-cache_logs\2020-07-06T07_01_25_424Z-debug.log