-
Notifications
You must be signed in to change notification settings - Fork 27
/
index.ts
53 lines (49 loc) · 2 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import schema from './lib/schema'
import { graphql, GraphQLSchema } from 'graphql';
import { makeExecutableSchema } from 'graphql-tools';
import resolverMapBuilder from './lib/resolvers';
import { spotifyWebAPIClient } from './lib/client';
import { SpotifyClientConfiguration, SpotifyGraphQLInterface } from './lib/interfaces';
export function getSchema(spotifyClientConfiguration: SpotifyClientConfiguration): GraphQLSchema {
return makeExecutableSchema({
typeDefs: schema,
resolvers: resolverMapBuilder(
spotifyWebAPIClient(spotifyClientConfiguration)
),
});
}
// Export the main entry function for the library
// This factory takes a `spotifyClientConfiguration` object as parameter and return a `query` method
export function SpotifyGraphQLClient(spotifyClientConfiguration: SpotifyClientConfiguration): SpotifyGraphQLInterface {
let executableSchema:GraphQLSchema = getSchema(spotifyClientConfiguration);
return {
// This public method wrap the `graphql-js` method
// by passing schema in a calls
//
// requestString:
// A GraphQL language formatted string representing the requested operation.
// rootValue:
// The value provided as the first argument to resolver functions on the top
// level type (e.g. the query object type).
// variableValues:
// A mapping of variable name to runtime value to use for all variables
// defined in the requestString.
// operationName:
// The name of the operation to use if requestString contains multiple
// possible operations. Can be omitted if requestString contains only
// one operation.
query(
requestString: string,
rootValue?: any,
contextValue?: any,
variableValues?: any,
operationName?: string
): any {
let graphqlArgs = [executableSchema];
graphqlArgs = graphqlArgs.concat(Array.from(arguments));
return graphql.apply(graphql, graphqlArgs);
}
}
}
export * from './lib/decorators';
export * from './lib/interfaces';