You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用graphql-tools的makeRemoteExecutableSchema时,如何为远程GraphQL Schema类型名加前缀?

Adding Prefixes to Remote GraphQL Schema Types with graphql-tools

Absolutely, there's a clean and straightforward way to add prefixes to your remote schema's type names before merging it with your local schema—this is crucial to avoid naming collisions between remote and local types (like if both schemas have a User type, for example).

Here are the two most common, easy-to-implement approaches:

The @graphql-tools/wrap package includes a transformSchema utility paired with the RenameTypes transformer that lets you modify type names across the entire remote schema in one go.

Step-by-Step Implementation:

First, install the necessary packages if you haven't already:

npm install @graphql-tools/wrap @graphql-tools/schema

Then, modify your code to transform the remote schema before creating the executable version:

const { makeRemoteExecutableSchema, mergeSchemas, introspectSchema } = require('@graphql-tools/schema');
const { transformSchema, RenameTypes } = require('@graphql-tools/wrap');
const { HttpLink } = require('apollo-link-http');
const fetch = require('node-fetch');

// 1. Set up your remote API link
const remoteLink = new HttpLink({ uri: 'https://your-remote-graphql-api.com/graphql', fetch });

// 2. Fetch the raw remote schema
const remoteSchema = await introspectSchema(remoteLink);

// 3. Add a custom prefix to all remote type names
const prefixedRemoteSchema = transformSchema(remoteSchema, [
  new RenameTypes((typeName) => {
    // Customize your prefix here—e.g., prepend "Remote" to every type
    return `Remote${typeName}`;
  })
]);

// 4. Create the executable remote schema with prefixed types
const executableRemoteSchema = makeRemoteExecutableSchema({
  schema: prefixedRemoteSchema,
  link: remoteLink,
});

// 5. Merge with your local schema
const localSchema = buildSchema(`
  type LocalUser {
    id: ID!
    name: String!
  }
  type Query {
    localUser(id: ID!): LocalUser
  }
`);

const mergedSchema = mergeSchemas({
  schemas: [localSchema, executableRemoteSchema],
});

This transformer automatically renames every type in the remote schema (e.g., User → RemoteUser, Post → RemotePost) and adjusts all references to those types (fields, arguments, unions, interfaces) to match the new names.

2. Targeted Type Renaming (For Edge Cases)

If you only need to prefix specific types instead of all, tweak the RenameTypes callback to target just those:

new RenameTypes((typeName) => {
  const typesToPrefix = ['User', 'Post'];
  return typesToPrefix.includes(typeName) ? `Remote${typeName}` : typeName;
})

Key Notes:

  • Test the merged schema after transformation to ensure all fields and queries still function as expected.
  • The transformer handles enums, scalars, and interfaces automatically—no extra configuration needed for those types.

内容的提问来源于stack exchange,提问作者Jonathan Byrne

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:24:32