Skip to content

Commit eaf164a

Browse files
committed
Added and tested resolvers for join queries and mutations (i.e. inserts); authored by Jacob Prall
1 parent e0a59ca commit eaf164a

4 files changed

Lines changed: 1745 additions & 9 deletions

File tree

graphql-apollo/client/MusicExplorer/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ web-build/
1212
package-lock.json
1313
# macOS
1414
.DS_Store
15+
16+
# @generated expo-cli sync-2b81b286409207a5da26e14c78851eb30d8ccbdb
17+
# The following patterns were generated by expo-cli
18+
19+
expo-env.d.ts
20+
# @end expo-cli

graphql-apollo/server/bun.lockb

0 Bytes
Binary file not shown.

graphql-apollo/server/index.ts

Lines changed: 52 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Database } from '@sqlitecloud/drivers';
44

55
const connStr = Bun.env.CONN_STR || ':memory:';
66

7-
const db = new Database(connStr)
7+
const db = new Database(connStr);
88

99
const 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

75118
const { 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

Comments
 (0)