@@ -4,7 +4,7 @@ import { Database } from '@sqlitecloud/drivers';
44
55const connStr = Bun . env . CONN_STR || ':memory:' ;
66
7- const db = new Database ( connStr )
7+ const db = new Database ( connStr ) ;
88
99const typeDefs = `#graphql
1010 # Comments in GraphQL strings (such as this one) start with the hash (#) symbol.
@@ -41,12 +41,26 @@ const typeDefs = `#graphql
4141 Name: String
4242 }
4343
44+ type Join {
45+ AlbumId: Int
46+ Title: String
47+ ArtistName: String
48+ }
49+
4450 type Query {
45- albums: [Album],
46- artists: [Artist],
47- tracks: [Track],
48- genres: [Genre],
51+ albums: [Album]
52+ artists: [Artist]
53+ tracks: [Track]
54+ genres: [Genre]
4955 mediaTypes: [MediaType]
56+ joins: [Join]
57+ artist(name: String): Artist
58+ albumsByArtist(artistId: Int): [Album]
59+ }
60+
61+ type Mutation {
62+ createArtist(name: String): Artist
63+ createAlbum(title: String, artistId: Int): Album
5064 }
5165` ;
5266
@@ -67,13 +81,42 @@ const resolvers = {
6781 mediaTypes : async ( ) => {
6882 return await db . sql `SELECT * FROM media_types` ;
6983 } ,
84+ artist : async ( _ : any , { name } : { name : string } ) => {
85+ const res = await db . sql `SELECT * FROM artists WHERE Name LIKE ${ name } ;` ;
86+ if ( res . length === 0 ) return null ;
87+ return res [ 0 ] ;
88+ } ,
89+ albumsByArtist : async ( _ : any , { artistId } : { artistId : number } ) => {
90+ return await db . sql `SELECT albums.AlbumId, albums.Title FROM albums INNER JOIN artists ON albums.ArtistId = artists.ArtistId WHERE artists.ArtistId = ${ artistId } ` ;
91+ } ,
92+ } ,
93+ Mutation : {
94+ createArtist : async ( _ : any , args : { name : string } ) => {
95+ const res =
96+ await db . sql `INSERT INTO artists (Name) VALUES (${ args . name } )` ;
97+ if ( res . changes === 0 ) return null ;
98+ return { ArtistId : res . lastID , Name : args . name } ;
99+ } ,
100+ createAlbum : async ( _ : any , args : { title : string ; artistId : number } ) => {
101+ const res =
102+ await db . sql `INSERT INTO albums (Title, ArtistId) VALUES (${ args . title } , ${ args . artistId } )` ;
103+ if ( res . changes === 0 ) return null ;
104+ return {
105+ AlbumId : res . lastID ,
106+ Title : args . title ,
107+ ArtistId : args . artistId ,
108+ } ;
109+ } ,
70110 } ,
71111} ;
72112
73- const server = new ApolloServer ( { typeDefs, resolvers } ) ;
113+ const server = new ApolloServer ( {
114+ typeDefs,
115+ resolvers,
116+ } ) ;
74117
75118const { url } = await startStandaloneServer ( server , {
76- listen : { port : 4000 } ,
119+ listen : { port : 4000 } ,
77120} ) ;
78-
79- console . log ( `🚀 Server ready at: ${ url } ` ) ;
121+
122+ console . log ( `🚀 Server ready at: ${ url } ` ) ;
0 commit comments