Skip to content

proxySchema

connector.proxySchema(fn)

Used to define and build a dynamic connector that builds a GraphQL schema using @graphql-tools modules.

Takes a function with an object as a parameter. The object contains the following properties:

ParameterDescription
optionsObject that contains the configuration values set for options defined using defineOptions().
stateObject that contains data stored during init().
typePrefixString that represents a prefix to add all GraphQL types from that data source.

You can use proxySchema() in place of model() and sync() to build a dynamic-only connector, or you can include all of these methods and build a connector that supports a data source that is both static and dynamic.

Learn more about building a dynamic connector.

Usage example

import { makeExecutableSchema } from "@graphql-tools/schema";
import { stitchSchemas } from "@graphql-tools/stitch";
import { buildHTTPExecutor } from "@graphql-tools/executor-http";
import { schemaFromExecutor, RenameTypes } from "@graphql-tools/wrap";
// Connecting an existing GraphQL API
async function getRemoteGraphQLSchema({ typePrefix, uri }) {
const remoteExecutor = buildHTTPExecutor({
endpoint: uri,
});
const schema = {
schema: await schemaFromExecutor(remoteExecutor),
executor: remoteExecutor,
transforms: [new RenameTypes((name) => `${typePrefix}${name}`)],
};
return schema;
}
// Leverage an existing REST API
async function getSchemaFromCustomRestAPI({
typePrefix,
apiClient,
}: {
typePrefix: string;
apiClient: BreweryApiClient;
}) {
const typeDefs = `
enum BreweryType {
micro
large
brewpub
closed
proprietor
contract
}
type Brewery {
id: ID
name: String
brewery_type: BreweryType
address_1: String
address_2: String
address_3: String
city: String
state_province: String
postal_code: String
country: String
longitude: String
latitude: String
phone: String
website_url: String
state: String
street: String
}
type Query {
breweryFromOrigin(id: ID): Brewery
randomBreweryFromOrigin(size: Int): [Brewery]
breweriesFromOrigin(by_type: BreweryType, by_ids: [String], by_name: String, by_postal: String, by_city: String): [Brewery]
}
`;
const resolvers = {
Query: {
breweryFromOrigin: async (_, { id }) => {
return apiClient.breweryById(id);
},
randomBreweryFromOrigin: async (_, { size }) => {
return apiClient.randomBrewery(size);
},
breweriesFromOrigin: async (_, { by_type, by_ids, by_name, by_postal, by_city }) => {
return apiClient.getBreweries({
by_type,
by_ids,
by_name,
by_postal,
by_city,
});
},
},
};
return {
transforms: [new RenameTypes((name) => `${typePrefix}Dynamic${name}`)],
schema: makeExecutableSchema({
typeDefs,
resolvers,
}),
};
}
connector.proxySchema(async ({ typePrefix, state }) => {
const swapiSchema = await getRemoteGraphQLSchema({
typePrefix,
uri: `https://swapi-graphql.netlify.app/.netlify/functions/index`,
});
const brewerySchema = await getSchemaFromCustomRestAPI({
typePrefix,
apiClient: state.client,
});
return stitchSchemas({
subschemas: [swapiSchema, brewerySchema],
});
});

For more details and the full example, refer to this combined static and dynamic connector example repository.

Got it!

Your feedback helps us improve our docs.