Skip to content

Commit 24b6c57

Browse files
committed
schemaFromEndpoints method
1 parent 6046872 commit 24b6c57

File tree

2 files changed

+39
-32
lines changed

2 files changed

+39
-32
lines changed

lib/index.js

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,51 @@ const rp = require('request-promise');
44
const {GraphQLSchema, GraphQLObjectType} = require('graphql');
55
const {getAllEndPoints, loadSchema} = require('./swagger');
66
const {createGQLObject, mapParametersToFields} = require('./type_map');
7-
const build = (swaggerPath) => {
8-
return loadSchema(swaggerPath).then(swaggerSchema => {
9-
const endpoints = getAllEndPoints(swaggerSchema);
10-
const rootType = new GraphQLObjectType({
11-
name: 'Query',
12-
fields: () => ({
13-
viewer: {
14-
type: new GraphQLObjectType({
15-
name: 'viewer',
16-
fields: () => {
17-
const queryFields = getQueriesFields(endpoints, false);
18-
if (!Object.keys(queryFields).length) {
19-
throw new Error('Did not find any GET endpoints');
20-
}
21-
return queryFields;
7+
8+
const schemaFromEndpoints = (endpoints) => {
9+
const rootType = new GraphQLObjectType({
10+
name: 'Query',
11+
fields: () => ({
12+
viewer: {
13+
type: new GraphQLObjectType({
14+
name: 'viewer',
15+
fields: () => {
16+
const queryFields = getQueriesFields(endpoints, false);
17+
if (!Object.keys(queryFields).length) {
18+
throw new Error('Did not find any GET endpoints');
2219
}
23-
}),
24-
resolve: () => 'Without this resolver graphql does not resolve further'
25-
}
26-
})
27-
});
20+
return queryFields;
21+
}
22+
}),
23+
resolve: () => 'Without this resolver graphql does not resolve further'
24+
}
25+
})
26+
});
2827

29-
const graphQLSchema = {
30-
query: rootType
31-
};
28+
const graphQLSchema = {
29+
query: rootType
30+
};
3231

33-
const mutationFields = getQueriesFields(endpoints, true);
34-
if (Object.keys(mutationFields).length) {
35-
graphQLSchema.mutation = new GraphQLObjectType({
36-
name: 'Mutation',
37-
fields: mutationFields
38-
});
39-
}
32+
const mutationFields = getQueriesFields(endpoints, true);
33+
if (Object.keys(mutationFields).length) {
34+
graphQLSchema.mutation = new GraphQLObjectType({
35+
name: 'Mutation',
36+
fields: mutationFields
37+
});
38+
}
39+
40+
return new GraphQLSchema(graphQLSchema);
41+
};
4042

41-
return new GraphQLSchema(graphQLSchema);
43+
const build = (swaggerPath) => {
44+
return loadSchema(swaggerPath).then(swaggerSchema => {
45+
const endpoints = getAllEndPoints(swaggerSchema);
46+
return schemaFromEndpoints(endpoints);
4247
});
4348
};
4449

50+
build.schemaFromEndpoints = schemaFromEndpoints;
51+
4552
function resolver(endpoint) {
4653
return (_, args, opts) => {
4754
const req = endpoint.request(args, {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "swagger-to-graphql",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "",
55
"main": "lib/index.js",
66
"scripts": {

0 commit comments

Comments
 (0)