- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在研究 GraphQL 模式验证工具。我想在内存中更新我的 GraphQLSchema
目的。
例如替换我尝试做的类型:
const replaceType = (schema: GraphQLSchema, oldType: GraphQLNamedType, newType: GraphQLNamedType) => {
const config = schema.toConfig();
config.types = config.types.filter(t => t.name !== oldType.name);
config.types.push(newType);
return new GraphQLSchema(config);
}
Schema must contain uniquely named types but contains multiple types named "MyType".
at typeMapReducer (../../node_modules/graphql/type/schema.js:262:13)
at Array.reduce (<anonymous>)
at new GraphQLSchema (../../node_modules/graphql/type/schema.js:145:28)
最佳答案
如果这对其他人有用,则类型引用的以下更新似乎有效
const replaceType = (schema: GraphQLSchema, oldType: GraphQLNamedType, newType: GraphQLNamedType) => {
const config = schema.toConfig();
config.types = config.types.filter(t => t.name !== oldType.name);
config.types.push(newType);
makeConfigConsistent(config);
return new GraphQLSchema(config);
}
/**
* As we add types that originally come from a different schema, we need to update all the references to maintain consistency
* within the set of types we are including.
*
* Types from the original schema need to update their references to point to the new types,
* and types from the new schema need to update their references to point to the original types that were not replaces.
*/
const makeConfigConsistent = (config: SchemaConfig) => {
const typeMap: { [typeName: string]: GraphQLNamedType } = {};
// Update references for root types
config.query = null;
config.mutation = null;
config.subscription = null;
config.types.forEach(type => {
typeMap[type.name] = type;
if (isObjectType(type)) {
if (type.name === 'Query') {
config.query = type;
} else if (type.name === 'Mutation') {
config.mutation = type;
} else if (type.name === 'Subscription') {
config.subscription = type;
}
}
});
// Update references to only point to the final set of types.
const finalTypes = config.types;
if (config.query) {
finalTypes.push(config.query);
}
if (config.mutation) {
finalTypes.push(config.mutation);
}
if (config.subscription) {
finalTypes.push(config.subscription);
}
const updatedType = (type: any): any | undefined => {
if (isNamedType(type)) {
if (type === typeMap[type.name]) {
return type;
}
}
if (isListType(type)) {
const subType = updatedType(type.ofType);
if (!subType) {
return undefined;
}
return new GraphQLList(subType);
}
if (isNonNullType(type)) {
const subType = updatedType(type.ofType);
if (!subType) {
return undefined;
}
return new GraphQLNonNull(subType);
}
if (isScalarType(type)) {
if (type === typeMap[type.name]) {
return type;
}
if (['Int', 'String', 'Float', 'Boolean', 'ID'].includes(type.name)) {
// This is a default scalar type (https://graphql.org/learn/schema/#scalar-types)
return type;
}
}
if (isNamedType(type)) {
const result = typeMap[type.name];
if (!result) {
return undefined;
}
return result;
}
throw new Error(`Unhandled cases for ${type}`);
};
finalTypes.forEach(type => {
if (isObjectType(type) || isInterfaceType(type)) {
const anyType = type as any;
anyType._fields = arraytoDict(
Object.values(type.getFields())
.filter(field => updatedType(field.type) !== undefined)
.map(field => {
field.type = updatedType(field.type);
field.args = field.args
.filter(arg => updatedType(arg.type) !== undefined)
.map(arg => {
arg.type = updatedType(arg.type);
return arg;
});
return field;
}),
field => field.name,
);
if (isObjectType(type)) {
anyType._interfaces = type.getInterfaces().map(int => updatedType(int));
}
} else if (isInputObjectType(type)) {
const anyType = type as any;
anyType._fields = arraytoDict(
Object.values(type.getFields())
.filter(field => updatedType(field.type) !== undefined)
.map(field => {
field.type = updatedType(field.type);
return field;
}),
field => field.name,
);
} else if (isUnionType(type)) {
const anyType = type as any;
anyType._types = type
.getTypes()
.map(t => updatedType(t))
.filter(t => t !== undefined);
}
});
};
function arraytoDict<T>(array: T[], getKey: (element: T) => string): { [key: string]: T } {
const result: { [key: string]: T } = {};
array.forEach(element => {
result[getKey(element)] = element;
});
return result;
};
关于graphql - 如何从 GraphQLSchema 添加/替换/删除类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60895759/
我正在使用 npm link更改我的私有(private)包并查看另一个项目中的操作更改。问题是我的两个项目都使用 graphql作为一个依赖,所以我得到一个错误 Cannot use GraphQL
给定以下代码: import { graphql } from 'graphql' import graphqlTools from 'graphql-tools' const { makeExecu
我正在使用 type-graphql 构建无服务器后端和 apollo-server-lambda ,但是在对 graphql 端点发出大约第三次请求后,我收到错误 Error: Cannot use
我是 GraphQL 的新手。如果这很明显,请原谅我。 除了使用 buildSchema , 有没有一种方法可以使用 new GraphQLSchema 定义多个查询/变异? 这就是我现在所拥有的。
两者之间有什么显着差异吗?我对从运行时和启动性能到功能和工作流程差异的任何事情都感兴趣。文档在解释差异以及何时应该使用一个而不是另一个方面做得很差。 两个版本中的示例: 构建架构 const { gr
我正在研究 GraphQL 模式验证工具。我想在内存中更新我的 GraphQLSchema目的。 例如替换我尝试做的类型: const replaceType = (schema: GraphQLSc
我在这里遵循这个例子:- http://www.baeldung.com/spring-graphql 对我来说,GraphQLSchema bean 没有自动注册。它抛出这个错误:-没有可用的“gr
我正在使用 graphql-js 创建一个新的 graphQL 模式但不会通过 http 提供服务,将用作本地 graphql 服务。我需要以 json 格式获取此 graphql 的架构,以将其与另
我是一名优秀的程序员,十分优秀!