在Apollo服务器中使用多个第三方APIs可以通过多种方式实现,具体取决于你如何管理和调用这些APIs。以下是一些常见的方法:
你可以在GraphQL的resolver函数中直接调用第三方API。
const { ApolloServer, gql } = require('apollo-server');
const axios = require('axios');
// 定义Schema
const typeDefs = gql`
type Query {
getDataFromApi1: String
getDataFromApi2: String
}
`;
// 定义Resolvers
const resolvers = {
Query: {
getDataFromApi1: async () => {
const response = await axios.get('https://api.example.com/data1');
return response.data;
},
getDataFromApi2: async () => {
const response = await axios.get('https://api.example.com/data2');
return response.data;
},
},
};
// 创建Apollo服务器
const server = new ApolloServer({ typeDefs, resolvers });
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
如果你的应用程序需要批量处理多个API请求,或者想要减少重复的API调用,可以使用数据加载器。
const DataLoader = require('dataloader');
// 创建数据加载器
const api1Loader = new DataLoader(async (keys) => {
const responses = await axios.all(keys.map(key => axios.get(`https://api.example.com/data1/${key}`)));
return responses.map(response => response.data);
});
const api2Loader = new DataLoader(async (keys) => {
const responses = await axios.all(keys.map(key => axios.get(`https://api.example.com/data2/${key}`)));
return responses.map(response => response.data);
});
// 在Resolver中使用数据加载器
const resolvers = {
Query: {
getDataFromApi1: async (_, { id }) => api1Loader.load(id),
getDataFromApi2: async (_, { id }) => api2Loader.load(id),
},
};
对于更复杂的逻辑,你可以创建一个中间件或服务层来处理API调用。
// apiService.js
const axios = require('axios');
class ApiService {
async getDataFromApi1(id) {
const response = await axios.get(`https://api.example.com/data1/${id}`);
return response.data;
}
async getDataFromApi2(id) {
const response = await axios.get(`https://api.example.com/data2/${id}`);
return response.data;
}
}
module.exports = new ApiService();
// resolvers.js
const apiService = require('./apiService');
const resolvers = {
Query: {
getDataFromApi1: async (_, { id }) => apiService.getDataFromApi1(id),
getDataFromApi2: async (_, { id }) => apiService.getDataFromApi2(id),
},
};
Apollo Server支持插件,你可以创建自定义插件来封装API调用逻辑。
// apollo-server-plugin-api.js
const axios = require('axios');
class ApiPlugin {
async getDataFromApi1({ id }) {
const response = await axios.get(`https://api.example.com/data1/${id}`);
return response.data;
}
async getDataFromApi2({ id }) {
const response = await axios.get(`https://api.example.com/data2/${id}`);
return response.data;
}
}
module.exports = new ApiPlugin();
// server.js
const { ApolloServer, gql } = require('apollo-server');
const apiPlugin = require('./apollo-server-plugin-api');
const typeDefs = gql`
type Query {
getDataFromApi1(id: ID!): String
getDataFromApi2(id: ID!): String
}
`;
const resolvers = {
Query: {
getDataFromApi1: apiPlugin.getDataFromApi1.bind(apiPlugin),
getDataFromApi2: apiPlugin.getDataFromApi2.bind(apiPlugin),
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
plugins: [apiPlugin],
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
选择哪种方法取决于你的应用程序的具体需求和架构。对于简单的用例,直接在resolver中调用可能是最简单的方法。对于更复杂的场景,使用数据加载器、中间件或服务层可能更合适。如果你想要更多的灵活性和控制,可以考虑使用Apollo Server插件。
领取专属 10元无门槛券
手把手带您无忧上云