Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Readme

Tracks GraphQL API

Query some Spotify tracks I probably listened to via GraphQL.

GraphQL Yoga

Powered by GraphQL Yoga from The Guild is a batteries-included cross-platform GraphQL over HTTP spec-compliant GraphQL server powered by Envelop and GraphQL Tools focused on easy setup, performance and great developer experience.

Usage

query MyTracks { tracks { id artist title url image_url } }

See Track type for fields.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import { createSchema, createYoga } from "npm:graphql-yoga";
import { sqlite } from "https://esm.town/v/std/sqlite";
import { eq, sql } from "npm:drizzle-orm";
import { drizzle } from "npm:drizzle-orm/libsql";
import { integer, sqliteTable, text } from "npm:drizzle-orm/sqlite-core";
const db = drizzle(sqlite as any);
const tracksTable = sqliteTable("tracks", {
id: integer("id").primaryKey(),
track_id: text("track_id").notNull().unique(),
artist: text("artist"),
title: text("title"),
url: text("url"),
image_url: text("image_url"),
thumbnail_url: text("thumbnail_url"),
icon_url: text("icon_url"),
duration_ms: text("duration_ms"),
duration: text("duration"),
is_hidden: integer("is_hidden", { mode: "boolean" }),
});
const typeDefs = /* GraphQL */ `
type Track {
id: Int!
track_id: String!
artist: String!
title: String!
url: String!
image_url: String
thumbnail_url: String
icon_url: String
duration_ms: Int
duration: Float
is_hidden: Boolean
}
type Query {
hello: String
now: String
tracks: [Track!]!
}
`;
const resolvers = {
Query: {
hello: () => "Hello, Val Town!",
tracks: async () =>
{
return await db.select().from(tracksTable).orderBy(tracksTable.artist, tracksTable.title);
},
},
};
const defaultQuery = `#
# Welcome to Yoga GraphiQL running on Val town
#
# Yoga GraphiQL is an in-browser tool for writing, validating, and
# testing GraphQL queries.
#
# Try a GraphQL query:
#
query MyValQuery {
hello
tracks {
id
artist
title
url
image_url
}
}
`;
// Create a Yoga GraphQL schema
const schema = createSchema({
typeDefs,
resolvers,
});
// Create a Yoga GraphQL server
const yoga = createYoga({
schema,
graphqlEndpoint: "/",
graphiql: {
title: "Spotify Tracks Listened To",
defaultQuery,
},
});
export default yoga.fetch;
dthyresson-tracksapi.web.val.run
July 28, 2024