在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):poetic/apollo-datasource-graphql开源软件地址(OpenSource Url):https://github.com/poetic/apollo-datasource-graphql开源编程语言(OpenSource Language):TypeScript 100.0%开源软件介绍(OpenSource Introduction):apollo-datasource-graphqlConnect your GraphQL server to an existing GraphQL API using DataSources. Note: This is designed to work with Apollo Server 2.0 and Data Sources GraphQL Data SourceInstall
or
UsageDefine a data source by extending the import { GraphQLDataSource } from 'apollo-datasource-graphql';
import { gql } from 'apollo-server-express';
const CRAFT_BEERS = gql`
query {
craftBeers {
name
style
abv
brewery {
name
}
}
}
`;
export class CraftBeerGraphQLAPI extends GraphQLDataSource {
baseURL = 'https//craft-beer-api.example/graphql';
async getCraftBeers() {
try {
const response = await this.query(CRAFT_BEERS);
return response.data.craftBeers;
} catch (error) {
console.error(error);
}
}
} GraphQL OperationsThe async searchCraftBeerByName(name) {
try {
const response = await this.query(CRAFT_BEERS, {
variables: {
name,
},
});
return response.data.craftBeer;
} catch (error) {
console.error(error);
}
}
Intercepting OperationsYou can intercept the request to set headers on an outgoing request. Since Apollo Data Sources have access to GraphQL context, you can store a user token or other information you need to have available when making a request. Add the method willSendRequest(request) {
const { accessToken } = this.context;
if (!request.headers) {
request.headers = {};
}
request.headers.authorization = accessToken;
} TODO
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论