Skip to content

proxySchema

For use with Netlify Connect only.

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
getRemoteGraphQLSchemaFunction for creating a dynamic API from an existing GraphQL endpoint.
optionsObject that contains the configuration values set for options defined using defineOptions in addConnector.
stateObject that contains data stored in initState when addConnector runs.
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.

getRemoteGraphQLSchema

Function that allows you to create a dynamic API from an existing GraphQL endpoint.

Takes an object with the following parameters:

ParameterDescription
headersHeaders required to access the schema from your GraphQL endpoint. For example, authentication headers.
urlURI for your GraphQL endpoint.

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],
});
});

Got it!

Your feedback helps us improve our docs.