From 6952a74cafd97fd68d8ffea9cd636169958ab9f2 Mon Sep 17 00:00:00 2001 From: Salma Elbekraoui Date: Wed, 17 Apr 2024 14:04:50 +0000 Subject: [PATCH 1/2] Fix bbook-schema --- javascript/MLE-Demo/book-schema.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascript/MLE-Demo/book-schema.mjs b/javascript/MLE-Demo/book-schema.mjs index 28c90d74..c3e5c04b 100644 --- a/javascript/MLE-Demo/book-schema.mjs +++ b/javascript/MLE-Demo/book-schema.mjs @@ -118,7 +118,7 @@ import { resolve: (book) => { const result = session.execute('select stock from inventory where id = :id', [book.id]); if (result.rows.length > 0) { - return result.rows[0][0]; + return result.rows[0].STOCK; } else { return 0; } From a645f591bd56b0696a7b58b6b0d3bf6590ef15a5 Mon Sep 17 00:00:00 2001 From: Salma Elbekraoui Date: Wed, 12 Jun 2024 15:45:51 +0000 Subject: [PATCH 2/2] Change to use declarative types with @graphql-tools/schema --- javascript/MLE-Demo/README.md | 2 +- javascript/MLE-Demo/book-schema.mjs | 225 ++++++++++------------------ 2 files changed, 82 insertions(+), 145 deletions(-) diff --git a/javascript/MLE-Demo/README.md b/javascript/MLE-Demo/README.md index a540a805..89920d52 100644 --- a/javascript/MLE-Demo/README.md +++ b/javascript/MLE-Demo/README.md @@ -8,4 +8,4 @@ For more information, please refer to our [blog post](https://medium.com/@salma. ## Acknowledgements - Author/Contributors - Lucas Braun, Principal Program Manager, Salma Elbekraoui, MLE team intern -- Last Updated By/Date - Lucas Braun, Principal Program Manager, Salma Elbekraoui, MLE team intern, July 2023 +- Last Updated By/Date - Lucas Braun, Principal Program Manager, Salma Elbekraoui, MLE developer, June 2024 diff --git a/javascript/MLE-Demo/book-schema.mjs b/javascript/MLE-Demo/book-schema.mjs index c3e5c04b..e418c766 100644 --- a/javascript/MLE-Demo/book-schema.mjs +++ b/javascript/MLE-Demo/book-schema.mjs @@ -1,4 +1,4 @@ -/*Copyright 2023 Oracle and/or its affiliates. +/*Copyright 2024 Oracle and/or its affiliates. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -13,151 +13,88 @@ See the License for the specific language governing permissions and limitations under the License. */ -import { - GraphQLSchema, - GraphQLObjectType, - GraphQLString, - GraphQLInt, - GraphQLFloat, - GraphQLList - } from 'graphql'; - import 'mle-js-fetch'; - import oracledb from 'mle-js-oracledb'; - - const BASE_URL = "https://www.googleapis.com/books/v1/volumes?q="; - - const getBooks = async (SEARCH_URL) => { +import { makeExecutableSchema } from 'graphql-tools-schema'; +import 'mle-js-fetch'; +import oracledb from 'mle-js-oracledb'; + +const BASE_URL = "https://www.googleapis.com/books/v1/volumes?q="; + +const typeDefs = ` + type ImageLinks { + thumbnail: String + } + + type Author { + name: String + } + + type VolumeInfo { + title: String + subtitle: String + publisher: String + description: String + authors: [Author] + publishedDate: String + imageLinks: ImageLinks + averageRating: Float + pageCount: Int + language: String + } + + type Book { + id: String + kind: String + etag: String + volumeInfo: VolumeInfo + stock: Int + } + + type Volumes { + kind: String + totalItems: Int + items: [Book] + } + + type Query { + library(research: String): Volumes + } +`; + +const resolvers = { + Query: { + library: async (_, { research }) => { + return await getBooks(research); + } + }, + VolumeInfo: { + authors: (volumeInfo) => volumeInfo.authors || [], + }, + Book: { + stock: async (book) => { + const conn = oracledb.defaultConnection(); + const result = await conn.execute('SELECT stock FROM inventory WHERE id = :id', [book.id], { outFormat: oracledb.OUT_FORMAT_OBJECT }); + if (result.rows.length > 0) { + return result.rows[0].STOCK; + } else { + return 0; + } + } + }, +}; + +const getBooks = async (SEARCH_URL) => { const conn = oracledb.defaultConnection(); - conn.execute(` - begin - utl_http.set_wallet('file:/'); - end; + await conn.execute(` + begin + utl_http.set_wallet('file:/'); + end; `); - const response = await fetch(`${BASE_URL}${SEARCH_URL}`, { credentials: "include" }); const result = await response.json(); return result; - } - - const imageLinks = new GraphQLObjectType({ - name: 'IMAGE_LINKS', - fields: () => ({ - thumbnail: { - type: GraphQLString, - }, - }) - }); - - const author = new GraphQLObjectType({ - name: 'AUTHOR', - fields: () => ({ - name: { - type: GraphQLString, - resolve: (author) => author || "Unknown", - }, - }) - }); - - const volumeInfo = new GraphQLObjectType({ - name: 'VOLUME_INFO', - fields: () => ({ - title: { - type: GraphQLString, - }, - subtitle: { - type: GraphQLString, - }, - publisher: { - type: GraphQLString, - }, - description: { - type: GraphQLString, - }, - authors: { - type: new GraphQLList(author), - resolve: (volumeInfo) => volumeInfo.authors || [], - }, - publishedDate: { - type: GraphQLString, - }, - imageLinks: { - type: imageLinks, - }, - averageRating: { - type: GraphQLFloat, - }, - pageCount: { - type: GraphQLInt, - }, - language: { - type: GraphQLString, - } - }) - }); - - const book = new GraphQLObjectType({ - name: 'BOOK', - fields: () => ({ - id: { - type: GraphQLString, - description: 'The id of the book.', - }, - kind: { - type: GraphQLString, - description: 'The kind of the book.', - }, - etag: { - type: GraphQLString, - description: 'The etag of the book.', - }, - volumeInfo: { - type: volumeInfo, - }, - stock: { - type: GraphQLInt, - resolve: (book) => { - const result = session.execute('select stock from inventory where id = :id', [book.id]); - if (result.rows.length > 0) { - return result.rows[0].STOCK; - } else { - return 0; - } - } - }, - }) - }); - - const volumes = new GraphQLObjectType({ - name: 'VOLUMES', - fields: () => ({ - kind: { - type: GraphQLString, - }, - totalItems: { - type: GraphQLInt, - }, - items: { - type: new GraphQLList(book), - }, - }), - }); - - const queryType = new GraphQLObjectType({ - name: 'Query', - fields: () => ({ - library: { - args: { - research: { - type: GraphQLString, - }, - }, - type: volumes, - resolve: async (_source, {research}) => await getBooks(research), - }, - }), - }); - - export const schema = new GraphQLSchema({ - query: queryType, - types: [volumes, book, volumeInfo, author, imageLinks] - }); +}; + +export const schema = makeExecutableSchema({ + typeDefs, + resolvers +}); \ No newline at end of file