在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):yarax/swagger-to-graphql开源软件地址(OpenSource Url):https://github.com/yarax/swagger-to-graphql开源编程语言(OpenSource Language):TypeScript 97.5%开源软件介绍(OpenSource Introduction):Swagger-to-GraphQLSwagger-to-GraphQL converts your existing Swagger schema to an executable GraphQL schema where resolvers perform HTTP calls to certain real endpoints. It allows you to move your API to GraphQL with nearly zero effort and maintain both REST and GraphQL APIs. Our CLI tool also allows you get the GraphQL schema in Schema Definition Language. Try it online! You can paste in the url to your own Swagger schema. There are also public OpenAPI schemas available in the APIs.guru OpenAPI directory. Features
UsageBasic serverThis library will fetch your swagger schema, convert it to a GraphQL schema and convert GraphQL parameters to REST parameters. From there you are control of making the actual REST call. This means you can reuse your existing HTTP client, use existing authentication schemes and override any part of the REST call. You can override the REST host, proxy incoming request headers along to your REST backend, add caching etc. import express, { Request } from 'express';
import graphqlHTTP from 'express-graphql';
import { createSchema, CallBackendArguments } from 'swagger-to-graphql';
const app = express();
// Define your own http client here
async function callBackend({
context,
requestOptions,
}: CallBackendArguments<Request>) {
return 'Not implemented';
}
createSchema({
swaggerSchema: `./petstore.yaml`,
callBackend,
})
.then(schema => {
app.use(
'/graphql',
graphqlHTTP(() => {
return {
schema,
graphiql: true,
};
}),
);
app.listen(3009, 'localhost', () => {
console.info('http://localhost:3009/graphql');
});
})
.catch(e => {
console.log(e);
}); Constructor (graphQLSchema) arguments: export interface Options<TContext> {
swaggerSchema: string | JSONSchema;
callBackend: (args: CallBackendArguments<TContext>) => Promise<any>;
}
CLI usageYou can use the library just to convert schemas without actually running server
Apollo FederationApollo federation support can be added by using graphql-transform-federation. You can extend your swagger-to-graphql schema with other federated schemas or the other way around. See the demo with a transformed schema for a working example. Defining your HTTP clientThis repository has:
To get started install npm install node-fetch --save Implementing your own HTTP clientThere a unit test for our HTTP client example, it might be useful when implementing your own client as well. The function
export interface CallBackendArguments<TContext> {
context: TContext;
requestOptions: RequestOptions;
} RequestOptionsexport interface RequestOptions {
baseUrl?: string;
path: string;
method: string;
headers?: {
[key: string]: string;
};
query?: {
[key: string]: string | string[];
};
body?: any;
bodyType: 'json' | 'formData';
}
Resources
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论