Skip to content

Commit 9352877

Browse files
committed
close #4
1 parent 2406063 commit 9352877

File tree

10 files changed

+55
-25
lines changed

10 files changed

+55
-25
lines changed

lib/interface.d.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,16 @@ type refToUrl = (typeName: TypeRef) => string;
148148
/**
149149
* Introspection types
150150
*/
151-
type Introspection = {
152-
data: {
153-
__schema: Schema
154-
}
151+
type RawIntrospection = {
152+
__schema: Schema
155153
}
156154

155+
type WrappedIntrospection = {
156+
data: RawIntrospection
157+
};
158+
159+
type Introspection = RawIntrospection | WrappedIntrospection;
160+
157161
type Schema = {
158162
queryType: Description,
159163
mutationType: Description,

lib/schema-loader/http.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as Bluebird from 'bluebird';
22
import * as request from 'request';
33

44
import { Introspection, Schema, SchemaLoader } from '../interface';
5-
5+
import { getSchemaFromIntrospection } from './introspectionParser';
66
import { query as introspectionQuery } from '../utility';
77

88
export type THttpSchemaLoaderOptions = {
@@ -30,7 +30,7 @@ async function r(options: request.OptionsWithUrl) {
3030
'Unexpected response from "' + options.url + '": ' + body.slice(0, 10) + '...'
3131
));
3232

33-
return resolve(body.data.__schema);
33+
return resolve(getSchemaFromIntrospection(body));
3434
});
3535
});
3636
}
@@ -57,4 +57,4 @@ export const httpSchemaLoader: SchemaLoader = async function (options: THttpSche
5757
}, {});
5858

5959
return r(requestOptions);
60-
};
60+
};

lib/schema-loader/idl.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { SchemaLoader, Introspection } from '../interface';
22
import { query as introspectionQuery } from '../utility';
3+
import { getSchemaFromIntrospection } from './introspectionParser';
34
import { resolve } from 'path';
45
import { readFile } from '../utility/fs';
56
import { buildSchema, parse, execute } from 'graphql';
@@ -17,5 +18,5 @@ export const idlSchemaLoader: SchemaLoader = async function (options: TIDLSchema
1718
parse(introspectionQuery)
1819
) as Introspection;
1920

20-
return introspection.data.__schema;
21-
};
21+
return getSchemaFromIntrospection(introspection);
22+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Schema, Introspection, RawIntrospection, WrappedIntrospection } from '../interface';
2+
3+
function isRawIntrospection(arg: any): arg is RawIntrospection {
4+
return arg.__schema !== undefined;
5+
}
6+
7+
function isWrappedIntrospection(arg: any): arg is WrappedIntrospection {
8+
return arg.data !== undefined;
9+
}
10+
11+
export function getSchemaFromIntrospection(introspection: Introspection): Schema {
12+
if (isRawIntrospection(introspection)) {
13+
return introspection.__schema;
14+
} else if (isWrappedIntrospection(introspection)) {
15+
return introspection.data.__schema;
16+
} else {
17+
throw new Error('Could not parse schema');
18+
}
19+
}

lib/schema-loader/js.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Introspection, SchemaLoader } from '../interface';
2+
import { getSchemaFromIntrospection } from './introspectionParser';
23
import { buildSchema, execute, parse } from 'graphql';
34

45
import { query as introspectionQuery } from '../utility';
@@ -36,5 +37,5 @@ export const jsSchemaLoader: SchemaLoader = async function (options: TJsSchemaLo
3637
parse(introspectionQuery)
3738
) as Introspection;
3839

39-
return introspection.data.__schema;
40-
};
40+
return getSchemaFromIntrospection(introspection);
41+
};

lib/schema-loader/json.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { SchemaLoader, Introspection } from '../interface';
2+
import { getSchemaFromIntrospection } from './introspectionParser';
23
import { resolve } from 'path';
34

45
export type TJsonSchemaLoaderOptions = {
@@ -9,9 +10,9 @@ export const jsonSchemaLoader: SchemaLoader = function (options: TJsonSchemaLoad
910
try {
1011
const schemaPath = resolve(options.schemaFile);
1112
const introspection: Introspection = require(schemaPath);
12-
return Promise.resolve(introspection.data.__schema);
1313

14+
return Promise.resolve(getSchemaFromIntrospection(introspection))
1415
} catch (err) {
1516
return Promise.reject(err);
1617
}
17-
};
18+
};

