【发布时间】:2019-07-02 06:59:49
【问题描述】:
所以我有这两个模式
架构1
type Permission {
relation: Relation
}
enum Relation {
ONE
TWO
THREE
}
架构2
type Permission {
relation: Relation
}
enum Relation {
FOUR
FIVE
SIX
}
预期结果类似于:(但我对不同的想法持开放态度) 合并后我想做的查询是:
{
permissions{
relation
}
}
得到类似的结果
"permissions": [
{
"relation": "ONE"
},
{
"relation": "SIX"
}
]
或
"permissions": [
{
"relation": "schema1ONE"
},
{
"relation": "schema2SIX"
}
]
还有像这样的突变:
mutation{
createPermission(
relation: ONE
){
relation
}
}
mutation{
createPermission(
relation: SIX
){
relation
}
}
或
mutation{
createPermission(
relation: schema1ONE
){
relation
}
}
mutation{
createPermission(
relation: schema2SIX
){
relation
}
}
我正在尝试在 graphql-tools 上使用 transformSchema 函数,但不能完全正确地弄清楚:
const Schema1 = await getRemoteSchema('schema1_url', 'schema1');
const Schema2 = await getRemoteSchema('schema2_url', 'schema2');
const schemas = [Schema1, Schema2]
const schema = mergeSchemas({
schemas: schemas,
resolvers: {}
});
getRemoteSchema 定义
export const getRemoteSchema = async (uri: string, schemaName: string): Promise<GraphQLSchema> => {
const httpLink = new HttpLink({ uri, fetch });
const schema = await introspectSchema(httpLink);
const executableSchema = makeRemoteExecutableSchema({
schema,
httpLink,
});
// transform schema by renaming root fields and types
const renamedSchema = transformSchema(
executableSchema,
[
new RenameTypes(name => {
if (name == 'Relation') {
return schemaName + name
} else {
return name
}
}),
// new RenameRootFields((operation, name) => `${schemaName}_${name}`)
]
);
return renamedSchema;
}
我犯了这个错误https://glitch.com/edit/#!/schema-stitching-conflict 所以更容易看出问题。
【问题讨论】:
-
所以一方面是类型的重命名,还有权限类型的加入
标签: typescript graphql apollo apollo-server graphql-tools