Skip to content

Commit a86d3a8

Browse files
authored
feat: allow passing in endpoint to constructor (#1)
Works with either a string, or a function on context
1 parent 7cbb9d1 commit a86d3a8

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

src/index.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { createGQLObject, mapParametersToFields } from './typeMap';
77

88
type Endpoints ={[string]: Endpoint};
99

10-
const schemaFromEndpoints = (endpoints: Endpoints) => {
10+
const schemaFromEndpoints = (endpoints: Endpoints, proxyUrl: ?(Function | string) = null) => {
1111
const rootType = new GraphQLObjectType({
1212
name: 'Query',
1313
fields: () => ({
1414
viewer: {
1515
type: new GraphQLObjectType({
1616
name: 'viewer',
1717
fields: () => {
18-
const queryFields = getQueriesFields(endpoints, false);
18+
const queryFields = getQueriesFields(endpoints, false, proxyUrl);
1919
if (!Object.keys(queryFields).length) {
2020
throw new Error('Did not find any GET endpoints');
2121
}
@@ -31,7 +31,7 @@ const schemaFromEndpoints = (endpoints: Endpoints) => {
3131
query: rootType
3232
};
3333

34-
const mutationFields = getQueriesFields(endpoints, true);
34+
const mutationFields = getQueriesFields(endpoints, true, proxyUrl);
3535
if (Object.keys(mutationFields).length) {
3636
graphQLSchema.mutation = new GraphQLObjectType({
3737
name: 'Mutation',
@@ -42,17 +42,18 @@ const schemaFromEndpoints = (endpoints: Endpoints) => {
4242
return new GraphQLSchema(graphQLSchema);
4343
};
4444

45-
const resolver = (endpoint: Endpoint) =>
45+
const resolver = (endpoint: Endpoint, proxyUrl: ?(Function | string)) =>
4646
async (_, args: GraphQLParameters, opts: SwaggerToGraphQLOptions) => {
47-
const req = endpoint.request(args, opts.GQLProxyBaseUrl);
47+
const proxy = !proxyUrl ? opts.GQLProxyBaseUrl : typeof proxyUrl === 'function' ? proxyUrl(opts) : proxyUrl
48+
const req = endpoint.request(args, proxy);
4849
if (opts.headers) {
4950
req.headers = Object.assign({}, req.headers, opts.headers);
5051
}
5152
const res = await rp(req);
5253
return JSON.parse(res);
5354
};
5455

55-
const getQueriesFields = (endpoints: Endpoints, isMutation: boolean): {[string]: GraphQLType} => {
56+
const getQueriesFields = (endpoints: Endpoints, isMutation: boolean, proxyUrl: ?(Function | string)): {[string]: GraphQLType} => {
5657
return Object.keys(endpoints).filter((typeName: string) => {
5758
return !!endpoints[typeName].mutation === !!isMutation;
5859
}).reduce((result, typeName) => {
@@ -62,17 +63,17 @@ const getQueriesFields = (endpoints: Endpoints, isMutation: boolean): {[string]:
6263
type,
6364
description: endpoint.description,
6465
args: mapParametersToFields(endpoint.parameters, typeName),
65-
resolve: resolver(endpoint)
66+
resolve: resolver(endpoint, proxyUrl)
6667
};
6768
result[typeName] = gType;
6869
return result;
6970
}, {});
7071
};
7172

72-
const build = async (swaggerPath: string) => {
73+
const build = async (swaggerPath: string, proxyUrl: ?(Function | string) = null) => {
7374
const swaggerSchema = await loadSchema(swaggerPath);
7475
const endpoints = getAllEndPoints(swaggerSchema);
75-
const schema = schemaFromEndpoints(endpoints);
76+
const schema = schemaFromEndpoints(endpoints, proxyUrl);
7677
return schema;
7778
};
7879

0 commit comments

Comments
 (0)