plugins/__test__/navigation.directive.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Introspection } from '../../lib/interface';
2+
import {getSchemaFromIntrospection} from '../../lib/schema-loader/introspectionParser';
23
import NavigationDirectives from '../navigation.directive';
34

4-
const schema: Introspection = require('./empty.schema.json');
5+
const introspection: Introspection = require('./empty.schema.json');
56
const projectPackage: any = require('./projectPackage.json');
67

78
describe('pĺugins/navigation.directive#NavigationDirectives', () => {
89

9-
const plugin = new NavigationDirectives(schema.data.__schema, projectPackage, {});
10+
const plugin = new NavigationDirectives(getSchemaFromIntrospection(introspection), projectPackage, {});
1011

1112
test('plugin return navigation', () => {
1213
const navigations = plugin.getNavigations('Query');
@@ -22,4 +23,4 @@ describe('pĺugins/navigation.directive#NavigationDirectives', () => {
2223
}
2324
]);
2425
});
25-
});
26+
});

plugins/__test__/navigation.enum.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Introspection } from '../../lib/interface';
2+
import {getSchemaFromIntrospection} from '../../lib/schema-loader/introspectionParser';
23
import NavigationEnums from '../navigation.enum';
34

4-
const schema: Introspection = require('./empty.schema.json');
5+
const introspection: Introspection = require('./empty.schema.json');
56
const projectPackage: any = require('./projectPackage.json');
67

78
describe('pĺugins/navigation.directive#NavigationDirectives', () => {
89

9-
const plugin = new NavigationEnums(schema.data.__schema, projectPackage, {});
10+
const plugin = new NavigationEnums(getSchemaFromIntrospection(introspection), projectPackage, {});
1011

1112
test('plugin return navigation', () => {
1213
const navigations = plugin.getNavigations('Query');
@@ -21,4 +22,4 @@ describe('pĺugins/navigation.directive#NavigationDirectives', () => {
2122
}
2223
]);
2324
});
24-
});
25+
});

plugins/__test__/navigation.input.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Introspection } from '../../lib/interface';
2+
import {getSchemaFromIntrospection} from '../../lib/schema-loader/introspectionParser';
23
import NavigationInputs from '../navigation.input';
34

4-
const schema: Introspection = require('./empty.schema.json');
5+
const introspection: Introspection = require('./empty.schema.json');
56
const projectPackage: any = require('./projectPackage.json');
67

78
describe('pĺugins/navigation.directive#NavigationDirectives', () => {
89

9-
const plugin = new NavigationInputs(schema.data.__schema, projectPackage, {});
10+
const plugin = new NavigationInputs(getSchemaFromIntrospection(introspection), projectPackage, {});
1011

1112
test('plugin return navigation', () => {
1213
const navigations = plugin.getNavigations('Query');
@@ -20,4 +21,4 @@ describe('pĺugins/navigation.directive#NavigationDirectives', () => {
2021
}
2122
]);
2223
});
23-
});
24+
});
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { Introspection } from '../../lib/interface';
2+
import {getSchemaFromIntrospection} from '../../lib/schema-loader/introspectionParser';
23
import NavigationInterfaces from '../navigation.interface';
34

4-
const schema: Introspection = require('./empty.schema.json');
5+
const introspection: Introspection = require('./empty.schema.json');
56
const projectPackage: any = require('./projectPackage.json');
67

78
describe('pĺugins/navigation.interface#NavigationInterfaces', () => {
89

9-
const plugin = new NavigationInterfaces(schema.data.__schema, projectPackage, {});
10+
const plugin = new NavigationInterfaces(getSchemaFromIntrospection(introspection), projectPackage, {});
1011

1112
test('plugin return navigation', () => {
1213
const navigations = plugin.getNavigations('Query');
1314
expect(navigations).toBeInstanceOf(Array);
1415
expect(navigations).toEqual([]);
1516
});
16-
});
17+
});

0 commit comments

Comments
 (0)