graphql - 如何将本地 @client 模式扩展添加到 apollo-codegen
问题描述
我正在使用apollo-link-state 添加 local state,将一些字段添加到查询根,并且我提供了一个typeDef
看起来像这样的内容:
extend type Query {
foo: String
}
我还使用apollo-codegen 来添加流注释。但是,它不应用扩展,并返回错误:
.../src/components/Foo/Foo.js: Cannot query field "foo" on type "Query".
当它遇到对扩展字段的查询时:
query FooQuery {
foo @client
}
所以我的问题是:我如何讲述apollo-codegen
客户端扩展?
解决方案
好的,所以根据这个问题,还没有内置的方法。受zhenwenc 的 gist启发,我编写了一个快速脚本来合并服务器和客户端模式:
#!/usr/bin/env node
const fs = require("fs");
const path = require("path");
const { introspectionFromSchema } = require("graphql/utilities");
const { makeExecutableSchema } = require("graphql-tools");
const { fileLoader, mergeTypes } = require("merge-graphql-schemas");
// Make sure unhandled errors in async code are propagated correctly
process.on("uncaughtException", error => {
console.error(error);
process.exit(1);
});
process.on("unhandledRejection", error => {
throw error;
});
async function introspectSchema(input, output) {
const schemas = [].concat(...input.map(i => fileLoader(i)));
const typeDefs = mergeTypes(schemas, {
all: true
});
const schema = await makeExecutableSchema({
typeDefs,
resolverValidationOptions: { requireResolversForResolveType: false }
});
const introspection = await introspectionFromSchema(schema);
const json = JSON.stringify(introspection, null, 2);
fs.writeFileSync(output, json);
}
const input = [
path.join(__dirname, "../data/*.graphql"),
path.join(__dirname, "../src/*.graphql")
];
const output = path.join(__dirname, "../src/__generated__/schema.json");
// Generate an introspection JSON format from remote GraphQL server merging
// with any local GraphQL schemas
introspectSchema(input, output, true);
并在运行 codegen 之前调用它:
node scripts/merge.js && \
apollo-codegen generate src/components/**/*.js \
--schema src/__generated__/schema.json \
--target flow-modern \
--add-typename \
--use-flow-exact-objects false \
--use-flow-read-only-types true
请注意,这不会验证对客户端模式的查询是否使用该@client
指令,这将是一流支持的巨大优势。但是,这至少会保持生成的类型正常工作!
推荐阅读
- javascript - 为什么在使用道具将值传递给另一个组件时我不能在反应输入字段中连续输入
- java - 着色器在 Java 中的 OpenGL 中无法正常工作
- laravel - 如何使用 GitlabCI 在远程主机上导出环境变量
- authentication - 支持基于目录的个人帐户和工作或学校帐户
- excel - Historian 检索数据
- git - 如果 diff.tool 未设置,Git (2.22.0) difftool 不再传递到 git diff
- php - 如何使用 Ubuntu 在 Ingres 中创建 DSN?
- r - 直方图频率
- reactjs - 从根组件获取状态 - react.js
- reactjs - 使用 Create React App 的环境配置文件