@@ -5,21 +5,90 @@ import { GraphQLModel, GrampsError } from '@gramps/gramps-express';
55 * https://ibm.biz/graphql-data-source-model
66 */
77
8- // TODO: change `YourDataSourceModel` to a descriptive name
9- export default class YourDataSourceModel extends GraphQLModel {
8+ const makeUrlSafe = str => encodeURIComponent ( str ) . replace ( '%20' , '+' ) ;
9+ const getQueryString = args =>
10+ Object . keys ( args )
11+ . map ( key => ( args [ key ] ? `${ key } =${ makeUrlSafe ( args [ key ] ) } ` : false ) )
12+ // Remove any arguments that were falsy.
13+ . filter ( pair => pair !== false )
14+ // Turn the array into a query string.
15+ . join ( '&' ) ;
16+
17+ export default class IMDBAPIModel extends GraphQLModel {
18+ /**
19+ * Searches for a movie by its title and (optionally) its release year.
20+ *
21+ * @see http://www.theimdbapi.org/
22+ * @param {String } args.title movie title to search for
23+ * @param {String? } args.year year the movie was released
24+ * @return {Promise } resolves with movie(s) matching the search
25+ */
26+ searchMoviesByTitle ( args ) {
27+ return this . connector
28+ . get ( `/find/movie?${ getQueryString ( args ) } ` )
29+ . catch ( res =>
30+ this . throwError ( res , {
31+ description : 'Unable to search movies' ,
32+ docsLink : 'https://github.com/gramps-express/data-source-imdbapi' ,
33+ } ) ,
34+ ) ;
35+ }
36+
37+ /**
38+ * Searches for a person by their name.
39+ *
40+ * A quirk of the IMDB API is that it seems to only return a single result for
41+ * searches. Bear that in mind when making searches.
42+ *
43+ * @see http://www.theimdbapi.org/
44+ * @param {String } name name to search for
45+ * @return {Promise } resolves with person matching the search
46+ */
47+ searchPersonByName ( name ) {
48+ return this . connector
49+ . get ( `/find/person?${ getQueryString ( { name } ) } ` )
50+ . catch ( res =>
51+ this . throwError ( res , {
52+ description : 'Unable to search people' ,
53+ docsLink : 'https://github.com/gramps-express/data-source-imdbapi' ,
54+ } ) ,
55+ ) ;
56+ }
57+
58+ /**
59+ * Retrieves a movie by its ID.
60+ *
61+ * @see http://www.theimdbapi.org/
62+ * @param {String } movieId the IMDB movie ID
63+ * @return {Promise } resolves with movie matching the ID
64+ */
65+ getMovieById ( movieId ) {
66+ return this . connector
67+ . get ( `/movie?${ getQueryString ( { movie_id : movieId } ) } ` )
68+ . catch ( res =>
69+ this . throwError ( res , {
70+ description : 'Unable to get movie by ID' ,
71+ docsLink : 'https://github.com/gramps-express/data-source-imdbapi' ,
72+ } ) ,
73+ ) ;
74+ }
75+
1076 /**
11- * Loads a thing by its ID
12- * @param {String } id the ID of the thing to load
13- * @return {Promise } resolves with the loaded user data
77+ * Retrieves a person by their IMDB ID.
78+ *
79+ * @see http://www.theimdbapi.org/
80+ * @param {String } personId the IMDB person ID
81+ * @return {Promise } resolves with person matching the ID
1482 */
15- getById ( id ) {
16- return this . connector . get ( `/data/${ id } ` ) . catch ( res =>
17- this . throwError ( res , {
18- description : 'This is an example call. Add your own!' ,
19- docsLink :
20- 'https://gramps-graphql.github.io/gramps-express/data-source/tutorial/' ,
21- } ) ,
22- ) ;
83+ getPersonById ( personId ) {
84+ return this . connector
85+ . get ( `/person?${ getQueryString ( { person_id : personId } ) } ` )
86+ . catch ( res =>
87+ this . throwError ( res , {
88+ description : 'Unable to get person by ID' ,
89+ docsLink : 'https://github.com/gramps-express/data-source-imdbapi' ,
90+ } ) ,
91+ ) ;
2392 }
2493
2594 /**
0 commit comments