gpt4 book ai didi

graphql - 如何将本地 @client 模式扩展添加到 apollo-codegen

转载 作者:行者123 更新时间:2023-12-02 18:43:05 25 4
gpt4 key购买 nike

我正在使用apollo-link-state to add local state ,这会向查询根添加一些字段,并且我提供一个看起来像这样的 typeDef:

extend type Query {
foo: String
}

我也在使用apollo-codegen to add flow annotations 。但是,它不应用扩展,并返回错误:

.../src/components/Foo/Foo.js: Cannot query field "foo" on type "Query".

当遇到扩展字段的查询时:

query FooQuery {
foo @client
}

所以我的问题是:如何告诉 apollo-codegen 有关客户端扩展的信息?

最佳答案

好的,所以根据this issue目前还没有内置方法。灵感来自zhenwenc's 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);

并在运行代码生成之前调用它:

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 指令,这将是一流支持的巨大优势。然而,这至少会让生成的类型保持工作!

关于graphql - 如何将本地 @client 模式扩展添加到 apollo-codegen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50308912/

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