|
| 1 | +/*Copyright 2023 Oracle and/or its affiliates. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +import { |
| 17 | + GraphQLSchema, |
| 18 | + GraphQLObjectType, |
| 19 | + GraphQLString, |
| 20 | + GraphQLInt, |
| 21 | + GraphQLFloat, |
| 22 | + GraphQLList |
| 23 | + } from 'graphql'; |
| 24 | + import 'mle-js-fetch'; |
| 25 | + import oracledb from 'mle-js-oracledb'; |
| 26 | + |
| 27 | + const BASE_URL = "https://www.googleapis.com/books/v1/volumes?q="; |
| 28 | + |
| 29 | + const getBooks = async (SEARCH_URL) => { |
| 30 | + const conn = oracledb.defaultConnection(); |
| 31 | + conn.execute(` |
| 32 | + begin |
| 33 | + utl_http.set_wallet('file:/<absolute-path-to-your-wallet-directory>'); |
| 34 | + end; |
| 35 | + `); |
| 36 | + |
| 37 | + const response = await fetch(`${BASE_URL}${SEARCH_URL}`, { credentials: "include" }); |
| 38 | + const result = await response.json(); |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + const imageLinks = new GraphQLObjectType({ |
| 43 | + name: 'IMAGE_LINKS', |
| 44 | + fields: () => ({ |
| 45 | + thumbnail: { |
| 46 | + type: GraphQLString, |
| 47 | + }, |
| 48 | + }) |
| 49 | + }); |
| 50 | + |
| 51 | + const author = new GraphQLObjectType({ |
| 52 | + name: 'AUTHOR', |
| 53 | + fields: () => ({ |
| 54 | + name: { |
| 55 | + type: GraphQLString, |
| 56 | + resolve: (author) => author || "Unknown", |
| 57 | + }, |
| 58 | + }) |
| 59 | + }); |
| 60 | + |
| 61 | + const volumeInfo = new GraphQLObjectType({ |
| 62 | + name: 'VOLUME_INFO', |
| 63 | + fields: () => ({ |
| 64 | + title: { |
| 65 | + type: GraphQLString, |
| 66 | + }, |
| 67 | + subtitle: { |
| 68 | + type: GraphQLString, |
| 69 | + }, |
| 70 | + publisher: { |
| 71 | + type: GraphQLString, |
| 72 | + }, |
| 73 | + description: { |
| 74 | + type: GraphQLString, |
| 75 | + }, |
| 76 | + authors: { |
| 77 | + type: new GraphQLList(author), |
| 78 | + resolve: (volumeInfo) => volumeInfo.authors || [], |
| 79 | + }, |
| 80 | + publishedDate: { |
| 81 | + type: GraphQLString, |
| 82 | + }, |
| 83 | + imageLinks: { |
| 84 | + type: imageLinks, |
| 85 | + }, |
| 86 | + averageRating: { |
| 87 | + type: GraphQLFloat, |
| 88 | + }, |
| 89 | + pageCount: { |
| 90 | + type: GraphQLInt, |
| 91 | + }, |
| 92 | + language: { |
| 93 | + type: GraphQLString, |
| 94 | + } |
| 95 | + }) |
| 96 | + }); |
| 97 | + |
| 98 | + const book = new GraphQLObjectType({ |
| 99 | + name: 'BOOK', |
| 100 | + fields: () => ({ |
| 101 | + id: { |
| 102 | + type: GraphQLString, |
| 103 | + description: 'The id of the book.', |
| 104 | + }, |
| 105 | + kind: { |
| 106 | + type: GraphQLString, |
| 107 | + description: 'The kind of the book.', |
| 108 | + }, |
| 109 | + etag: { |
| 110 | + type: GraphQLString, |
| 111 | + description: 'The etag of the book.', |
| 112 | + }, |
| 113 | + volumeInfo: { |
| 114 | + type: volumeInfo, |
| 115 | + }, |
| 116 | + stock: { |
| 117 | + type: GraphQLInt, |
| 118 | + resolve: (book) => { |
| 119 | + const result = session.execute('select stock from inventory where id = :id', [book.id]); |
| 120 | + if (result.rows.length > 0) { |
| 121 | + return result.rows[0][0]; |
| 122 | + } else { |
| 123 | + return 0; |
| 124 | + } |
| 125 | + } |
| 126 | + }, |
| 127 | + }) |
| 128 | + }); |
| 129 | + |
| 130 | + const volumes = new GraphQLObjectType({ |
| 131 | + name: 'VOLUMES', |
| 132 | + fields: () => ({ |
| 133 | + kind: { |
| 134 | + type: GraphQLString, |
| 135 | + }, |
| 136 | + totalItems: { |
| 137 | + type: GraphQLInt, |
| 138 | + }, |
| 139 | + items: { |
| 140 | + type: new GraphQLList(book), |
| 141 | + }, |
| 142 | + }), |
| 143 | + }); |
| 144 | + |
| 145 | + const queryType = new GraphQLObjectType({ |
| 146 | + name: 'Query', |
| 147 | + fields: () => ({ |
| 148 | + library: { |
| 149 | + args: { |
| 150 | + research: { |
| 151 | + type: GraphQLString, |
| 152 | + }, |
| 153 | + }, |
| 154 | + type: volumes, |
| 155 | + resolve: async (_source, {research}) => await getBooks(research), |
| 156 | + }, |
| 157 | + }), |
| 158 | + }); |
| 159 | + |
| 160 | + export const schema = new GraphQLSchema({ |
| 161 | + query: queryType, |
| 162 | + types: [volumes, book, volumeInfo, author, imageLinks] |
| 163 | + }); |
0 commit comments