Compare commits

..

No commits in common. "master" and "v7.3.0-0" have entirely different histories.

743 changed files with 18796 additions and 31229 deletions

10
.babelrc Normal file
View file

@ -0,0 +1,10 @@
{
"presets": ["es2015", "stage-2"],
"plugins": ["transform-runtime"],
"only": ["scripts/**", "src/**", "test/helpers/**"],
"env": {
"test": {
"plugins": ["istanbul"]
}
}
}

View file

@ -1,4 +1,3 @@
/.nyc_output /.nyc_output
/coverage /coverage
/lib /lib
!.eslintrc.cjs

View file

@ -1,45 +0,0 @@
module.exports = {
env: {
es2020: true,
node: true,
},
parserOptions: {
ecmaVersion: 2020,
sourceType: 'module',
},
extends: [
'eslint:recommended',
'plugin:promise/recommended',
'plugin:node/recommended',
'plugin:import/errors',
'plugin:import/warnings',
'plugin:prettier/recommended',
],
rules: {
'no-unused-vars': [
'error',
{ vars: 'all', args: 'none', ignoreRestSiblings: false },
],
'import/default': 'off',
'import/no-named-as-default': 'off',
'node/no-unsupported-features/es-syntax': [
'error',
{
ignores: ['dynamicImport', 'modules'],
},
],
'prettier/prettier': [
'error',
{
singleQuote: true,
},
],
'promise/always-return': 'off',
'promise/catch-or-return': [
'error',
{
allowThen: true,
},
],
},
};

18
.eslintrc.js Normal file
View file

@ -0,0 +1,18 @@
module.exports = {
extends: [
'standard',
'prettier',
'prettier/standard'
],
env: {
es6: true,
node: true
},
plugins: ['prettier'],
rules: {
'prettier/prettier': ['error', {
singleQuote: true,
semi: false
}]
}
}

View file

@ -1,3 +0,0 @@
/.nyc_output
/coverage
/lib

View file

@ -1,26 +1,26 @@
language: node_js language: node_js
node_js: node_js:
- '12' - '6'
- '14' - '7'
- '15' - '8'
# Use container-based Travis infrastructure. # Use container-based Travis infrastructure.
sudo: false sudo: false
branches: branches:
only: only:
- master - master
- '/^greenkeeper/.*$/' - "/^greenkeeper/.*$/"
env: env:
global: global:
- secure: fyw/6iyz4skp/sdAb9wr9Pq/72j4z2TiIZIJLT6B4zZZUKclA9ihHYx2Qf4IK7rHe+mvg0dSKaJi+zgrEx04fg2bP05dV1f52mmR71sE/FDRc9nkGgDl6/2AeXfXh0Raf8lVsYuBWclI2G0GPQeG1xskY7EBEnlKBdRmnljXPDNu1kwSrcLoGkoP1sIMRIxnMAHPXlOO3QKstiHM/HMa3A4SsQ4VGVLfrYqDJJ3L3XOkSbqyaSdWhELxSXAxSEzEOYi28kPeKtztdaU+BQtOSamuIMrEPwpM6+bFtTrf/SxWcybCk2sqn4sUgDVE8658cEIECR6NC1U/gWvDZSVHEetT2caHkhe1NRjxQmKrRfx88a+qZPeqbv6ydpvK8XAnaMtpwZtytoPZ7gw4IF0wxfkeBhK8dgBb4Lsdpnv90tbzcIyHgrHJiKBgJLlghBr9McFo4Fe3W06eD4rBTNX1LCmURQCm0V6mfCPUPup/68tAuscgysVW54XT51P4fk1iR3MJzCjbxaWbxwxWdCEeD2ptIkZwXDGydVe2qL4eI7NpYyTyhHc8vITlXYQnDCj9ztwjSiSf+4jJQiN7mbNcrRymnF22ICubnpj0SHBxEkQFJ+f/Wf3Ksxtl8t8DLTBCPtSzv7rxbdtnKRR1QRWgeYnhos1QHcNaoDgrlWCkOIg= - secure: fyw/6iyz4skp/sdAb9wr9Pq/72j4z2TiIZIJLT6B4zZZUKclA9ihHYx2Qf4IK7rHe+mvg0dSKaJi+zgrEx04fg2bP05dV1f52mmR71sE/FDRc9nkGgDl6/2AeXfXh0Raf8lVsYuBWclI2G0GPQeG1xskY7EBEnlKBdRmnljXPDNu1kwSrcLoGkoP1sIMRIxnMAHPXlOO3QKstiHM/HMa3A4SsQ4VGVLfrYqDJJ3L3XOkSbqyaSdWhELxSXAxSEzEOYi28kPeKtztdaU+BQtOSamuIMrEPwpM6+bFtTrf/SxWcybCk2sqn4sUgDVE8658cEIECR6NC1U/gWvDZSVHEetT2caHkhe1NRjxQmKrRfx88a+qZPeqbv6ydpvK8XAnaMtpwZtytoPZ7gw4IF0wxfkeBhK8dgBb4Lsdpnv90tbzcIyHgrHJiKBgJLlghBr9McFo4Fe3W06eD4rBTNX1LCmURQCm0V6mfCPUPup/68tAuscgysVW54XT51P4fk1iR3MJzCjbxaWbxwxWdCEeD2ptIkZwXDGydVe2qL4eI7NpYyTyhHc8vITlXYQnDCj9ztwjSiSf+4jJQiN7mbNcrRymnF22ICubnpj0SHBxEkQFJ+f/Wf3Ksxtl8t8DLTBCPtSzv7rxbdtnKRR1QRWgeYnhos1QHcNaoDgrlWCkOIg=
- secure: gS7EN+j/l9SDLjwM9YHCgOh6iPw0YTKvuQX9eXUI9YIEYoyhnBRQMfp77LJTuXdlucGgYj4v05YriLoxl4L7hmUmAKnDOVVEOUQqwYoAzg+h2GS3FQhoQRioxSAmTJc90nki0uhYAHW8FZ34+BjONCDSnew7r71TTU+UxRdRu4wOneFwXW1crPCNRtO5Ov/gkuiOWQaSPOoP6tzPOMP6FUueRQVqvf2GOoF3lBM8LnKeIq+m4H80DGsNjMtqdPJz9QQzAwO3VRNgsMAgr2wzVzjUnum7DfQC808AQDURlj9apg1HdYZDJH9bQyI/jIZ1gnHIcZ+kqqoq0l0QVNmITeMGrblFALU8xCFtGUyq70FGMjcmDGCanpveNOOhxjdqqfvvPKuqHEsfZtZkBJDovJdFtKmzf7DWb5xv8liPCzX26eBG/DPQgCEzyW0bl7IcB2LVMW/ObMRgixik2qiqYjN05pveWYsiO2iaiMo9ebm7FHXw/B0yfjM+CCGf7bjR9+RczXlN1x/gj+yjnYJ4K/nojSx0wms3QE7tJsXjmAMeguKUoZtaGGlF5yGbKtaZEVw/ZhYQDfLwwHQX2S5E424tYP/NAdi++CNCvyHJ7n0zel18FfsUNE1jIMaGb6aG6uOv6eOVI4+O09R71wxcYbQs40axdF+Y0A5f+CllNc8= - secure: gS7EN+j/l9SDLjwM9YHCgOh6iPw0YTKvuQX9eXUI9YIEYoyhnBRQMfp77LJTuXdlucGgYj4v05YriLoxl4L7hmUmAKnDOVVEOUQqwYoAzg+h2GS3FQhoQRioxSAmTJc90nki0uhYAHW8FZ34+BjONCDSnew7r71TTU+UxRdRu4wOneFwXW1crPCNRtO5Ov/gkuiOWQaSPOoP6tzPOMP6FUueRQVqvf2GOoF3lBM8LnKeIq+m4H80DGsNjMtqdPJz9QQzAwO3VRNgsMAgr2wzVzjUnum7DfQC808AQDURlj9apg1HdYZDJH9bQyI/jIZ1gnHIcZ+kqqoq0l0QVNmITeMGrblFALU8xCFtGUyq70FGMjcmDGCanpveNOOhxjdqqfvvPKuqHEsfZtZkBJDovJdFtKmzf7DWb5xv8liPCzX26eBG/DPQgCEzyW0bl7IcB2LVMW/ObMRgixik2qiqYjN05pveWYsiO2iaiMo9ebm7FHXw/B0yfjM+CCGf7bjR9+RczXlN1x/gj+yjnYJ4K/nojSx0wms3QE7tJsXjmAMeguKUoZtaGGlF5yGbKtaZEVw/ZhYQDfLwwHQX2S5E424tYP/NAdi++CNCvyHJ7n0zel18FfsUNE1jIMaGb6aG6uOv6eOVI4+O09R71wxcYbQs40axdF+Y0A5f+CllNc8=
script: script:
- yarn test - yarn test
after_success: after_success:
- $(yarn bin)/c8 report --reporter=text-lcov | $(yarn bin)/coveralls - $(yarn bin)/nyc report --reporter=text-lcov | $(yarn bin)/coveralls
- bash <(curl -s https://codecov.io/bash) - bash <(curl -s https://codecov.io/bash)

131
README.md
View file

@ -2,38 +2,27 @@
[![build status](https://img.shields.io/travis/exogen/graphbrainz/master.svg)](https://travis-ci.org/exogen/graphbrainz) [![build status](https://img.shields.io/travis/exogen/graphbrainz/master.svg)](https://travis-ci.org/exogen/graphbrainz)
[![coverage](https://img.shields.io/codecov/c/github/exogen/graphbrainz.svg)](https://codecov.io/gh/exogen/graphbrainz) [![coverage](https://img.shields.io/codecov/c/github/exogen/graphbrainz.svg)](https://codecov.io/gh/exogen/graphbrainz)
[![Greenkeeper badge](https://badges.greenkeeper.io/exogen/graphbrainz.svg)](https://greenkeeper.io/)
[![npm version](https://img.shields.io/npm/v/graphbrainz.svg)](https://www.npmjs.com/package/graphbrainz) [![npm version](https://img.shields.io/npm/v/graphbrainz.svg)](https://www.npmjs.com/package/graphbrainz)
[![license](https://img.shields.io/npm/l/graphbrainz.svg)](https://github.com/exogen/graphbrainz/blob/master/LICENSE) [![license](https://img.shields.io/npm/l/graphbrainz.svg)](https://github.com/exogen/graphbrainz/blob/master/LICENSE)
A [GraphQL][] schema, [Express][] server, and middleware for querying the A [GraphQL][] schema, [Express][] server, and middleware for querying the
[MusicBrainz][] API. It features an [extensible](./docs/extensions) schema to [MusicBrainz][] API. It features an [extensible](./docs/extensions) schema to
add integration with Discogs, Spotify, Last.fm, fanart.tv, and more! add integration with Last.fm, fanart.tv, and more!
**[Try out the live demo!][demo]** :bulb: Use the “Docs” sidebar, the
[schema][], or the [types][] docs to help construct your query.
## Install
Install with npm:
```sh ```sh
npm install graphbrainz --save npm install graphbrainz --save
``` ```
Install with Yarn: **[Try out the live demo!][demo]** :bulb: Use the “Docs” sidebar, the
[schema][], or the [types][] docs to help construct your query.
```sh
yarn add graphbrainz
```
_GraphBrainz is written and distributed as native ECMAScript modules
(ESM) and requires a compatible version of Node.js_
## Contents ## Contents
<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
- [Usage](#usage) - [Usage](#usage)
- [As a standalone server](#as-a-standalone-server) - [As a standalone server](#as-a-standalone-server)
- [As middleware](#as-middleware) - [As middleware](#as-middleware)
@ -55,8 +44,8 @@ middleware supplying a GraphQL endpoint.
### As a standalone server ### As a standalone server
Run the included `graphbrainz` executable to start the server. The server is Run the included `graphbrainz` executable to start the server. The server
configured using [environment variables](#environment-variables). is configured using [environment variables](#environment-variables).
```sh ```sh
$ graphbrainz $ graphbrainz
@ -81,7 +70,7 @@ an endpoint, or you just want more customization, use the middleware.
```js ```js
import express from 'express'; import express from 'express';
import { middleware as graphbrainz } from 'graphbrainz'; import graphbrainz from 'graphbrainz';
const app = express(); const app = express();
@ -100,12 +89,12 @@ app.listen(3000);
The `graphbrainz` middleware function accepts the following options: The `graphbrainz` middleware function accepts the following options:
- **`client`**: A custom API client instance to use. See the * **`client`**: A custom API client instance to use. See the
[client submodule](src/api/client.js) for help with creating a custom [client submodule](src/api/client.js) for help with creating a custom instance.
instance. You probably only need to do this if you want to adjust the rate You probably only need to do this if you want to adjust the rate limit and retry
limit and retry behavior. behavior.
- Any remaining options are passed along to the standard GraphQL middleware. See * Any remaining options are passed along to the standard GraphQL middleware.
the [express-graphql][] documentation for more information. See the [express-graphql][] documentation for more information.
### As a client ### As a client
@ -116,58 +105,52 @@ GraphBrainz resolvers expect, like so:
```js ```js
import { graphql } from 'graphql'; import { graphql } from 'graphql';
import { MusicBrainz, createContext, baseSchema } from 'graphbrainz'; import { MusicBrainz } from 'graphbrainz/lib/api';
import createContext from 'graphbrainz/lib/context';
import schema from 'graphbrainz/lib/schema';
const client = new MusicBrainz(); const client = new MusicBrainz();
const context = createContext({ client }); const context = createContext({ client })
graphql( graphql(schema, `
schema, {
` lookup {
{ releaseGroup(mbid: "99599db8-0e36-4a93-b0e8-350e9d7502a9") {
lookup { title
releaseGroup(mbid: "99599db8-0e36-4a93-b0e8-350e9d7502a9") {
title
}
} }
} }
`, }
null, `, null, context).then(result => {
context const { releaseGroup } = result.data.lookup;
) console.log(`The album title is “${releaseGroup.title}”.`);
.then((result) => { }).catch(err => {
const { releaseGroup } = result.data.lookup; console.error(err);
console.log(`The album title is “${releaseGroup.title}”.`); });
})
.catch((err) => {
console.error(err);
});
``` ```
### Environment Variables ### Environment Variables
- **`MUSICBRAINZ_BASE_URL`**: The base MusicBrainz API URL to use. Change this * **`MUSICBRAINZ_BASE_URL`**: The base MusicBrainz API URL to use. Change this
if you are running your own MusicBrainz mirror. Defaults to if you are running your own MusicBrainz mirror. Defaults to `http://musicbrainz.org/ws/2/`.
`http://musicbrainz.org/ws/2/`. * **`GRAPHBRAINZ_PATH`**: The URL route at which to expose the GraphQL endpoint,
- **`GRAPHBRAINZ_PATH`**: The URL route at which to expose the GraphQL endpoint,
if running the standalone server. Defaults to `/`. if running the standalone server. Defaults to `/`.
- **`GRAPHBRAINZ_CORS_ORIGIN`**: The value of the `origin` option to pass to the * **`GRAPHBRAINZ_CORS_ORIGIN`**: The value of the `origin` option to pass to the
[CORS][cors] middleware. Valid values are `true` to reflect the request [CORS][cors] middleware. Valid values are `true` to reflect the request
origin, a specific origin string to allow, `*` to allow all origins, and origin, a specific origin string to allow, `*` to allow all origins, and
`false` to disable CORS (the default). `false` to disable CORS (the default).
- **`GRAPHBRAINZ_CACHE_SIZE`**: The maximum number of REST API responses to * **`GRAPHBRAINZ_CACHE_SIZE`**: The maximum number of REST API responses to
cache. Increasing the cache size and TTL will greatly lower query execution cache. Increasing the cache size and TTL will greatly lower query execution
time for complex queries involving frequently accessed entities. Defaults to time for complex queries involving frequently accessed entities. Defaults to
`8192`. `8192`.
- **`GRAPHBRAINZ_CACHE_TTL`**: The maximum age of REST API responses in the * **`GRAPHBRAINZ_CACHE_TTL`**: The maximum age of REST API responses in the
cache, in milliseconds. Responses older than this will be disposed of (and cache, in milliseconds. Responses older than this will be disposed of (and
re-requested) the next time they are accessed. Defaults to `86400000` (one re-requested) the next time they are accessed. Defaults to `86400000` (one
day). day).
- **`GRAPHBRAINZ_GRAPHIQL`**: Set this to `true` if you want to force the * **`GRAPHBRAINZ_GRAPHIQL`**: Set this to `true` if you want to force the
[GraphiQL][] interface to be available even in production mode. [GraphiQL][] interface to be available even in production mode.
- **`GRAPHBRAINZ_EXTENSIONS`**: A JSON array of module paths to load as * **`GRAPHBRAINZ_EXTENSIONS`**: A JSON array of module paths to load as
[extensions](./docs/extensions). [extensions](./docs/extensions).
- **`PORT`**: Port number to use, if running the standalone server. * **`PORT`**: Port number to use, if running the standalone server.
When running the standalone server, [dotenv][] is used to load these variables When running the standalone server, [dotenv][] is used to load these variables
from a `.env` file, if one exists in the current working directory. This just from a `.env` file, if one exists in the current working directory. This just
@ -187,8 +170,7 @@ See the [debug][] package for more information.
## Example Queries ## Example Queries
Nirvana albums and each albums singles Nirvana albums and each albums singles ([try it](https://graphbrainz.herokuapp.com/?query=query%20NirvanaAlbumSingles%20%7B%0A%20%20lookup%20%7B%0A%20%20%20%20artist(mbid%3A%20%225b11f4ce-a62d-471e-81fc-a69a8278c7da%22)%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20releaseGroups(type%3A%20ALBUM)%20%7B%0A%20%20%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20title%0A%20%20%20%20%20%20%20%20%20%20%20%20firstReleaseDate%0A%20%20%20%20%20%20%20%20%20%20%20%20relationships%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20releaseGroups(type%3A%20%22single%20from%22)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20target%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20...%20on%20ReleaseGroup%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20title%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20firstReleaseDate%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A&operationName=NirvanaAlbumSingles)):
([try it](<https://graphbrainz.herokuapp.com/?query=query%20NirvanaAlbumSingles%20%7B%0A%20%20lookup%20%7B%0A%20%20%20%20artist(mbid%3A%20%225b11f4ce-a62d-471e-81fc-a69a8278c7da%22)%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20releaseGroups(type%3A%20ALBUM)%20%7B%0A%20%20%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20title%0A%20%20%20%20%20%20%20%20%20%20%20%20firstReleaseDate%0A%20%20%20%20%20%20%20%20%20%20%20%20relationships%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20releaseGroups(type%3A%20%22single%20from%22)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20target%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20...%20on%20ReleaseGroup%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20title%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20firstReleaseDate%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A&operationName=NirvanaAlbumSingles>)):
```graphql ```graphql
query NirvanaAlbumSingles { query NirvanaAlbumSingles {
@ -224,8 +206,7 @@ query NirvanaAlbumSingles {
### Pagination ### Pagination
The first five labels with “Apple” in the name The first five labels with “Apple” in the name ([try it](https://graphbrainz.herokuapp.com/?query=query%20AppleLabels%20%7B%0A%20%20search%20%7B%0A%20%20%20%20labels(query%3A%20%22Apple%22%2C%20first%3A%205)%20%7B%0A%20%20%20%20%20%20...labelResults%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%0Afragment%20labelResults%20on%20LabelConnection%20%7B%0A%20%20pageInfo%20%7B%0A%20%20%20%20endCursor%0A%20%20%7D%0A%20%20edges%20%7B%0A%20%20%20%20cursor%0A%20%20%20%20node%20%7B%0A%20%20%20%20%20%20mbid%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20type%0A%20%20%20%20%20%20area%20%7B%0A%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A&operationName=AppleLabels)):
([try it](<https://graphbrainz.herokuapp.com/?query=query%20AppleLabels%20%7B%0A%20%20search%20%7B%0A%20%20%20%20labels(query%3A%20%22Apple%22%2C%20first%3A%205)%20%7B%0A%20%20%20%20%20%20...labelResults%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%0Afragment%20labelResults%20on%20LabelConnection%20%7B%0A%20%20pageInfo%20%7B%0A%20%20%20%20endCursor%0A%20%20%7D%0A%20%20edges%20%7B%0A%20%20%20%20cursor%0A%20%20%20%20node%20%7B%0A%20%20%20%20%20%20mbid%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20type%0A%20%20%20%20%20%20area%20%7B%0A%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A&operationName=AppleLabels>)):
```graphql ```graphql
query AppleLabels { query AppleLabels {
@ -254,8 +235,7 @@ fragment labelResults on LabelConnection {
} }
``` ```
…and the next five, using the `endCursor` from the previous result …and the next five, using the `endCursor` from the previous result ([try it](https://graphbrainz.herokuapp.com/?query=query%20AppleLabels%20%7B%0A%20%20search%20%7B%0A%20%20%20%20labels(query%3A%20%22Apple%22%2C%20first%3A%205%2C%20after%3A%20%22YXJyYXljb25uZWN0aW9uOjQ%3D%22)%20%7B%0A%20%20%20%20%20%20...labelResults%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%0Afragment%20labelResults%20on%20LabelConnection%20%7B%0A%20%20pageInfo%20%7B%0A%20%20%20%20endCursor%0A%20%20%7D%0A%20%20edges%20%7B%0A%20%20%20%20cursor%0A%20%20%20%20node%20%7B%0A%20%20%20%20%20%20mbid%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20type%0A%20%20%20%20%20%20area%20%7B%0A%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A&operationName=AppleLabels)):
([try it](<https://graphbrainz.herokuapp.com/?query=query%20AppleLabels%20%7B%0A%20%20search%20%7B%0A%20%20%20%20labels(query%3A%20%22Apple%22%2C%20first%3A%205%2C%20after%3A%20%22YXJyYXljb25uZWN0aW9uOjQ%3D%22)%20%7B%0A%20%20%20%20%20%20...labelResults%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%0Afragment%20labelResults%20on%20LabelConnection%20%7B%0A%20%20pageInfo%20%7B%0A%20%20%20%20endCursor%0A%20%20%7D%0A%20%20edges%20%7B%0A%20%20%20%20cursor%0A%20%20%20%20node%20%7B%0A%20%20%20%20%20%20mbid%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20type%0A%20%20%20%20%20%20area%20%7B%0A%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A&operationName=AppleLabels>)):
```graphql ```graphql
query AppleLabels { query AppleLabels {
@ -268,7 +248,7 @@ query AppleLabels {
``` ```
Who the members of the band on an Apple Records release married, and when Who the members of the band on an Apple Records release married, and when
([try it](<https://graphbrainz.herokuapp.com/?query=query%20AppleRecordsMarriages%20%7B%0A%20%20search%20%7B%0A%20%20%20%20labels(query%3A%20%22Apple%20Records%22%2C%20first%3A%201)%20%7B%0A%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20disambiguation%0A%20%20%20%20%20%20%20%20%20%20country%0A%20%20%20%20%20%20%20%20%20%20releases(first%3A%201)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20title%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20date%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20artists%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20...bandMembers%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%0Afragment%20bandMembers%20on%20Artist%20%7B%0A%20%20relationships%20%7B%0A%20%20%20%20artists(direction%3A%20%22backward%22%2C%20type%3A%20%22member%20of%20band%22)%20%7B%0A%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20type%0A%20%20%20%20%20%20%20%20%20%20target%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20...%20on%20Artist%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20...marriages%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%0Afragment%20marriages%20on%20Artist%20%7B%0A%20%20relationships%20%7B%0A%20%20%20%20artists(type%3A%20%22married%22)%20%7B%0A%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20type%0A%20%20%20%20%20%20%20%20%20%20direction%0A%20%20%20%20%20%20%20%20%20%20begin%0A%20%20%20%20%20%20%20%20%20%20end%0A%20%20%20%20%20%20%20%20%20%20target%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20...%20on%20Artist%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A&operationName=AppleRecordsMarriages>)): ([try it](https://graphbrainz.herokuapp.com/?query=query%20AppleRecordsMarriages%20%7B%0A%20%20search%20%7B%0A%20%20%20%20labels(query%3A%20%22Apple%20Records%22%2C%20first%3A%201)%20%7B%0A%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20disambiguation%0A%20%20%20%20%20%20%20%20%20%20country%0A%20%20%20%20%20%20%20%20%20%20releases(first%3A%201)%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20title%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20date%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20artists%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20...bandMembers%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%0Afragment%20bandMembers%20on%20Artist%20%7B%0A%20%20relationships%20%7B%0A%20%20%20%20artists(direction%3A%20%22backward%22%2C%20type%3A%20%22member%20of%20band%22)%20%7B%0A%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20type%0A%20%20%20%20%20%20%20%20%20%20target%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20...%20on%20Artist%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20...marriages%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A%0Afragment%20marriages%20on%20Artist%20%7B%0A%20%20relationships%20%7B%0A%20%20%20%20artists(type%3A%20%22married%22)%20%7B%0A%20%20%20%20%20%20edges%20%7B%0A%20%20%20%20%20%20%20%20node%20%7B%0A%20%20%20%20%20%20%20%20%20%20type%0A%20%20%20%20%20%20%20%20%20%20direction%0A%20%20%20%20%20%20%20%20%20%20begin%0A%20%20%20%20%20%20%20%20%20%20end%0A%20%20%20%20%20%20%20%20%20%20target%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20...%20on%20Artist%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20name%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A&operationName=AppleRecordsMarriages)):
```graphql ```graphql
query AppleRecordsMarriages { query AppleRecordsMarriages {
@ -340,8 +320,7 @@ fragment marriages on Artist {
} }
``` ```
Images of Tom Petty provided by various extensions Images of Tom Petty provided by various extensions ([try it](https://graphbrainz.herokuapp.com/?query=query%20TomPettyImages%20%7B%0A%20%20lookup%20%7B%0A%20%20%20%20artist(mbid%3A%20%225ca3f318-d028-4151-ac73-78e2b2d6cdcc%22)%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20mediaWikiImages%20%7B%0A%20%20%20%20%20%20%20%20url%0A%20%20%20%20%20%20%20%20objectName%0A%20%20%20%20%20%20%20%20descriptionHTML%0A%20%20%20%20%20%20%20%20licenseShortName%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20fanArt%20%7B%0A%20%20%20%20%20%20%20%20thumbnails%20%7B%0A%20%20%20%20%20%20%20%20%20%20url%0A%20%20%20%20%20%20%20%20%20%20likeCount%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20theAudioDB%20%7B%0A%20%20%20%20%20%20%20%20logo%0A%20%20%20%20%20%20%20%20biography%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A&operationName=TomPettyImages)):
([try it](<https://graphbrainz.herokuapp.com/?query=query%20TomPettyImages%20%7B%0A%20%20lookup%20%7B%0A%20%20%20%20artist(mbid%3A%20%225ca3f318-d028-4151-ac73-78e2b2d6cdcc%22)%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%20%20mediaWikiImages%20%7B%0A%20%20%20%20%20%20%20%20url%0A%20%20%20%20%20%20%20%20objectName%0A%20%20%20%20%20%20%20%20descriptionHTML%0A%20%20%20%20%20%20%20%20licenseShortName%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20fanArt%20%7B%0A%20%20%20%20%20%20%20%20thumbnails%20%7B%0A%20%20%20%20%20%20%20%20%20%20url%0A%20%20%20%20%20%20%20%20%20%20likeCount%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20theAudioDB%20%7B%0A%20%20%20%20%20%20%20%20logo%0A%20%20%20%20%20%20%20%20biography%0A%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A&operationName=TomPettyImages>)):
```graphql ```graphql
query TomPettyImages { query TomPettyImages {
@ -373,8 +352,8 @@ You can find more example queries in the [schema tests][].
## Questions ## Questions
**Whats with the cumbersome `edges`/`node` nesting? Why `first`/`after` instead **Whats with the cumbersome `edges`/`node` nesting? Why `first`/`after`
of `limit`/`offset`? Why `mbid` instead of `id`?** instead of `limit`/`offset`? Why `mbid` instead of `id`?**
You can thank [Relay][] for that; these are properties of a Relay-compliant You can thank [Relay][] for that; these are properties of a Relay-compliant
schema. The schema was originally designed to be more user-friendly, but in the schema. The schema was originally designed to be more user-friendly, but in the
@ -408,9 +387,9 @@ query ChristmasAlbums {
Its likely that your query requires multiple round trips to the MusicBrainz Its likely that your query requires multiple round trips to the MusicBrainz
REST API, which is subject to [rate limiting][]. While the query resolver tries REST API, which is subject to [rate limiting][]. While the query resolver tries
very hard to fetch only the data necessary, and with the smallest number of API very hard to fetch only the data necessary, and with the smallest number of
requests, it is not 100% optimal (yet). Make sure you are only requesting the API requests, it is not 100% optimal (yet). Make sure you are only requesting
fields you need and a reasonable level of nested entities  unless you are the fields you need and a reasonable level of nested entities  unless you are
willing to wait. willing to wait.
You can also set up a [local MusicBrainz mirror][mirror] and configure You can also set up a [local MusicBrainz mirror][mirror] and configure
@ -428,19 +407,19 @@ third-party services. See the [Extensions](./docs/extensions) docs for more
info. info.
[demo]: https://graphbrainz.herokuapp.com/ [demo]: https://graphbrainz.herokuapp.com/
[express]: http://expressjs.com/ [Express]: http://expressjs.com/
[musicbrainz]: https://musicbrainz.org/ [MusicBrainz]: https://musicbrainz.org/
[graphql]: http://graphql.org/ [GraphQL]: http://graphql.org/
[express-graphql]: https://www.npmjs.com/package/express-graphql [express-graphql]: https://www.npmjs.com/package/express-graphql
[dotenv]: https://www.npmjs.com/package/dotenv [dotenv]: https://www.npmjs.com/package/dotenv
[debug]: https://www.npmjs.com/package/debug [debug]: https://www.npmjs.com/package/debug
[graphiql]: https://github.com/graphql/graphiql [GraphiQL]: https://github.com/graphql/graphiql
[graphql-js]: https://github.com/graphql/graphql-js [graphql-js]: https://github.com/graphql/graphql-js
[relay]: https://facebook.github.io/relay/ [Relay]: https://facebook.github.io/relay/
[schema]: docs/schema.md [schema]: docs/schema.md
[types]: docs/types.md [types]: docs/types.md
[rate limiting]: https://musicbrainz.org/doc/XML_Web_Service/Rate_Limiting [rate limiting]: https://musicbrainz.org/doc/XML_Web_Service/Rate_Limiting
[mirror]: https://musicbrainz.org/doc/MusicBrainz_Server/Setup [mirror]: https://musicbrainz.org/doc/MusicBrainz_Server/Setup
[aliases]: http://graphql.org/learn/queries/#aliases [aliases]: http://graphql.org/learn/queries/#aliases
[schema tests]: test/_schema.js [schema tests]: test/schema.js
[cors]: https://github.com/expressjs/cors [cors]: https://github.com/expressjs/cors

3
circle.yml Normal file
View file

@ -0,0 +1,3 @@
machine:
node:
version: 6.0.0

4
cli.js
View file

@ -1,4 +1,2 @@
#!/usr/bin/env node #!/usr/bin/env node
import { start } from './src/index.js'; require('./lib/index').start()
start();

View file

@ -6,14 +6,14 @@ Extensions can define new GraphQL types and use the `extend type` syntax to add
new fields to any existing GraphBrainz type, including the root query. new fields to any existing GraphBrainz type, including the root query.
Several extensions are included by default, and you can install any number of Several extensions are included by default, and you can install any number of
additional extensions from a package manager or additional extensions from a package manager or [write your own](#extension-api).
[write your own](#extension-api).
## Contents ## Contents
<!-- START doctoc generated TOC please keep comment here to allow auto update --> <!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --> <!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->
- [Loading Extensions](#loading-extensions) - [Loading Extensions](#loading-extensions)
- [Built-in Extensions](#built-in-extensions) - [Built-in Extensions](#built-in-extensions)
- [More Extensions](#more-extensions) - [More Extensions](#more-extensions)
@ -31,9 +31,9 @@ additional extensions from a package manager or
## Loading Extensions ## Loading Extensions
The extensions to load are specified using the `extensions` option to the The extensions to load are specified using the `extensions` option to the
exported `middleware()` function. Each extension must be an object conforming to exported `graphbrainz()` middleware function. Each extension must be an object
the [Extension API](#extension-api), or the path to a module to load via conforming to the [Extension API](#extension-api), or the path to a module to
`require()` that exports such an object. load via `require()` that exports such an object.
If you are running GraphBrainz as a standalone server, you may specify If you are running GraphBrainz as a standalone server, you may specify
extensions via the `GRAPHBRAINZ_EXTENSIONS` environment variable, which will be extensions via the `GRAPHBRAINZ_EXTENSIONS` environment variable, which will be
@ -50,14 +50,14 @@ or environment variables. Check the documentation for each extension you use.
The default extensions configuration looks like this: The default extensions configuration looks like this:
```js ```js
middleware({ graphbrainz({
extensions: [ extensions: [
'graphbrainz/extensions/cover-art-archive', 'graphbrainz/extensions/cover-art-archive',
'graphbrainz/extensions/fanart-tv', 'graphbrainz/extensions/fanart-tv',
'graphbrainz/extensions/mediawiki', 'graphbrainz/extensions/mediawiki',
'graphbrainz/extensions/the-audio-db', 'graphbrainz/extensions/the-audio-db'
], ]
}); })
``` ```
## Built-in Extensions ## Built-in Extensions
@ -65,36 +65,29 @@ middleware({
The following extensions are included with GraphBrainz and loaded by default. The following extensions are included with GraphBrainz and loaded by default.
See their respective documentation pages for schema info and config options. See their respective documentation pages for schema info and config options.
- [Cover Art Archive](./cover-art-archive.md): Retrieve cover art images for * [Cover Art Archive](./cover-art-archive.md): Retrieve cover art images for
releases from the Cover Art Archive. releases from the Cover Art Archive.
- [fanart.tv](./fanart-tv.md): Retrieve high quality artwork for artists, * [fanart.tv](./fanart-tv.md): Retrieve high quality artwork for artists,
releases, and labels from fanart.tv. releases, and labels from fanart.tv.
- [MediaWiki](./mediawiki.md): Retrieve information from MediaWiki image pages, * [MediaWiki](./mediawiki.md): Retrieve information from MediaWiki image pages,
like the actual image file URL and EXIF metadata. like the actual image file URL and EXIF metadata.
- [TheAudioDB](./the-audio-db.md): Retrieve images and information about * [TheAudioDB](./the-audio-db.md): Retrieve images and information about artists,
artists, releases, and recordings from TheAudioDB.com. releases, and recordings from TheAudioDB.com.
## More Extensions ## More Extensions
The following extensions are published separately, but can easily be added to The following extensions are published separately, but can easily be added to
GraphBrainz by installing them: GraphBrainz by installing them:
- [Last.fm](https://github.com/exogen/graphbrainz-extension-lastfm): Retrieve * [Last.fm](https://github.com/exogen/graphbrainz-extension-lastfm): Retrieve
artist, release, and recording information from artist, release, and recording information from [Last.fm](https://www.last.fm/).
[Last.fm](https://www.last.fm/).
- [Discogs](https://github.com/exogen/graphbrainz-extension-discogs): Retrieve
artist, label, release, and release group information from
[Discogs](https://www.discogs.com/).
- [Spotify](https://github.com/exogen/graphbrainz-extension-spotify): Retrieve
artist, release, and recording information from
[Spotify](https://www.spotify.com/).
## Extension API ## Extension API
The core idea behind extensions comes from the [schema stitching][] feature from The core idea behind extensions comes from the [schema stitching][] feature
[graphql-tools][], although GraphBrainz does not currently use the exact from [graphql-tools][], although GraphBrainz does not currently use the exact
technique documented there. Instead, we call `parse` and `extendSchema` from technique documented there. Instead, we call `parse` and `extendSchema` from
[GraphQL.js][], followed by [addResolversToSchema][]. [GraphQL.js][], followed by [addResolveFunctionsToSchema][].
Extensions must export an object shaped like so: Extensions must export an object shaped like so:
@ -104,9 +97,9 @@ type Extension = {
description?: string, description?: string,
extendContext?: (context: Context, options: Options) => Context, extendContext?: (context: Context, options: Options) => Context,
extendSchema?: extendSchema?:
| { schemas: Array<string | DocumentNode>, resolvers: ResolverMap } { schemas: Array<string | DocumentNode>, resolvers: ResolverMap } |
| ((schema: GraphQLSchema, options: Options) => GraphQLSchema), (schema: GraphQLSchema, options: Options) => GraphQLSchema
}; }
``` ```
### Properties ### Properties
@ -135,7 +128,7 @@ If it is an object, it should have a `schemas` array and a `resolvers` object.
Each schema must be a string (containing type definitions in GraphQL schema Each schema must be a string (containing type definitions in GraphQL schema
language) or a `DocumentNode` (if the type definitions have already been language) or a `DocumentNode` (if the type definitions have already been
parsed). The `resolvers` object should contain a mapping of type fields to new parsed). The `resolvers` object should contain a mapping of type fields to new
resolver functions for those fields. See [addResolversToSchema][]. resolver functions for those fields. See [addResolveFunctionsToSchema][].
If it is a function, it should accept `schema` and `options` arguments and If it is a function, it should accept `schema` and `options` arguments and
return a new schema. Use this if youd like to perform custom schema extension return a new schema. Use this if youd like to perform custom schema extension
@ -150,22 +143,20 @@ module.exports = {
name: 'Hello World', name: 'Hello World',
description: 'A simple example extension.', description: 'A simple example extension.',
extendSchema: { extendSchema: {
schemas: [ schemas: [`
`
extend type Query { extend type Query {
helloWorld: String! helloWorld: String!
} }
`, `],
],
resolvers: { resolvers: {
Query: { Query: {
helloWorld: { helloWorld: {
resolve: () => 'It worked!', resolve: () => 'It worked!'
}, }
}, }
}, }
}, }
}; }
``` ```
This will allow the following query to be made: This will allow the following query to be made:
@ -183,73 +174,65 @@ See the code for the [built-in extensions][] for more examples.
Extensions can load and resolve data in any manner they please, and you can Extensions can load and resolve data in any manner they please, and you can
write them in any way that conforms to the API. But if you want an extra feather write them in any way that conforms to the API. But if you want an extra feather
in your cap, there are a few guidelines you should follow in order to maintain in your cap, there are a few guidelines you should follow in order to maintain
consistency with GraphBrainz and the built-in extensions. Here are some tips for consistency with GraphBrainz and the built-in extensions. Here are some tips
writing a good extension: for writing a good extension:
- If you need to make HTTP requests, using a [Client][] subclass will get you * If you need to make HTTP requests, using a [Client][] subclass will get you
rate limiting, error handling, retrying, and a Promise-based API for free. rate limiting, error handling, and a Promise-based API for free.
- Default to following the rate limiting rules of any APIs you use. If there are * Default to following the rate limiting rules of any APIs you wrap. If there
no guidelines on rate limiting, consider playing nice anyway and limiting your are no guidelines on rate limiting, consider playing nice anyway and limiting
client to around 1 to 10 requests per second. your client to around 1 to 10 requests per second.
- Use a [DataLoader][dataloader] instance to batch and cache requests. Even if * Use a [DataLoader][dataloader] instance to batch and cache requests. Even if
the data source doesnt support batching, DataLoader will help by deduping the data source doesnt support batching, it will dedupe in-flight requests
in-flight requests for the same key, preventing unnecessary requests. for the same key, preventing extra requests before the response has been
- Use a configurable cache and make sure you arent caching everything cached.
* Use a configurable cache and make sure you arent caching everything
indefinitely by accident. The `cacheMap` option to DataLoader is a good place indefinitely by accident. The `cacheMap` option to DataLoader is a good place
to put it. to put it.
- Get as much configuration from environment variables as possible so that users * Get as much configuration from environment variables as possible, so that
can just run the standalone server instead of writing any code. If you need people can just run the standalone server instead of writing any code. If
more complex configuration, use a single field on the `options` object as a you need more complex configuration, use a single object on the `options`
namespace for your extensions options. object as a namespace for your extensions options.
- Dont hesitate to rename fields returned by third-party APIs when translating * Dont be afraid to rename fields returned by third-party APIs when translating
them to the GraphQL schema. Consistency with GraphQL conventions and the them to the GraphQL schema. Consistency with GraphQL conventions and the
GraphBrainz schema is more desirable than consistency with the original API GraphBrainz schema is more desirable than consistency with the original API
being used. Some general rules: being wrapped. Some general rules:
- Match type names to the service theyre coming from (e.g. many services use * Use camel case naming and capitalize acronyms (unless they are the only
the words “album” and “track” and the type names should reflect that), but
match scalar field names to their MusicBrainz equivalents when possible
(e.g. `name` for artists but `title` for releases and recordings).
- Use camel case naming and capitalize acronyms (unless they are the only
word), e.g. `id`, `url`, `artistID`, `pageURL`. word), e.g. `id`, `url`, `artistID`, `pageURL`.
- If its ambiguous whether a field refers to an object/list vs. a scalar * If its ambiguous whether a field refers to an object/list vs. a scalar
summary of an object/list, consider clarifying the field name, e.g. `user` summary of an object/list, consider clarifying the field name, e.g. `user`
`userID`, `members``memberCount`. `userID`, `members``memberCount`.
- Dont include fields that are already available in MusicBrainz (unless its * Dont include fields that are already available in MusicBrainz, only include
possible to retrieve an entity that isnt in MusicBrainz). Only include
whats relevant and useful. whats relevant and useful.
- Add descriptions for everything: types, fields, arguments, enum values, etc. * Add descriptions for everything: types, fields, arguments, enum values, etc.
 with Markdown links wherever theyd be helpful.  with Markdown links wherever theyd be helpful.
- When extending the built-in types, prefer adding a single object field that * When extending the built-in types, prefer adding a single object field that
serves as a namespace rather than adding many fields. That way its more serves as a namespace rather than adding many fields. That way its more
obvious that the data source isnt MusicBrainz itself, and youre less likely obvious that the data source isnt MusicBrainz itself, and youre less likely
to conflict with new MusicBrainz fields in the future. to conflict with new MusicBrainz fields in the future.
- Prefer using a [Relay][]-compliant schema for lists of objects that (1) have * Prefer using a [Relay][]-compliant schema for lists of objects that (1) have
their own IDs and (2) are likely to be paginated. Feel free to add a `nodes` their own IDs and (2) are likely to be paginated. Feel free to add a `nodes`
shortcut field to the Connection type (for users who want to skip over shortcut field to the Connection type (for users who want to skip over
`edges`). `edges`).
- If you publish your extension, consider prefixing the package name with * If you publish your extension, consider prefixing the package name with
`graphbrainz-extension-` and having the default export of its `main` entry `graphbrainz-extension-` and having the default export of its `main` entry
point be the extension object. That way, using it is as simple as adding the point be the extension object. That way, using it is as simple as adding the
package name to the list of extensions. package name to the list of extensions.
- Consider using [graphql-markdown][] to document the schema created by your * Consider using [graphql-markdown][] to document the schema created by your
extension; this will match how GraphBrainz itself is documented. You can use extension; this will match how GraphBrainz itself is documented. You can use
the [diffSchema][] function to document only the schema updates, see the [diffSchema][] function to document only the schema updates, see
[scripts/build-extension-docs.js][build-extension-docs] for how this is done [scripts/build-extension-docs.js][build-extension-docs] for how this is done
with the built-in extensions. with the built-in extensions.
[graphql-tools]: http://dev.apollodata.com/tools/graphql-tools/index.html [graphql-tools]: http://dev.apollodata.com/tools/graphql-tools/index.html
[schema stitching]: [schema stitching]: http://dev.apollodata.com/tools/graphql-tools/schema-stitching.html
http://dev.apollodata.com/tools/graphql-tools/schema-stitching.html [mergeSchemas]: http://dev.apollodata.com/tools/graphql-tools/schema-stitching.html#mergeSchemas
[mergeschemas]:
http://dev.apollodata.com/tools/graphql-tools/schema-stitching.html#mergeSchemas
[dataloader]: https://github.com/facebook/dataloader [dataloader]: https://github.com/facebook/dataloader
[built-in extensions]: ../../src/extensions [built-in extensions]: ../../src/extensions
[client]: ../../src/api/client.js [Client]: ../../src/api/client.js
[graphql-markdown]: https://github.com/exogen/graphql-markdown [graphql-markdown]: https://github.com/exogen/graphql-markdown
[diffschema]: [diffSchema]: https://github.com/exogen/graphql-markdown#diffschemaoldschema-object-newschema-object-options-object
https://github.com/exogen/graphql-markdown#diffschemaoldschema-object-newschema-object-options-object
[build-extension-docs]: ../../scripts/build-extension-docs.js [build-extension-docs]: ../../scripts/build-extension-docs.js
[relay]: https://facebook.github.io/relay/ [Relay]: https://facebook.github.io/relay/
[graphql.js]: http://graphql.org/graphql-js/ [GraphQL.js]: http://graphql.org/graphql-js/
[addresolverstoschema]: [addResolveFunctionsToSchema]: http://dev.apollodata.com/tools/graphql-tools/resolvers.html#addResolveFunctionsToSchema
http://dev.apollodata.com/tools/graphql-tools/resolvers.html#addResolversToSchema

View file

@ -136,7 +136,8 @@ A free-text comment left for the image.
#### CoverArtArchiveImageThumbnails #### CoverArtArchiveImageThumbnails
URLs for thumbnails of different sizes for a particular piece of cover art. URLs for thumbnails of different sizes for a particular piece of
cover art.
<table> <table>
<thead> <thead>
@ -192,14 +193,14 @@ as well as a summary of what artwork is available.
<td valign="top"><a href="../types.md#urlstring">URLString</a></td> <td valign="top"><a href="../types.md#urlstring">URLString</a></td>
<td> <td>
The URL of an image depicting the album cover or “main front” of the release, The URL of an image depicting the album cover or “main front” of the release,
i.e. the front of the packaging of the audio recording (or in the case of a i.e. the front of the packaging of the audio recording (or in the case of a
digital release, the image associated with it in a digital media store). digital release, the image associated with it in a digital media store).
In the MusicBrainz schema, this field is a Boolean value indicating the In the MusicBrainz schema, this field is a Boolean value indicating the
presence of a front image, whereas here the value is the URL for the image presence of a front image, whereas here the value is the URL for the image
itself if one exists. You can check for null if you just want to determine itself if one exists. You can check for null if you just want to determine
the presence of an image. the presence of an image.
</td> </td>
</tr> </tr>
@ -219,13 +220,13 @@ retrieved as well.
<td valign="top"><a href="../types.md#urlstring">URLString</a></td> <td valign="top"><a href="../types.md#urlstring">URLString</a></td>
<td> <td>
The URL of an image depicting the “main back” of the release, i.e. the back The URL of an image depicting the “main back” of the release, i.e. the back
of the packaging of the audio recording. of the packaging of the audio recording.
In the MusicBrainz schema, this field is a Boolean value indicating the In the MusicBrainz schema, this field is a Boolean value indicating the
presence of a back image, whereas here the value is the URL for the image presence of a back image, whereas here the value is the URL for the image
itself. You can check for null if you just want to determine the presence of itself. You can check for null if you just want to determine the presence of
an image. an image.
</td> </td>
</tr> </tr>

View file

@ -188,7 +188,7 @@ A review of the album.
</tr> </tr>
<tr> <tr>
<td colspan="2" valign="top"><strong>salesCount</strong></td> <td colspan="2" valign="top"><strong>salesCount</strong></td>
<td valign="top"><a href="../types.md#float">Float</a></td> <td valign="top"><a href="../types.md#int">Int</a></td>
<td> <td>
The worldwide sales figure. The worldwide sales figure.
@ -206,7 +206,7 @@ The albums rating as determined by user votes, out of 10.
</tr> </tr>
<tr> <tr>
<td colspan="2" valign="top"><strong>scoreVotes</strong></td> <td colspan="2" valign="top"><strong>scoreVotes</strong></td>
<td valign="top"><a href="../types.md#float">Float</a></td> <td valign="top"><a href="../types.md#int">Int</a></td>
<td> <td>
The number of users who voted to determine the albums score. The number of users who voted to determine the albums score.
@ -548,7 +548,7 @@ The size of the images to retrieve.
</tr> </tr>
<tr> <tr>
<td colspan="2" valign="top"><strong>viewCount</strong></td> <td colspan="2" valign="top"><strong>viewCount</strong></td>
<td valign="top"><a href="../types.md#float">Float</a></td> <td valign="top"><a href="../types.md#int">Int</a></td>
<td> <td>
The number of views the video has received at the given URL. This will rarely The number of views the video has received at the given URL. This will rarely
@ -558,7 +558,7 @@ be up to date, so use cautiously.
</tr> </tr>
<tr> <tr>
<td colspan="2" valign="top"><strong>likeCount</strong></td> <td colspan="2" valign="top"><strong>likeCount</strong></td>
<td valign="top"><a href="../types.md#float">Float</a></td> <td valign="top"><a href="../types.md#int">Int</a></td>
<td> <td>
The number of likes the video has received at the given URL. This will rarely The number of likes the video has received at the given URL. This will rarely
@ -568,7 +568,7 @@ be up to date, so use cautiously.
</tr> </tr>
<tr> <tr>
<td colspan="2" valign="top"><strong>dislikeCount</strong></td> <td colspan="2" valign="top"><strong>dislikeCount</strong></td>
<td valign="top"><a href="../types.md#float">Float</a></td> <td valign="top"><a href="../types.md#int">Int</a></td>
<td> <td>
The number of dislikes the video has received at the given URL. This will The number of dislikes the video has received at the given URL. This will
@ -578,7 +578,7 @@ rarely be up to date, so use cautiously.
</tr> </tr>
<tr> <tr>
<td colspan="2" valign="top"><strong>commentCount</strong></td> <td colspan="2" valign="top"><strong>commentCount</strong></td>
<td valign="top"><a href="../types.md#float">Float</a></td> <td valign="top"><a href="../types.md#int">Int</a></td>
<td> <td>
The number of comments the video has received at the given URL. This will The number of comments the video has received at the given URL. This will
@ -678,7 +678,7 @@ The tracks rating as determined by user votes, out of 10.
</tr> </tr>
<tr> <tr>
<td colspan="2" valign="top"><strong>scoreVotes</strong></td> <td colspan="2" valign="top"><strong>scoreVotes</strong></td>
<td valign="top"><a href="../types.md#float">Float</a></td> <td valign="top"><a href="../types.md#int">Int</a></td>
<td> <td>
The number of users who voted to determine the albums score. The number of users who voted to determine the albums score.

File diff suppressed because it is too large Load diff

View file

@ -62,7 +62,6 @@ or the schemas provided by the [built-in extensions](extensions).
* [Tag](#tag) * [Tag](#tag)
* [TagConnection](#tagconnection) * [TagConnection](#tagconnection)
* [TagEdge](#tagedge) * [TagEdge](#tagedge)
* [Track](#track)
* [URL](#url) * [URL](#url)
* [Work](#work) * [Work](#work)
* [WorkConnection](#workconnection) * [WorkConnection](#workconnection)
@ -95,7 +94,7 @@ or the schemas provided by the [built-in extensions](extensions).
</details> </details>
## Query ## Query
The query root, from which multiple types of MusicBrainz The query root, from which multiple types of MusicBrainz
requests can be made. requests can be made.
@ -318,16 +317,6 @@ alternate names or misspellings.
[ISO 3166 codes](https://en.wikipedia.org/wiki/ISO_3166) are [ISO 3166 codes](https://en.wikipedia.org/wiki/ISO_3166) are
the codes assigned by ISO to countries and subdivisions. the codes assigned by ISO to countries and subdivisions.
</td>
</tr>
<tr>
<td colspan="2" align="right" valign="top">standard</td>
<td valign="top"><a href="#string">String</a></td>
<td>
Specify the particular ISO standard codes to retrieve.
Available ISO standards are 3166-1, 3166-2, and 3166-3.
</td> </td>
</tr> </tr>
<tr> <tr>
@ -3545,15 +3534,6 @@ The number of audio tracks on this medium.
A list of physical discs and their disc IDs for this medium. A list of physical discs and their disc IDs for this medium.
</td>
</tr>
<tr>
<td colspan="2" valign="top"><strong>tracks</strong></td>
<td valign="top">[<a href="#track">Track</a>]</td>
<td>
The list of tracks on the given media.
</td> </td>
</tr> </tr>
</tbody> </tbody>
@ -6729,81 +6709,6 @@ these results were found through a search.
</tbody> </tbody>
</table> </table>
### Track
A track is the way a recording is represented on a particular
release (or, more exactly, on a particular medium). Every track has a title
(see the guidelines for titles) and is credited to one or more artists.
<table>
<thead>
<tr>
<th align="left">Field</th>
<th align="right">Argument</th>
<th align="left">Type</th>
<th align="left">Description</th>
</tr>
</thead>
<tbody>
<tr>
<td colspan="2" valign="top"><strong>mbid</strong></td>
<td valign="top"><a href="#mbid">MBID</a>!</td>
<td>
The MBID of the entity.
</td>
</tr>
<tr>
<td colspan="2" valign="top"><strong>title</strong></td>
<td valign="top"><a href="#string">String</a></td>
<td>
The official title of the entity.
</td>
</tr>
<tr>
<td colspan="2" valign="top"><strong>position</strong></td>
<td valign="top"><a href="#int">Int</a></td>
<td>
The tracks position on the overall release (including all
tracks from all discs).
</td>
</tr>
<tr>
<td colspan="2" valign="top"><strong>number</strong></td>
<td valign="top"><a href="#string">String</a></td>
<td>
The track number, which may include information about the
disc or side it appears on, e.g. “A1” or “B3”.
</td>
</tr>
<tr>
<td colspan="2" valign="top"><strong>length</strong></td>
<td valign="top"><a href="#duration">Duration</a></td>
<td>
The length of the track.
</td>
</tr>
<tr>
<td colspan="2" valign="top"><strong>recording</strong></td>
<td valign="top"><a href="#recording">Recording</a></td>
<td>
The recording that appears on the track.
</td>
</tr>
</tbody>
</table>
### URL ### URL
A [URL](https://musicbrainz.org/doc/URL) pointing to a resource A [URL](https://musicbrainz.org/doc/URL) pointing to a resource
@ -7405,7 +7310,7 @@ A length of time, in milliseconds.
### Float ### Float
The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point). The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point).
### ID ### ID
@ -7444,7 +7349,7 @@ compositions.
### Int ### Int
The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1. The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.
### Locale ### Locale

View file

@ -1 +1 @@
export { default } from '../src/extensions/cover-art-archive/index.js'; module.exports = require('../lib/extensions/cover-art-archive')

View file

@ -1 +1 @@
export { default } from '../src/extensions/fanart-tv/index.js'; module.exports = require('../lib/extensions/fanart-tv')

View file

@ -1 +1 @@
export { default } from '../src/extensions/mediawiki/index.js'; module.exports = require('../lib/extensions/mediawiki')

View file

@ -1 +1 @@
export { default } from '../src/extensions/the-audio-db/index.js'; module.exports = require('../lib/extensions/the-audio-db')

View file

@ -1,7 +1,54 @@
{ {
"name": "graphbrainz", "name": "graphbrainz",
"version": "9.0.0", "version": "7.3.0-0",
"description": "A GraphQL schema, Express server, and middleware for querying the MusicBrainz.", "description": "An Express server and middleware for querying the MusicBrainz API using GraphQL.",
"main": "lib/index.js",
"bin": "cli.js",
"files": [
"extensions",
"lib",
"scripts",
"src",
".babelrc",
"cli.js",
"Procfile",
"schema.json",
"yarn.lock"
],
"engines": {
"node": ">=6.0.0",
"npm": ">=3.8.0"
},
"scripts": {
"build": "npm run build:lib && npm run update-schema && npm run build:docs",
"build:docs": "npm run build:docs:readme && npm run build:docs:schema && npm run build:docs:types && npm run build:docs:extensions",
"build:docs:extensions": "babel-node scripts/build-extension-docs.js",
"build:docs:readme": "doctoc --notitle README.md docs/extensions/README.md",
"build:docs:schema": "printf '# GraphQL Schema\\n\\n%s\n' \"$(npm run -s print-schema:md)\" > docs/schema.md",
"build:docs:types": "graphql-markdown ./src/schema.js --require babel-register --no-title --update-file docs/types.md",
"build:lib": "babel --out-dir lib src",
"clean": "npm run clean:lib",
"clean:lib": "rm -rf lib",
"deploy": "./scripts/deploy.sh",
"format": "npm run lint:fix",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"postinstall": "postinstall-build lib --script build:lib",
"prepublish": "npm run clean:lib && npm run build:lib",
"preversion": "npm run update-schema && npm run build:docs && git add schema.json docs",
"print-schema": "babel-node scripts/print-schema.js",
"print-schema:json": "npm run print-schema -- --json",
"print-schema:md": "printf '```graphql\\n%s\\n```' \"$(npm run -s print-schema)\"",
"start": "node lib/index.js",
"start:dev": "nodemon --exec babel-node src/index.js",
"test": "npm run lint && npm run test:coverage",
"test:coverage": "cross-env NODE_ENV=test nyc npm run test:only",
"test:only": "cross-env VCR_MODE=playback ava",
"test:record": "cross-env VCR_MODE=record ava",
"test:record-new": "cross-env VCR_MODE=cache ava --serial",
"test:watch": "npm run test:only -- --watch",
"update-schema": "npm run -s print-schema:json > schema.json"
},
"keywords": [ "keywords": [
"musicbrainz", "musicbrainz",
"graphql", "graphql",
@ -14,107 +61,83 @@
"author": { "author": {
"name": "Brian Beck", "name": "Brian Beck",
"email": "exogen@gmail.com", "email": "exogen@gmail.com",
"url": "https://brianbeck.com/" "url": "http://brianbeck.com/"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
"url": "https://github.com/exogen/graphbrainz.git" "url": "https://github.com/exogen/graphbrainz.git"
}, },
"license": "MIT", "license": "MIT",
"engines": { "dependencies": {
"node": ">=12.18.0", "babel-runtime": "^6.25.0",
"npm": ">=6.0.0" "compression": "^1.7.0",
"cors": "^2.8.4",
"dashify": "^0.2.2",
"dataloader": "^1.3.0",
"debug": "^3.0.0",
"dotenv": "^4.0.0",
"es6-error": "^4.0.2",
"express": "^4.16.2",
"express-graphql": "^0.6.7",
"graphql": "^0.11.7",
"graphql-relay": "^0.5.2",
"graphql-tools": "^2.7.2",
"lru-cache": "^4.1.1",
"pascalcase": "^0.1.1",
"postinstall-build": "^5.0.1",
"qs": "^6.5.0",
"request": "^2.81.0",
"retry": "^0.10.1"
}, },
"type": "module", "devDependencies": {
"main": "./src/index.js", "ava": "^0.23.0",
"exports": { "babel-cli": "^6.24.1",
".": "./src/index.js", "babel-eslint": "^8.0.2",
"./extensions/cover-art-archive": "./extensions/cover-art-archive.js", "babel-plugin-istanbul": "^4.1.4",
"./extensions/fanart-tv": "./extensions/fanart-tv.js", "babel-plugin-transform-runtime": "^6.23.0",
"./extensions/mediawiki": "./extensions/mediawiki.js", "babel-preset-es2015": "^6.24.1",
"./extensions/the-audio-db": "./extensions/the-audio-db.js", "babel-preset-stage-2": "^6.24.1",
"./package.json": "./package.json", "babel-register": "^6.24.1",
"./schema.json": "./schema.json" "coveralls": "^3.0.0",
"cross-env": "^5.1.1",
"doctoc": "^1.3.0",
"eslint": "^4.11.0",
"eslint-config-prettier": "^2.7.0",
"eslint-config-standard": "^10.2.1",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-markdown": "^1.0.0-beta.6",
"eslint-plugin-node": "^5.2.1",
"eslint-plugin-prettier": "^2.3.1",
"eslint-plugin-promise": "^3.6.0",
"eslint-plugin-standard": "^3.0.1",
"graphql-markdown": "^3.2.0",
"nodemon": "^1.11.0",
"nyc": "^11.3.0",
"prettier": "prettier/prettier#4b070c3",
"rimraf": "^2.6.1",
"sepia": "^2.0.2",
"sinon": "^4.1.2"
}, },
"bin": "cli.js", "standard": {
"files": [ "parser": "babel-eslint"
"extensions",
"src",
"cli.js",
"schema.json"
],
"scripts": {
"build": "npm run update-schema && npm run build:docs",
"build:docs": "npm run build:docs:readme && npm run build:docs:schema && npm run build:docs:types && npm run build:docs:extensions",
"build:docs:extensions": "node scripts/build-extension-docs.js",
"build:docs:readme": "doctoc --notitle README.md docs/extensions/README.md",
"build:docs:schema": "printf '# GraphQL Schema\\n\\n%s\n' \"$(npm run -s print-schema:md)\" > docs/schema.md",
"build:docs:types": "graphql-markdown ./schema.json --no-title --update-file docs/types.md",
"deploy": "./scripts/deploy.sh",
"format": "npm run lint:fix",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"preversion": "npm run update-schema && npm run build:docs && git add schema.json docs",
"print-schema": "node scripts/print-schema.js",
"print-schema:json": "npm run print-schema -- --json",
"print-schema:md": "printf '```graphql\\n%s\\n```' \"$(npm run -s print-schema)\"",
"start": "node cli.js",
"start:dev": "nodemon cli.js",
"test": "npm run lint && npm run test:coverage",
"test:coverage": "c8 --all npm run test:only",
"test:only": "cross-env NOCK_MODE=play ava",
"test:record": "cross-env NOCK_MODE=record ava --concurrency=1 --timeout=1m",
"test:record-new": "cross-env NOCK_MODE=cache ava --concurrency=1 --timeout=1m",
"test:watch": "npm run test:only -- --watch",
"update-schema": "npm run -s print-schema:json > schema.json"
}, },
"ava": { "ava": {
"require": [ "require": [
"dotenv/config" "dotenv/config",
"babel-register"
] ]
}, },
"ava-nock": { "nyc": {
"fixtureDir": "fixtures", "include": [
"pathFilter": [ "src/**"
"(([?&]api_key=)(\\w+))|((/json/)(\\w+)(/[\\w-]+-mb\\.php))", ],
"$2$5*$7" "reporter": [
] "lcov",
}, "text"
"dependencies": { ],
"@graphql-tools/schema": "^7.1.3", "all": true,
"compression": "^1.7.3", "cache": true,
"cors": "^2.8.4", "sourceMap": false,
"dashify": "^2.0.0", "instrument": false
"dataloader": "^2.0.0",
"debug": "^4.3.1",
"dotenv": "^8.2.0",
"es6-error": "^4.1.1",
"express": "^4.16.3",
"express-graphql": "^0.12.0",
"got": "^11.8.2",
"graphql": "^15.5.0",
"graphql-relay": "^0.6.0",
"lru-cache": "^6.0.0",
"pascalcase": "^1.0.0",
"read-pkg-up": "^8.0.0"
},
"devDependencies": {
"ava": "^3.15.0",
"ava-nock": "^2.1.0",
"c8": "^7.7.1",
"coveralls": "^3.0.2",
"cross-env": "^7.0.3",
"doctoc": "^2.0.0",
"eslint": "^7.24.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-import": "^2.13.0",
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-promise": "^5.1.0",
"graphql-markdown": "^6.0.0",
"nodemon": "^2.0.7",
"prettier": "^2.2.1",
"rimraf": "^3.0.2",
"sinon": "^10.0.0"
} }
} }

View file

@ -476,6 +476,16 @@
"enumValues": null, "enumValues": null,
"possibleTypes": null "possibleTypes": null
}, },
{
"kind": "SCALAR",
"name": "MBID",
"description": "The MBID scalar represents MusicBrainz identifiers, which are\n36-character UUIDs.",
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{ {
"kind": "OBJECT", "kind": "OBJECT",
"name": "Area", "name": "Area",
@ -568,18 +578,7 @@
{ {
"name": "isoCodes", "name": "isoCodes",
"description": "[ISO 3166 codes](https://en.wikipedia.org/wiki/ISO_3166) are\nthe codes assigned by ISO to countries and subdivisions.", "description": "[ISO 3166 codes](https://en.wikipedia.org/wiki/ISO_3166) are\nthe codes assigned by ISO to countries and subdivisions.",
"args": [ "args": [],
{
"name": "standard",
"description": "Specify the particular ISO standard codes to retrieve.\nAvailable ISO standards are 3166-1, 3166-2, and 3166-3.",
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"defaultValue": "\"3166-1\""
}
],
"type": { "type": {
"kind": "LIST", "kind": "LIST",
"name": null, "name": null,
@ -927,7 +926,7 @@
} }
], ],
"inputFields": null, "inputFields": null,
"interfaces": [], "interfaces": null,
"enumValues": null, "enumValues": null,
"possibleTypes": [ "possibleTypes": [
{ {
@ -1035,7 +1034,7 @@
} }
], ],
"inputFields": null, "inputFields": null,
"interfaces": [], "interfaces": null,
"enumValues": null, "enumValues": null,
"possibleTypes": [ "possibleTypes": [
{ {
@ -1058,11 +1057,6 @@
"name": "Release", "name": "Release",
"ofType": null "ofType": null
}, },
{
"kind": "OBJECT",
"name": "Track",
"ofType": null
},
{ {
"kind": "OBJECT", "kind": "OBJECT",
"name": "Label", "name": "Label",
@ -1110,16 +1104,6 @@
} }
] ]
}, },
{
"kind": "SCALAR",
"name": "MBID",
"description": "The MBID scalar represents MusicBrainz identifiers, which are\n36-character UUIDs.",
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{ {
"kind": "SCALAR", "kind": "SCALAR",
"name": "String", "name": "String",
@ -1233,6 +1217,16 @@
"enumValues": null, "enumValues": null,
"possibleTypes": null "possibleTypes": null
}, },
{
"kind": "SCALAR",
"name": "Int",
"description": "The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1. ",
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{ {
"kind": "OBJECT", "kind": "OBJECT",
"name": "ArtistConnection", "name": "ArtistConnection",
@ -2549,13 +2543,145 @@
"possibleTypes": null "possibleTypes": null
}, },
{ {
"kind": "SCALAR", "kind": "ENUM",
"name": "Int", "name": "ReleaseGroupType",
"description": "The `Int` scalar type represents non-fractional signed whole numeric values. Int can represent values between -(2^31) and 2^31 - 1.", "description": "A type used to describe release groups, e.g. album, single, EP,\netc.",
"fields": null, "fields": null,
"inputFields": null, "inputFields": null,
"interfaces": null, "interfaces": null,
"enumValues": null, "enumValues": [
{
"name": "ALBUM",
"description": "An album, perhaps better defined as a “Long Play” (LP)\nrelease, generally consists of previously unreleased material (unless this type\nis combined with secondary types which change that, such as “Compilation”). This\nincludes album re-issues, with or without bonus tracks.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "SINGLE",
"description": "A single typically has one main song and possibly a handful\nof additional tracks or remixes of the main track. A single is usually named\nafter its main song.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "EP",
"description": "An EP is a so-called “Extended Play” release and often\ncontains the letters EP in the title. Generally an EP will be shorter than a\nfull length release (an LP or “Long Play”) and the tracks are usually exclusive\nto the EP, in other words the tracks dont come from a previously issued\nrelease. EP is fairly difficult to define; usually it should only be assumed\nthat a release is an EP if the artist defines it as such.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "OTHER",
"description": "Any release that does not fit any of the other categories.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "BROADCAST",
"description": "An episodic release that was originally broadcast via radio,\ntelevision, or the Internet, including podcasts.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "COMPILATION",
"description": "A compilation is a collection of previously released tracks\nby one or more artists.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "SOUNDTRACK",
"description": "A soundtrack is the musical score to a movie, TV series,\nstage show, computer game, etc.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "SPOKENWORD",
"description": "A non-music spoken word release.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "INTERVIEW",
"description": "An interview release contains an interview, generally with\nan artist.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "AUDIOBOOK",
"description": "An audiobook is a book read by a narrator without music.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "LIVE",
"description": "A release that was recorded live.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "REMIX",
"description": "A release that was (re)mixed from previously released\nmaterial.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "DJMIX",
"description": "A DJ-mix is a sequence of several recordings played one\nafter the other, each one modified so that they blend together into a continuous\nflow of music. A DJ mix release requires that the recordings be modified in some\nmanner, and the DJ who does this modification is usually (although not always)\ncredited in a fairly prominent way.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "MIXTAPE",
"description": "Promotional in nature (but not necessarily free), mixtapes\nand street albums are often released by artists to promote new artists, or\nupcoming studio albums by prominent artists. They are also sometimes used to\nkeep fans attention between studio releases and are most common in rap & hip\nhop genres. They are often not sanctioned by the artists label, may lack proper\nsample or song clearances and vary widely in production and recording quality.\nWhile mixtapes are generally DJ-mixed, they are distinct from commercial DJ\nmixes (which are usually deemed compilations) and are defined by having a\nsignificant proportion of new material, including original production or\noriginal vocals over top of other artists instrumentals. They are distinct from\ndemos in that they are designed for release directly to the public and fans, not\nto labels.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "DEMO",
"description": "A release that was recorded for limited circulation or\nreference use rather than for general public release.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "NAT",
"description": "A non-album track (special case).",
"isDeprecated": false,
"deprecationReason": null
}
],
"possibleTypes": null
},
{
"kind": "ENUM",
"name": "ReleaseStatus",
"description": "A type used to describe the status of releases, e.g. official,\nbootleg, etc.",
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": [
{
"name": "OFFICIAL",
"description": "Any release officially sanctioned by the artist and/or their\nrecord company. (Most releases will fit into this category.)",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "PROMOTION",
"description": "A giveaway release or a release intended to promote an\nupcoming official release, e.g. prerelease albums or releases included with a\nmagazine.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "BOOTLEG",
"description": "An unofficial/underground release that was not sanctioned by\nthe artist and/or the record company.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "PSEUDORELEASE",
"description": "A pseudo-release is a duplicate release for\ntranslation/transliteration purposes.",
"isDeprecated": false,
"deprecationReason": null
}
],
"possibleTypes": null "possibleTypes": null
}, },
{ {
@ -3215,41 +3341,6 @@
"enumValues": null, "enumValues": null,
"possibleTypes": null "possibleTypes": null
}, },
{
"kind": "ENUM",
"name": "ReleaseStatus",
"description": "A type used to describe the status of releases, e.g. official,\nbootleg, etc.",
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": [
{
"name": "OFFICIAL",
"description": "Any release officially sanctioned by the artist and/or their\nrecord company. (Most releases will fit into this category.)",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "PROMOTION",
"description": "A giveaway release or a release intended to promote an\nupcoming official release, e.g. prerelease albums or releases included with a\nmagazine.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "BOOTLEG",
"description": "An unofficial/underground release that was not sanctioned by\nthe artist and/or the record company.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "PSEUDORELEASE",
"description": "A pseudo-release is a duplicate release for\ntranslation/transliteration purposes.",
"isDeprecated": false,
"deprecationReason": null
}
],
"possibleTypes": null
},
{ {
"kind": "OBJECT", "kind": "OBJECT",
"name": "Medium", "name": "Medium",
@ -3330,22 +3421,6 @@
}, },
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
},
{
"name": "tracks",
"description": "The list of tracks on the given media.",
"args": [],
"type": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "Track",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
} }
], ],
"inputFields": null, "inputFields": null,
@ -3493,99 +3568,6 @@
"enumValues": null, "enumValues": null,
"possibleTypes": null "possibleTypes": null
}, },
{
"kind": "OBJECT",
"name": "Track",
"description": "A track is the way a recording is represented on a particular\n release (or, more exactly, on a particular medium). Every track has a title\n (see the guidelines for titles) and is credited to one or more artists.",
"fields": [
{
"name": "mbid",
"description": "The MBID of the entity.",
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "MBID",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "title",
"description": "The official title of the entity.",
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "position",
"description": "The tracks position on the overall release (including all\ntracks from all discs).",
"args": [],
"type": {
"kind": "SCALAR",
"name": "Int",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "number",
"description": "The track number, which may include information about the\ndisc or side it appears on, e.g. “A1” or “B3”.",
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "length",
"description": "The length of the track.",
"args": [],
"type": {
"kind": "SCALAR",
"name": "Duration",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "recording",
"description": "The recording that appears on the track.",
"args": [],
"type": {
"kind": "OBJECT",
"name": "Recording",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
}
],
"inputFields": null,
"interfaces": [
{
"kind": "INTERFACE",
"name": "Entity",
"ofType": null
}
],
"enumValues": null,
"possibleTypes": null
},
{ {
"kind": "OBJECT", "kind": "OBJECT",
"name": "LabelConnection", "name": "LabelConnection",
@ -4053,113 +4035,6 @@
"enumValues": null, "enumValues": null,
"possibleTypes": null "possibleTypes": null
}, },
{
"kind": "ENUM",
"name": "ReleaseGroupType",
"description": "A type used to describe release groups, e.g. album, single, EP,\netc.",
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": [
{
"name": "ALBUM",
"description": "An album, perhaps better defined as a “Long Play” (LP)\nrelease, generally consists of previously unreleased material (unless this type\nis combined with secondary types which change that, such as “Compilation”). This\nincludes album re-issues, with or without bonus tracks.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "SINGLE",
"description": "A single typically has one main song and possibly a handful\nof additional tracks or remixes of the main track. A single is usually named\nafter its main song.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "EP",
"description": "An EP is a so-called “Extended Play” release and often\ncontains the letters EP in the title. Generally an EP will be shorter than a\nfull length release (an LP or “Long Play”) and the tracks are usually exclusive\nto the EP, in other words the tracks dont come from a previously issued\nrelease. EP is fairly difficult to define; usually it should only be assumed\nthat a release is an EP if the artist defines it as such.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "OTHER",
"description": "Any release that does not fit any of the other categories.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "BROADCAST",
"description": "An episodic release that was originally broadcast via radio,\ntelevision, or the Internet, including podcasts.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "COMPILATION",
"description": "A compilation is a collection of previously released tracks\nby one or more artists.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "SOUNDTRACK",
"description": "A soundtrack is the musical score to a movie, TV series,\nstage show, computer game, etc.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "SPOKENWORD",
"description": "A non-music spoken word release.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "INTERVIEW",
"description": "An interview release contains an interview, generally with\nan artist.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "AUDIOBOOK",
"description": "An audiobook is a book read by a narrator without music.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "LIVE",
"description": "A release that was recorded live.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "REMIX",
"description": "A release that was (re)mixed from previously released\nmaterial.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "DJMIX",
"description": "A DJ-mix is a sequence of several recordings played one\nafter the other, each one modified so that they blend together into a continuous\nflow of music. A DJ mix release requires that the recordings be modified in some\nmanner, and the DJ who does this modification is usually (although not always)\ncredited in a fairly prominent way.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "MIXTAPE",
"description": "Promotional in nature (but not necessarily free), mixtapes\nand street albums are often released by artists to promote new artists, or\nupcoming studio albums by prominent artists. They are also sometimes used to\nkeep fans attention between studio releases and are most common in rap & hip\nhop genres. They are often not sanctioned by the artists label, may lack proper\nsample or song clearances and vary widely in production and recording quality.\nWhile mixtapes are generally DJ-mixed, they are distinct from commercial DJ\nmixes (which are usually deemed compilations) and are defined by having a\nsignificant proportion of new material, including original production or\noriginal vocals over top of other artists instrumentals. They are distinct from\ndemos in that they are designed for release directly to the public and fans, not\nto labels.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "DEMO",
"description": "A release that was recorded for limited circulation or\nreference use rather than for general public release.",
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "NAT",
"description": "A non-album track (special case).",
"isDeprecated": false,
"deprecationReason": null
}
],
"possibleTypes": null
},
{ {
"kind": "OBJECT", "kind": "OBJECT",
"name": "Relationships", "name": "Relationships",
@ -6649,7 +6524,7 @@
{ {
"kind": "SCALAR", "kind": "SCALAR",
"name": "Float", "name": "Float",
"description": "The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](https://en.wikipedia.org/wiki/IEEE_floating_point).", "description": "The `Float` scalar type represents signed double-precision fractional values as specified by [IEEE 754](http://en.wikipedia.org/wiki/IEEE_floating_point). ",
"fields": null, "fields": null,
"inputFields": null, "inputFields": null,
"interfaces": null, "interfaces": null,
@ -8784,6 +8659,16 @@
"enumValues": null, "enumValues": null,
"possibleTypes": null "possibleTypes": null
}, },
{
"kind": "SCALAR",
"name": "URLString",
"description": "A web address.",
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{ {
"kind": "OBJECT", "kind": "OBJECT",
"name": "URL", "name": "URL",
@ -8866,16 +8751,6 @@
"enumValues": null, "enumValues": null,
"possibleTypes": null "possibleTypes": null
}, },
{
"kind": "SCALAR",
"name": "URLString",
"description": "A web address.",
"fields": null,
"inputFields": null,
"interfaces": null,
"enumValues": null,
"possibleTypes": null
},
{ {
"kind": "OBJECT", "kind": "OBJECT",
"name": "BrowseQuery", "name": "BrowseQuery",
@ -10252,18 +10127,6 @@
"name": "__Schema", "name": "__Schema",
"description": "A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.", "description": "A GraphQL Schema defines the capabilities of a GraphQL server. It exposes all available types and directives on the server, as well as the entry points for query, mutation, and subscription operations.",
"fields": [ "fields": [
{
"name": "description",
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{ {
"name": "types", "name": "types",
"description": "A list of all types supported by this server.", "description": "A list of all types supported by this server.",
@ -10361,7 +10224,7 @@
{ {
"kind": "OBJECT", "kind": "OBJECT",
"name": "__Type", "name": "__Type",
"description": "The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.\n\nDepending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name, description and optional `specifiedByUrl`, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.", "description": "The fundamental unit of any GraphQL Schema is the type. There are many kinds of types in GraphQL as represented by the `__TypeKind` enum.\n\nDepending on the kind of a type, certain fields describe information about that type. Scalar types provide no information beyond a name and description, while Enum types provide their values. Object and Interface types provide the fields they describe. Abstract types, Union and Interface, provide the Object types possible at runtime. List and NonNull types compose other types.",
"fields": [ "fields": [
{ {
"name": "kind", "name": "kind",
@ -10403,18 +10266,6 @@
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
}, },
{
"name": "specifiedByUrl",
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
},
{ {
"name": "fields", "name": "fields",
"description": null, "description": null,
@ -10520,18 +10371,7 @@
{ {
"name": "inputFields", "name": "inputFields",
"description": null, "description": null,
"args": [ "args": [],
{
"name": "includeDeprecated",
"description": null,
"type": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
},
"defaultValue": "false"
}
],
"type": { "type": {
"kind": "LIST", "kind": "LIST",
"name": null, "name": null,
@ -10588,7 +10428,7 @@
}, },
{ {
"name": "INTERFACE", "name": "INTERFACE",
"description": "Indicates this type is an interface. `fields`, `interfaces`, and `possibleTypes` are valid fields.", "description": "Indicates this type is an interface. `fields` and `possibleTypes` are valid fields.",
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
}, },
@ -10661,18 +10501,7 @@
{ {
"name": "args", "name": "args",
"description": null, "description": null,
"args": [ "args": [],
{
"name": "includeDeprecated",
"description": null,
"type": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
},
"defaultValue": "false"
}
],
"type": { "type": {
"kind": "NON_NULL", "kind": "NON_NULL",
"name": null, "name": null,
@ -10803,34 +10632,6 @@
}, },
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
},
{
"name": "isDeprecated",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{
"name": "deprecationReason",
"description": null,
"args": [],
"type": {
"kind": "SCALAR",
"name": "String",
"ofType": null
},
"isDeprecated": false,
"deprecationReason": null
} }
], ],
"inputFields": null, "inputFields": null,
@ -10938,22 +10739,6 @@
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
}, },
{
"name": "isRepeatable",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
}
},
"isDeprecated": false,
"deprecationReason": null
},
{ {
"name": "locations", "name": "locations",
"description": null, "description": null,
@ -11001,6 +10786,54 @@
}, },
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
},
{
"name": "onOperation",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
}
},
"isDeprecated": true,
"deprecationReason": "Use `locations`."
},
{
"name": "onFragment",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
}
},
"isDeprecated": true,
"deprecationReason": "Use `locations`."
},
{
"name": "onField",
"description": null,
"args": [],
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "Boolean",
"ofType": null
}
},
"isDeprecated": true,
"deprecationReason": "Use `locations`."
} }
], ],
"inputFields": null, "inputFields": null,
@ -11058,12 +10891,6 @@
"isDeprecated": false, "isDeprecated": false,
"deprecationReason": null "deprecationReason": null
}, },
{
"name": "VARIABLE_DEFINITION",
"description": "Location adjacent to a variable definition.",
"isDeprecated": false,
"deprecationReason": null
},
{ {
"name": "SCHEMA", "name": "SCHEMA",
"description": "Location adjacent to a schema definition.", "description": "Location adjacent to a schema definition.",
@ -11190,14 +11017,12 @@
"description": "Marks an element of a GraphQL schema as no longer supported.", "description": "Marks an element of a GraphQL schema as no longer supported.",
"locations": [ "locations": [
"FIELD_DEFINITION", "FIELD_DEFINITION",
"ARGUMENT_DEFINITION",
"INPUT_FIELD_DEFINITION",
"ENUM_VALUE" "ENUM_VALUE"
], ],
"args": [ "args": [
{ {
"name": "reason", "name": "reason",
"description": "Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted using the Markdown syntax, as specified by [CommonMark](https://commonmark.org/).", "description": "Explains why this element was deprecated, usually also including a suggestion for how to access supported similar data. Formatted in [Markdown](https://daringfireball.net/projects/markdown/).",
"type": { "type": {
"kind": "SCALAR", "kind": "SCALAR",
"name": "String", "name": "String",
@ -11206,29 +11031,6 @@
"defaultValue": "\"No longer supported\"" "defaultValue": "\"No longer supported\""
} }
] ]
},
{
"name": "specifiedBy",
"description": "Exposes a URL that specifies the behaviour of this scalar.",
"locations": [
"SCALAR"
],
"args": [
{
"name": "url",
"description": "The URL that specifies the behaviour of this scalar.",
"type": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "SCALAR",
"name": "String",
"ofType": null
}
},
"defaultValue": null
}
]
} }
] ]
} }

View file

@ -1,62 +1,52 @@
import path from 'path'; import path from 'path'
import { fileURLToPath } from 'url'; import { graphql, introspectionQuery } from 'graphql'
import GraphQL from 'graphql'; import { updateSchema, diffSchema } from 'graphql-markdown'
import GraphQLMarkdown from 'graphql-markdown'; import baseSchema, { createSchema } from '../src/schema'
import { baseSchema, createSchema } from '../src/schema.js';
const { graphql, getIntrospectionQuery } = GraphQL; const extensionModules = [
const { updateSchema, diffSchema } = GraphQLMarkdown;
async function getSchemaJSON(schema) {
const result = await graphql(schema, getIntrospectionQuery());
return result.data;
}
async function buildExtensionDocs(extensionModules) {
return Promise.all(
extensionModules.map(async (extensionName) => {
const extensionModule = await import(
`../src/extensions/${extensionName}/index.js`
);
const extension = extensionModule.default;
console.log(`Generating docs for “${extension.name}” extension...`);
const schema = createSchema(baseSchema, { extensions: [extension] });
const [baseSchemaJSON, schemaJSON] = await Promise.all([
getSchemaJSON(baseSchema),
getSchemaJSON(schema),
]);
const outputSchema = diffSchema(baseSchemaJSON, schemaJSON, {
processTypeDiff(type) {
if (type.description === undefined) {
type.description =
':small_blue_diamond: *This type has been extended. See the ' +
'[base schema](../types.md)\nfor a description and additional ' +
'fields.*';
}
return type;
},
});
const outputPath = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
`../docs/extensions/${extensionName}.md`
);
return updateSchema(outputPath, outputSchema, {
unknownTypeURL: '../types.md',
headingLevel: 2,
});
})
);
}
buildExtensionDocs([
'cover-art-archive', 'cover-art-archive',
'fanart-tv', 'fanart-tv',
'mediawiki', 'mediawiki',
'the-audio-db', 'the-audio-db'
]) ]
.then((extensions) => {
console.log(`Built docs for ${extensions.length} extension(s).`); function getSchemaJSON(schema) {
return graphql(schema, introspectionQuery).then(result => result.data)
}
Promise.all(
extensionModules.map(extensionModule => {
const extension = require(`../src/extensions/${extensionModule}`).default
console.log(`Generating docs for “${extension.name}” extension...`)
const schema = createSchema(baseSchema, { extensions: [extension] })
return Promise.all([getSchemaJSON(baseSchema), getSchemaJSON(schema)]).then(
([baseSchemaJSON, schemaJSON]) => {
const outputSchema = diffSchema(baseSchemaJSON, schemaJSON, {
processTypeDiff(type) {
if (type.description === undefined) {
type.description =
':small_blue_diamond: *This type has been extended. See the ' +
'[base schema](../types.md)\nfor a description and additional ' +
'fields.*'
}
return type
}
})
const outputPath = path.resolve(
__dirname,
`../docs/extensions/${extensionModule}.md`
)
return updateSchema(outputPath, outputSchema, {
unknownTypeURL: '../types.md',
headingLevel: 2
})
}
)
})
)
.then(extensions => {
console.log(`Built docs for ${extensions.length} extension(s).`)
})
.catch(err => {
console.log('Error:', err)
}) })
.catch((err) => {
console.log('Error:', err);
});

View file

@ -22,4 +22,3 @@ if [[ $STASH_OUTPUT != "No local changes"* ]]; then
fi fi
echo -e "\n${GREEN}✔︎ Successfully deployed.${RESET}" echo -e "\n${GREEN}✔︎ Successfully deployed.${RESET}"
heroku open || true

View file

@ -1,16 +1,14 @@
import GraphQL from 'graphql'; import { graphql, introspectionQuery, printSchema } from 'graphql'
import { baseSchema as schema } from '../src/schema.js'; import schema from '../src/schema'
const { graphql, getIntrospectionQuery, printSchema } = GraphQL;
if (process.argv[2] === '--json') { if (process.argv[2] === '--json') {
graphql(schema, getIntrospectionQuery()) graphql(schema, introspectionQuery)
.then((result) => { .then(result => {
console.log(JSON.stringify(result, null, 2)); console.log(JSON.stringify(result, null, 2))
})
.catch(err => {
console.error(err)
}) })
.catch((err) => {
console.error(err);
});
} else { } else {
console.log(printSchema(schema)); console.log(printSchema(schema))
} }

View file

@ -1,88 +1,146 @@
import { fileURLToPath } from 'url'; import request from 'request'
import createDebug from 'debug'; import retry from 'retry'
import got from 'got'; import ExtendableError from 'es6-error'
import { readPackageUpSync } from 'read-pkg-up'; import RateLimit from '../rate-limit'
import RateLimit from '../rate-limit.js'; import pkg from '../../package.json'
import { filterObjectValues, getTypeName } from '../util.js';
const debug = createDebug('graphbrainz:api/client'); const debug = require('debug')('graphbrainz:api/client')
const { packageJson: pkg } = readPackageUpSync({ // If the `request` callback returns an error, it indicates a failure at a lower
cwd: fileURLToPath(import.meta.url), // level than the HTTP response itself. If it's any of the following error
}); // codes, we should retry.
const RETRY_CODES = {
ECONNRESET: true,
ENOTFOUND: true,
ESOCKETTIMEDOUT: true,
ETIMEDOUT: true,
ECONNREFUSED: true,
EHOSTUNREACH: true,
EPIPE: true,
EAI_AGAIN: true
}
export default class Client { export class ClientError extends ExtendableError {
constructor({ constructor(message, statusCode) {
baseURL, super(message)
userAgent = `${pkg.name}/${pkg.version} ` + this.statusCode = statusCode
`( ${pkg.homepage || pkg.author.url || pkg.author.email} )`, }
extraHeaders = {}, }
timeout = 60000,
limit = 1, export default class Client {
period = 1000, constructor(
concurrency = 10, {
retry, baseURL,
} = {}) { userAgent = `${pkg.name}/${pkg.version} ` +
this.baseURL = baseURL; `( ${pkg.homepage || pkg.author.url || pkg.author.email} )`,
this.userAgent = userAgent; extraHeaders = {},
this.extraHeaders = extraHeaders; errorClass = ClientError,
this.timeout = timeout; timeout = 60000,
this.limiter = new RateLimit({ limit, period, concurrency }); limit = 1,
this.retryOptions = retry; period = 1000,
} concurrency = 10,
retries = 10,
parseErrorMessage(err) { // It's OK for `retryDelayMin` to be less than one second, even 0, because
return err; // `RateLimit` will already make sure we don't exceed the API rate limit.
} // We're not doing exponential backoff because it will help with being
// rate limited, but rather to be chill in case MusicBrainz is returning
/** // some other error or our network is failing.
* Send a request without any rate limiting. retryDelayMin = 100,
* Use `get` instead. retryDelayMax = 60000,
*/ randomizeRetry = true
async _get(path, { searchParams, ...options } = {}) { } = {}
const url = new URL(path, this.baseURL); ) {
if (searchParams) { this.baseURL = baseURL
if (getTypeName(searchParams) === 'Object') { this.userAgent = userAgent
searchParams = filterObjectValues( this.extraHeaders = extraHeaders
searchParams, this.errorClass = errorClass
(value) => value != null this.timeout = timeout
); this.limiter = new RateLimit({ limit, period, concurrency })
} this.retryOptions = {
const moreSearchParams = new URLSearchParams(searchParams); retries,
moreSearchParams.forEach((value, key) => { minTimeout: retryDelayMin,
url.searchParams.set(key, value); maxTimeout: retryDelayMax,
}); randomize: randomizeRetry
} }
options = { }
responseType: 'json',
timeout: this.timeout, /**
retry: this.retryOptions, * Determine if we should retry the request based on the given error.
...options, * Retry any 5XX response from MusicBrainz, as well as any error in
headers: { * `RETRY_CODES`.
'User-Agent': this.userAgent, */
...this.extraHeaders, shouldRetry(err) {
...options.headers, if (err instanceof this.errorClass) {
}, return err.statusCode >= 500 && err.statusCode < 600
}; }
return RETRY_CODES[err.code] || false
let response; }
try {
debug(`Sending request. url=%s`, url); parseErrorMessage(response, body) {
response = await got(url.toString(), options); return typeof body === 'string' && body ? body : `${response.statusCode}`
debug(`Success: %s url=%s`, response.statusCode, url); }
return response;
} catch (err) { /**
const parsedError = this.parseErrorMessage(err) || err; * Send a request without any retrying or rate limiting.
debug(`Error: “%s” url=%s`, parsedError, url); * Use `get` instead.
throw parsedError; */
} _get(path, options = {}, info = {}) {
} return new Promise((resolve, reject) => {
options = {
/** baseUrl: this.baseURL,
* Send a request with rate limiting. url: path,
*/ gzip: true,
get(path, options = {}) { timeout: this.timeout,
const fn = this._get.bind(this); ...options,
return this.limiter.enqueue(fn, [path, options]); headers: {
'User-Agent': this.userAgent,
...this.extraHeaders,
...options.headers
}
}
const url = `${options.baseUrl}${options.url}`
debug(`Sending request. url=${url} attempt=${info.currentAttempt}`)
request(options, (err, response, body) => {
if (err) {
debug(`Error: “${err}” url=${url}`)
reject(err)
} else if (response.statusCode >= 400) {
const message = this.parseErrorMessage(response, body)
debug(`Error: “${message}” url=${url}`)
const ClientError = this.errorClass
reject(new ClientError(message, response.statusCode))
} else if (options.method === 'HEAD') {
resolve(response.headers)
} else {
resolve(body)
}
})
})
}
/**
* Send a request with retrying and rate limiting.
*/
get(path, options = {}) {
return new Promise((resolve, reject) => {
const fn = this._get.bind(this)
const operation = retry.operation(this.retryOptions)
operation.attempt(currentAttempt => {
// This will increase the priority in our `RateLimit` queue for each
// retry, so that newer requests don't delay this one further.
const priority = currentAttempt
this.limiter
.enqueue(fn, [path, options, { currentAttempt }], priority)
.then(resolve)
.catch(err => {
if (!this.shouldRetry(err) || !operation.retry(err)) {
reject(operation.mainError() || err)
}
})
})
})
} }
} }

View file

@ -1,3 +1,3 @@
import MusicBrainz from './musicbrainz.js'; import MusicBrainz, { MusicBrainzError } from './musicbrainz'
export { MusicBrainz as default, MusicBrainz }; export { MusicBrainz as default, MusicBrainz, MusicBrainzError }

View file

@ -1,109 +1,92 @@
import ExtendableError from 'es6-error'; import qs from 'qs'
import Client from './client.js'; import Client, { ClientError } from './client'
import { filterObjectValues } from '../util.js';
export class MusicBrainzError extends ExtendableError { export class MusicBrainzError extends ClientError {}
constructor(message, response) {
super(message);
this.response = response;
}
}
export default class MusicBrainz extends Client { export default class MusicBrainz extends Client {
constructor({ constructor(
baseURL = process.env.MUSICBRAINZ_BASE_URL || {
'http://musicbrainz.org/ws/2/', baseURL = process.env.MUSICBRAINZ_BASE_URL ||
// MusicBrainz API requests are limited to an *average* of 1 req/sec. 'http://musicbrainz.org/ws/2/',
// That means if, for example, we only need to make a few API requests to errorClass = MusicBrainzError,
// fulfill a query, we might as well make them all at once - as long as // MusicBrainz API requests are limited to an *average* of 1 req/sec.
// we then wait a few seconds before making more. In practice this can // That means if, for example, we only need to make a few API requests to
// seemingly be set to about 5 requests every 5 seconds before we're // fulfill a query, we might as well make them all at once - as long as
// considered to exceed the rate limit. // we then wait a few seconds before making more. In practice this can
limit = 5, // seemingly be set to about 5 requests every 5 seconds before we're
period = 5500, // considered to exceed the rate limit.
...options limit = 5,
} = {}) { period = 5500,
super({ baseURL, limit, period, ...options }); ...options
} = {}
) {
super({ baseURL, errorClass, limit, period, ...options })
} }
parseErrorMessage(err) { parseErrorMessage(response, body) {
if (err.name === 'HTTPError') { if (body && body.error) {
const { body } = err.response; return body.error
if (body && body.error) {
return new MusicBrainzError(`${body.error}`, err.response);
}
} }
return super.parseErrorMessage(err); return super.parseErrorMessage(response, body)
}
get(url, options = {}) {
options = {
resolveBodyOnly: true,
...options,
searchParams: {
fmt: 'json',
...options.searchParams,
},
};
return super.get(url, options);
} }
stringifyParams(params) { stringifyParams(params) {
if (Array.isArray(params.inc)) { if (Array.isArray(params.inc)) {
params = { params = {
...params, ...params,
inc: params.inc.join('+'), inc: params.inc.join('+')
}; }
} }
if (Array.isArray(params.type)) { if (Array.isArray(params.type)) {
params = { params = {
...params, ...params,
type: params.type.join('|'), type: params.type.join('|')
}; }
} }
if (Array.isArray(params.status)) { if (Array.isArray(params.status)) {
params = { params = {
...params, ...params,
status: params.status.join('|'), status: params.status.join('|')
}; }
} }
return new URLSearchParams( return qs.stringify(params, {
filterObjectValues(params, (value) => value != null && value !== '') skipNulls: true,
).toString(); filter: (key, value) => (value === '' ? undefined : value)
})
} }
getURL(path, params) { getURL(path, params) {
const query = params ? this.stringifyParams(params) : ''; const query = params ? this.stringifyParams(params) : ''
return query ? `${path}?${query}` : path; return query ? `${path}?${query}` : path
} }
getLookupURL(entity, id, params) { getLookupURL(entity, id, params) {
if (id == null) { if (id == null) {
return this.getBrowseURL(entity, params); return this.getBrowseURL(entity, params)
} }
return this.getURL(`${entity}/${id}`, params); return this.getURL(`${entity}/${id}`, params)
} }
lookup(entity, id, params = {}) { lookup(entity, id, params = {}) {
const url = this.getLookupURL(entity, id, params); const url = this.getLookupURL(entity, id, params)
return this.get(url); return this.get(url, { json: true, qs: { fmt: 'json' } })
} }
getBrowseURL(entity, params) { getBrowseURL(entity, params) {
return this.getURL(entity, params); return this.getURL(entity, params)
} }
browse(entity, params = {}) { browse(entity, params = {}) {
const url = this.getBrowseURL(entity, params); const url = this.getBrowseURL(entity, params)
return this.get(url); return this.get(url, { json: true, qs: { fmt: 'json' } })
} }
getSearchURL(entity, query, params) { getSearchURL(entity, query, params) {
return this.getURL(entity, { ...params, query }); return this.getURL(entity, { ...params, query })
} }
search(entity, query, params = {}) { search(entity, query, params = {}) {
const url = this.getSearchURL(entity, query, params); const url = this.getSearchURL(entity, query, params)
return this.get(url); return this.get(url, { json: true, qs: { fmt: 'json' } })
} }
} }

View file

@ -1,30 +1,33 @@
import createLoaders from './loaders.js'; import createLoaders from './loaders'
import createDebug from 'debug'; import { loadExtension } from './extensions'
const debug = createDebug('graphbrainz:context'); const debug = require('debug')('graphbrainz:context')
export function extendContext(extension, context, options) { export function extendContext(extension, context, options) {
if (extension.extendContext) { if (extension.extendContext) {
if (typeof extension.extendContext === 'function') { if (typeof extension.extendContext === 'function') {
debug( debug(
`Extending context via a function from the “${extension.name}” extension.` `Extending context via a function from the “${
); extension.name
context = extension.extendContext(context, options); } extension.`
)
context = extension.extendContext(context, options)
} else { } else {
throw new Error( throw new Error(
`Extension “${extension.name}” contains an invalid \`extendContext\` ` + `Extension “${extension.name}” contains an invalid \`extendContext\` ` +
`value: ${extension.extendContext}` `value: ${extension.extendContext}`
); )
} }
} }
return context; return context
} }
export function createContext(options = {}) { export function createContext(options = {}) {
const { client, extensions = [] } = options; const { client } = options
const loaders = createLoaders(client); const loaders = createLoaders(client)
let context = { client, loaders }; const context = { client, loaders }
const { extensions = [] } = options
return extensions.reduce((context, extension) => { return extensions.reduce((context, extension) => {
return extendContext(extension, context, options); return extendContext(loadExtension(extension), context, options)
}, context); }, context)
} }

View file

@ -1,55 +1,41 @@
import ExtendableError from 'es6-error'; import Client from '../../api/client'
import Client from '../../api/client.js';
export class CoverArtArchiveError extends ExtendableError {
constructor(message, response) {
super(message);
this.response = response;
}
}
export default class CoverArtArchiveClient extends Client { export default class CoverArtArchiveClient extends Client {
constructor({ constructor(
baseURL = process.env.COVER_ART_ARCHIVE_BASE_URL || {
'http://coverartarchive.org/', baseURL = process.env.COVER_ART_ARCHIVE_BASE_URL ||
limit = 10, 'http://coverartarchive.org/',
period = 1000, limit = 10,
...options period = 1000,
} = {}) { ...options
super({ baseURL, limit, period, ...options }); } = {}
) {
super({ baseURL, limit, period, ...options })
} }
/** /**
* Sinfully attempt to parse HTML responses for the error message. * Sinfully attempt to parse HTML responses for the error message.
*/ */
parseErrorMessage(err) { parseErrorMessage(response, body) {
if (err.name === 'HTTPError') { if (typeof body === 'string' && body.startsWith('<!')) {
const { body } = err.response; const heading = /<h1>([^<]+)<\/h1>/i.exec(body)
if (typeof body === 'string' && body.startsWith('<!')) { const message = /<p>([^<]+)<\/p>/i.exec(body)
const heading = /<h1>([^<]+)<\/h1>/i.exec(body); return `${heading ? heading[1] + ': ' : ''}${message ? message[1] : ''}`
const message = /<p>([^<]+)<\/p>/i.exec(body);
return new CoverArtArchiveError(
`${heading ? heading[1] + ': ' : ''}${message ? message[1] : ''}`,
err.response
);
}
} }
return super.parseErrorMessage(err); return super.parseErrorMessage(response, body)
} }
images(entityType, mbid) { images(entityType, mbid) {
return this.get(`${entityType}/${mbid}`, { resolveBodyOnly: true }); return this.get(`${entityType}/${mbid}`, { json: true })
} }
async imageURL(entityType, mbid, typeOrID = 'front', size) { imageURL(entityType, mbid, typeOrID = 'front', size) {
let url = `${entityType}/${mbid}/${typeOrID}`; let url = `${entityType}/${mbid}/${typeOrID}`
if (size != null) { if (size != null) {
url += `-${size}`; url += `-${size}`
} }
const response = await this.get(url, { return this.get(url, { method: 'HEAD', followRedirect: false }).then(
method: 'HEAD', headers => headers.location
followRedirect: false, )
});
return response.headers.location;
} }
} }

View file

@ -1,27 +1,27 @@
import schema from './schema.js'; import schema from './schema'
import resolvers from './resolvers.js'; import resolvers from './resolvers'
import createLoaders from './loaders.js'; import createLoaders from './loaders'
import CoverArtArchiveClient from './client.js'; import CoverArtArchiveClient from './client'
import { ONE_DAY } from '../../util.js'; import { ONE_DAY } from '../../util'
export default { export default {
name: 'Cover Art Archive', name: 'Cover Art Archive',
description: `Retrieve cover art images for releases from the [Cover Art description: `Retrieve cover art images for releases from the [Cover Art
Archive](https://coverartarchive.org/).`, Archive](https://coverartarchive.org/).`,
extendContext(context, { coverArtClient, coverArtArchive = {} } = {}) { extendContext(context, { coverArtClient, coverArtArchive = {} } = {}) {
const client = coverArtClient || new CoverArtArchiveClient(coverArtArchive); const client = coverArtClient || new CoverArtArchiveClient(coverArtArchive)
const cacheSize = parseInt( const cacheSize = parseInt(
process.env.COVER_ART_ARCHIVE_CACHE_SIZE || process.env.COVER_ART_ARCHIVE_CACHE_SIZE ||
process.env.GRAPHBRAINZ_CACHE_SIZE || process.env.GRAPHBRAINZ_CACHE_SIZE ||
8192, 8192,
10 10
); )
const cacheTTL = parseInt( const cacheTTL = parseInt(
process.env.COVER_ART_ARCHIVE_CACHE_TTL || process.env.COVER_ART_ARCHIVE_CACHE_TTL ||
process.env.GRAPHBRAINZ_CACHE_TTL || process.env.GRAPHBRAINZ_CACHE_TTL ||
ONE_DAY, ONE_DAY,
10 10
); )
return { return {
...context, ...context,
// Add the client instance directly onto `context` for backwards // Add the client instance directly onto `context` for backwards
@ -29,12 +29,12 @@ Archive](https://coverartarchive.org/).`,
coverArtClient: client, coverArtClient: client,
loaders: { loaders: {
...context.loaders, ...context.loaders,
...createLoaders({ client, cacheSize, cacheTTL }), ...createLoaders({ client, cacheSize, cacheTTL })
}, }
}; }
}, },
extendSchema: { extendSchema: {
schemas: [schema], schemas: [schema],
resolvers, resolvers
}, }
}; }

View file

@ -1,68 +1,66 @@
import createDebug from 'debug'; import DataLoader from 'dataloader'
import DataLoader from 'dataloader'; import LRUCache from 'lru-cache'
import LRUCache from 'lru-cache';
const debug = createDebug('graphbrainz:extensions/cover-art-archive'); const debug = require('debug')('graphbrainz:extensions/cover-art-archive')
export default function createLoaders(options) { export default function createLoaders(options) {
const { client } = options; const { client } = options
const cache = new LRUCache({ const cache = LRUCache({
max: options.cacheSize, max: options.cacheSize,
maxAge: options.cacheTTL, maxAge: options.cacheTTL,
dispose(key) { dispose(key) {
debug(`Removed from cache. key=${key}`); debug(`Removed from cache. key=${key}`)
}, }
}); })
// Make the cache Map-like. // Make the cache Map-like.
cache.delete = cache.del; cache.delete = cache.del
cache.clear = cache.reset; cache.clear = cache.reset
return { return {
coverArtArchive: new DataLoader( coverArtArchive: new DataLoader(
(keys) => { keys => {
return Promise.all( return Promise.all(
keys.map((key) => { keys.map(key => {
const [entityType, id] = key; const [entityType, id] = key
return client return client
.images(entityType, id) .images(entityType, id)
.catch((err) => { .catch(err => {
if (err.response.statusCode === 404) { if (err.statusCode === 404) {
return { images: [] }; return { images: [] }
} }
throw err; throw err
}) })
.then((coverArt) => ({ .then(coverArt => ({
...coverArt, ...coverArt,
_entityType: entityType, _entityType: entityType,
_id: id, _id: id,
_releaseID: _releaseID:
coverArt.release && coverArt.release.split('/').pop(), coverArt.release && coverArt.release.split('/').pop()
})); }))
}) })
); )
}, },
{ {
cacheKeyFn: ([entityType, id]) => `${entityType}/${id}`, cacheKeyFn: ([entityType, id]) => `${entityType}/${id}`,
cacheMap: cache, cacheMap: cache
} }
), ),
coverArtArchiveURL: new DataLoader( coverArtArchiveURL: new DataLoader(
(keys) => { keys => {
return Promise.all( return Promise.all(
keys.map((key) => { keys.map(key => {
const [entityType, id, type, size] = key; const [entityType, id, type, size] = key
return client.imageURL(entityType, id, type, size); return client.imageURL(entityType, id, type, size)
}) })
); )
}, },
{ {
batch: false,
cacheKeyFn: ([entityType, id, type, size]) => { cacheKeyFn: ([entityType, id, type, size]) => {
const key = `${entityType}/${id}/${type}`; const key = `${entityType}/${id}/${type}`
return size ? `${key}-${size}` : key; return size ? `${key}-${size}` : key
}, },
cacheMap: cache, cacheMap: cache
} }
), )
}; }
} }

View file

@ -1,4 +1,4 @@
import { resolveLookup } from '../../resolvers.js'; import { resolveLookup } from '../../resolvers'
const SIZES = new Map([ const SIZES = new Map([
[null, null], [null, null],
@ -6,70 +6,70 @@ const SIZES = new Map([
[500, 500], [500, 500],
['FULL', null], ['FULL', null],
['SMALL', 250], ['SMALL', 250],
['LARGE', 500], ['LARGE', 500]
]); ])
function resolveImage(coverArt, args, { loaders }, info) { function resolveImage(coverArt, args, { loaders }, info) {
// Since migrating the schema to an extension, we lost custom enum values // Since migrating the schema to an extension, we lost custom enum values
// for the time being. Translate any incoming `size` arg to the old enum // for the time being. Translate any incoming `size` arg to the old enum
// values. // values.
const size = SIZES.get(args.size); const size = SIZES.get(args.size)
// Field should be `front` or `back`. // Field should be `front` or `back`.
const field = info.fieldName; const field = info.fieldName
if (coverArt.images) { if (coverArt.images) {
const matches = coverArt.images.filter((image) => image[field]); const matches = coverArt.images.filter(image => image[field])
if (!matches.length) { if (!matches.length) {
return null; return null
} else if (matches.length === 1) { } else if (matches.length === 1) {
const match = matches[0]; const match = matches[0]
if (size === 250) { if (size === 250) {
return match.thumbnails.small; return match.thumbnails.small
} else if (size === 500) { } else if (size === 500) {
return match.thumbnails.large; return match.thumbnails.large
} else { } else {
return match.image; return match.image
} }
} }
} }
const entityType = coverArt._entityType; const entityType = coverArt._entityType
const id = coverArt._id; const id = coverArt._id
const releaseID = coverArt._releaseID; const releaseID = coverArt._releaseID
if (entityType === 'release-group' && field === 'front') { if (entityType === 'release-group' && field === 'front') {
// Release groups only have an endpoint to retrieve the front image. // Release groups only have an endpoint to retrieve the front image.
// If someone requests the back of a release group, return the back of the // If someone requests the back of a release group, return the back of the
// release that the release group's cover art response points to. // release that the release group's cover art response points to.
return loaders.coverArtArchiveURL.load(['release-group', id, field, size]); return loaders.coverArtArchiveURL.load(['release-group', id, field, size])
} else { } else {
return loaders.coverArtArchiveURL.load(['release', releaseID, field, size]); return loaders.coverArtArchiveURL.load(['release', releaseID, field, size])
} }
} }
export default { export default {
CoverArtArchiveImage: { CoverArtArchiveImage: {
fileID: (image) => image.id, fileID: image => image.id
}, },
CoverArtArchiveRelease: { CoverArtArchiveRelease: {
front: resolveImage, front: resolveImage,
back: resolveImage, back: resolveImage,
images: (coverArt) => coverArt.images, images: coverArt => coverArt.images,
artwork: (coverArt) => coverArt.images.length > 0, artwork: coverArt => coverArt.images.length > 0,
count: (coverArt) => coverArt.images.length, count: coverArt => coverArt.images.length,
release: (coverArt, args, context, info) => { release: (coverArt, args, context, info) => {
const mbid = coverArt._releaseID; const mbid = coverArt._releaseID
if (mbid) { if (mbid) {
return resolveLookup(coverArt, { mbid }, context, info); return resolveLookup(coverArt, { mbid }, context, info)
} }
return null; return null
}, }
}, },
Release: { Release: {
coverArtArchive: (release, args, { loaders }) => { coverArtArchive: (release, args, { loaders }) => {
return loaders.coverArtArchive.load(['release', release.id]); return loaders.coverArtArchive.load(['release', release.id])
}, }
}, },
ReleaseGroup: { ReleaseGroup: {
coverArtArchive: (releaseGroup, args, { loaders }) => { coverArtArchive: (releaseGroup, args, { loaders }) => {
return loaders.coverArtArchive.load(['release-group', releaseGroup.id]); return loaders.coverArtArchive.load(['release-group', releaseGroup.id])
}, }
}, }
}; }

View file

@ -1,176 +1,121 @@
import gql from '../../tag.js'; import gql from '../../tag'
export default gql` export default gql`
""" # An individual piece of album artwork from the [Cover Art Archive](https://musicbrainz.org/doc/Cover_Art_Archive).
An individual piece of album artwork from the [Cover Art Archive](https://musicbrainz.org/doc/Cover_Art_Archive).
"""
type CoverArtArchiveImage { type CoverArtArchiveImage {
""" # The Internet Archives internal file ID for the image.
The Internet Archives internal file ID for the image.
"""
fileID: String! fileID: String!
""" # The URL at which the image can be found.
The URL at which the image can be found.
"""
image: URLString! image: URLString!
""" # A set of thumbnails for the image.
A set of thumbnails for the image.
"""
thumbnails: CoverArtArchiveImageThumbnails! thumbnails: CoverArtArchiveImageThumbnails!
""" # Whether this image depicts the main front of the release.
Whether this image depicts the main front of the release.
"""
front: Boolean! front: Boolean!
""" # Whether this image depicts the main back of the release.
Whether this image depicts the main back of the release.
"""
back: Boolean! back: Boolean!
""" # A list of [image types](https://musicbrainz.org/doc/Cover_Art/Types)
A list of [image types](https://musicbrainz.org/doc/Cover_Art/Types) # describing what part(s) of the release the image includes.
describing what part(s) of the release the image includes.
"""
types: [String]! types: [String]!
""" # The MusicBrainz edit ID.
The MusicBrainz edit ID.
"""
edit: Int edit: Int
""" # Whether the image was approved by the MusicBrainz edit system.
Whether the image was approved by the MusicBrainz edit system.
"""
approved: Boolean approved: Boolean
""" # A free-text comment left for the image.
A free-text comment left for the image.
"""
comment: String comment: String
} }
""" # The image sizes that may be requested at the [Cover Art Archive](https://musicbrainz.org/doc/Cover_Art_Archive).
The image sizes that may be requested at the [Cover Art Archive](https://musicbrainz.org/doc/Cover_Art_Archive).
"""
enum CoverArtArchiveImageSize { enum CoverArtArchiveImageSize {
""" # A maximum dimension of 250px.
A maximum dimension of 250px.
"""
SMALL SMALL
""" # A maximum dimension of 500px.
A maximum dimension of 500px.
"""
LARGE LARGE
""" # The images original dimensions, with no maximum.
The images original dimensions, with no maximum.
"""
FULL FULL
} }
""" # URLs for thumbnails of different sizes for a particular piece of
URLs for thumbnails of different sizes for a particular piece of cover art. # cover art.
"""
type CoverArtArchiveImageThumbnails { type CoverArtArchiveImageThumbnails {
""" # The URL of a small version of the cover art, where the maximum dimension is
The URL of a small version of the cover art, where the maximum dimension is # 250px.
250px.
"""
small: URLString small: URLString
""" # The URL of a large version of the cover art, where the maximum dimension is
The URL of a large version of the cover art, where the maximum dimension is # 500px.
500px.
"""
large: URLString large: URLString
} }
""" # An object containing a list of the cover art images for a release obtained
An object containing a list of the cover art images for a release obtained # from the [Cover Art Archive](https://musicbrainz.org/doc/Cover_Art_Archive),
from the [Cover Art Archive](https://musicbrainz.org/doc/Cover_Art_Archive), # as well as a summary of what artwork is available.
as well as a summary of what artwork is available.
"""
type CoverArtArchiveRelease { type CoverArtArchiveRelease {
""" # The URL of an image depicting the album cover or main front of the release,
The URL of an image depicting the album cover or main front of the release, # i.e. the front of the packaging of the audio recording (or in the case of a
i.e. the front of the packaging of the audio recording (or in the case of a # digital release, the image associated with it in a digital media store).
digital release, the image associated with it in a digital media store). #
# In the MusicBrainz schema, this field is a Boolean value indicating the
In the MusicBrainz schema, this field is a Boolean value indicating the # presence of a front image, whereas here the value is the URL for the image
presence of a front image, whereas here the value is the URL for the image # itself if one exists. You can check for null if you just want to determine
itself if one exists. You can check for null if you just want to determine # the presence of an image.
the presence of an image.
"""
front( front(
""" # The size of the image to retrieve. By default, the returned image will
The size of the image to retrieve. By default, the returned image will # have its full original dimensions, but certain thumbnail sizes may be
have its full original dimensions, but certain thumbnail sizes may be # retrieved as well.
retrieved as well.
"""
size: CoverArtArchiveImageSize = FULL size: CoverArtArchiveImageSize = FULL
): URLString ): URLString
""" # The URL of an image depicting the main back of the release, i.e. the back
The URL of an image depicting the main back of the release, i.e. the back # of the packaging of the audio recording.
of the packaging of the audio recording. #
# In the MusicBrainz schema, this field is a Boolean value indicating the
In the MusicBrainz schema, this field is a Boolean value indicating the # presence of a back image, whereas here the value is the URL for the image
presence of a back image, whereas here the value is the URL for the image # itself. You can check for null if you just want to determine the presence of
itself. You can check for null if you just want to determine the presence of # an image.
an image.
"""
back( back(
""" # The size of the image to retrieve. By default, the returned image will
The size of the image to retrieve. By default, the returned image will # have its full original dimensions, but certain thumbnail sizes may be
have its full original dimensions, but certain thumbnail sizes may be # retrieved as well.
retrieved as well.
"""
size: CoverArtArchiveImageSize = FULL size: CoverArtArchiveImageSize = FULL
): URLString ): URLString
""" # A list of images depicting the different sides and surfaces of a releases
A list of images depicting the different sides and surfaces of a releases # media and packaging.
media and packaging.
"""
images: [CoverArtArchiveImage]! images: [CoverArtArchiveImage]!
""" # Whether there is artwork present for this release.
Whether there is artwork present for this release.
"""
artwork: Boolean! artwork: Boolean!
""" # The number of artwork images present for this release.
The number of artwork images present for this release.
"""
count: Int! count: Int!
""" # The particular release shown in the returned cover art.
The particular release shown in the returned cover art.
"""
release: Release release: Release
} }
extend type Release { extend type Release {
""" # An object containing a list and summary of the cover art images that are
An object containing a list and summary of the cover art images that are # present for this release from the [Cover Art Archive](https://musicbrainz.org/doc/Cover_Art_Archive).
present for this release from the [Cover Art Archive](https://musicbrainz.org/doc/Cover_Art_Archive). # This field is provided by the Cover Art Archive extension.
This field is provided by the Cover Art Archive extension.
"""
coverArtArchive: CoverArtArchiveRelease coverArtArchive: CoverArtArchiveRelease
} }
extend type ReleaseGroup { extend type ReleaseGroup {
""" # The cover art for a release in the release group, obtained from the
The cover art for a release in the release group, obtained from the # [Cover Art Archive](https://musicbrainz.org/doc/Cover_Art_Archive). A
[Cover Art Archive](https://musicbrainz.org/doc/Cover_Art_Archive). A # release in the release group will be chosen as representative of the release
release in the release group will be chosen as representative of the release # group.
group. # This field is provided by the Cover Art Archive extension.
This field is provided by the Cover Art Archive extension.
"""
coverArtArchive: CoverArtArchiveRelease coverArtArchive: CoverArtArchiveRelease
} }
`; `

View file

@ -1,61 +1,63 @@
import ExtendableError from 'es6-error'; import Client from '../../api/client'
import Client from '../../api/client.js';
export class FanArtError extends ExtendableError {}
export default class FanArtClient extends Client { export default class FanArtClient extends Client {
constructor({ constructor(
apiKey = process.env.FANART_API_KEY, {
baseURL = process.env.FANART_BASE_URL || 'http://webservice.fanart.tv/v3/', apiKey = process.env.FANART_API_KEY,
limit = 10, baseURL = process.env.FANART_BASE_URL ||
period = 1000, 'http://webservice.fanart.tv/v3/',
...options limit = 10,
} = {}) { period = 1000,
super({ baseURL, limit, period, ...options }); ...options
this.apiKey = apiKey; } = {}
) {
super({ baseURL, limit, period, ...options })
this.apiKey = apiKey
} }
get(path, options = {}) { get(path, options = {}) {
const ClientError = this.errorClass
if (!this.apiKey) { if (!this.apiKey) {
return Promise.reject( return Promise.reject(
new FanArtError('No API key was configured for the fanart.tv client.') new ClientError('No API key was configured for the fanart.tv client.')
); )
} }
options = { options = {
resolveBodyOnly: true, json: true,
...options, ...options,
searchParams: { qs: {
...options.searchParams, ...options.qs,
api_key: this.apiKey, api_key: this.apiKey
}, }
}; }
return super.get(path, options); return super.get(path, options)
} }
musicEntity(entityType, mbid) { musicEntity(entityType, mbid) {
const ClientError = this.errorClass
switch (entityType) { switch (entityType) {
case 'artist': case 'artist':
return this.musicArtist(mbid); return this.musicArtist(mbid)
case 'label': case 'label':
return this.musicLabel(mbid); return this.musicLabel(mbid)
case 'release-group': case 'release-group':
return this.musicAlbum(mbid); return this.musicAlbum(mbid)
default: default:
return Promise.reject( return Promise.reject(
new FanArtError(`Entity type unsupported: ${entityType}`) new ClientError(`Entity type unsupported: ${entityType}`)
); )
} }
} }
musicArtist(mbid) { musicArtist(mbid) {
return this.get(`music/${mbid}`); return this.get(`music/${mbid}`)
} }
musicAlbum(mbid) { musicAlbum(mbid) {
return this.get(`music/albums/${mbid}`); return this.get(`music/albums/${mbid}`)
} }
musicLabel(mbid) { musicLabel(mbid) {
return this.get(`music/${mbid}`); return this.get(`music/${mbid}`)
} }
} }

View file

@ -1,37 +1,37 @@
import schema from './schema.js'; import schema from './schema'
import resolvers from './resolvers.js'; import resolvers from './resolvers'
import createLoader from './loader.js'; import createLoader from './loader'
import FanArtClient from './client.js'; import FanArtClient from './client'
import { ONE_DAY } from '../../util.js'; import { ONE_DAY } from '../../util'
export default { export default {
name: 'fanart.tv', name: 'fanart.tv',
description: `Retrieve high quality artwork for artists, releases, and labels description: `Retrieve high quality artwork for artists, releases, and labels
from [fanart.tv](https://fanart.tv/).`, from [fanart.tv](https://fanart.tv/).`,
extendContext(context, { fanArt = {} } = {}) { extendContext(context, { fanArt = {} } = {}) {
const client = new FanArtClient(fanArt); const client = new FanArtClient(fanArt)
const cacheSize = parseInt( const cacheSize = parseInt(
process.env.FANART_CACHE_SIZE || process.env.FANART_CACHE_SIZE ||
process.env.GRAPHBRAINZ_CACHE_SIZE || process.env.GRAPHBRAINZ_CACHE_SIZE ||
8192, 8192,
10 10
); )
const cacheTTL = parseInt( const cacheTTL = parseInt(
process.env.FANART_CACHE_TTL || process.env.FANART_CACHE_TTL ||
process.env.GRAPHBRAINZ_CACHE_TTL || process.env.GRAPHBRAINZ_CACHE_TTL ||
ONE_DAY, ONE_DAY,
10 10
); )
return { return {
...context, ...context,
loaders: { loaders: {
...context.loaders, ...context.loaders,
fanArt: createLoader({ client, cacheSize, cacheTTL }), fanArt: createLoader({ client, cacheSize, cacheTTL })
}, }
}; }
}, },
extendSchema: { extendSchema: {
schemas: [schema], schemas: [schema],
resolvers, resolvers
}, }
}; }

View file

@ -1,30 +1,29 @@
import createDebug from 'debug'; import DataLoader from 'dataloader'
import DataLoader from 'dataloader'; import LRUCache from 'lru-cache'
import LRUCache from 'lru-cache';
const debug = createDebug('graphbrainz:extensions/fanart-tv'); const debug = require('debug')('graphbrainz:extensions/fanart-tv')
export default function createLoader(options) { export default function createLoader(options) {
const { client } = options; const { client } = options
const cache = new LRUCache({ const cache = LRUCache({
max: options.cacheSize, max: options.cacheSize,
maxAge: options.cacheTTL, maxAge: options.cacheTTL,
dispose(key) { dispose(key) {
debug(`Removed from cache. key=${key}`); debug(`Removed from cache. key=${key}`)
}, }
}); })
// Make the cache Map-like. // Make the cache Map-like.
cache.delete = cache.del; cache.delete = cache.del
cache.clear = cache.reset; cache.clear = cache.reset
const loader = new DataLoader( const loader = new DataLoader(
(keys) => { keys => {
return Promise.all( return Promise.all(
keys.map((key) => { keys.map(key => {
const [entityType, id] = key; const [entityType, id] = key
return client return client
.musicEntity(entityType, id) .musicEntity(entityType, id)
.catch((err) => { .catch(err => {
if (err.statusCode === 404) { if (err.statusCode === 404) {
// 404s are OK, just return empty data. // 404s are OK, just return empty data.
return { return {
@ -34,32 +33,31 @@ export default function createLoader(options) {
hdmusiclogo: [], hdmusiclogo: [],
musicbanner: [], musicbanner: [],
musiclabel: [], musiclabel: [],
albums: {}, albums: {}
}; }
} }
throw err; throw err
}) })
.then((body) => { .then(body => {
if (entityType === 'artist') { if (entityType === 'artist') {
const releaseGroupIDs = Object.keys(body.albums || {}); const releaseGroupIDs = Object.keys(body.albums)
debug( debug(
`Priming album cache with ${releaseGroupIDs.length} album(s).` `Priming album cache with ${releaseGroupIDs.length} album(s).`
); )
releaseGroupIDs.forEach((key) => releaseGroupIDs.forEach(key =>
loader.prime(['release-group', key], body) loader.prime(['release-group', key], body)
); )
} }
return body; return body
}); })
}) })
); )
}, },
{ {
batch: false,
cacheKeyFn: ([entityType, id]) => `${entityType}/${id}`, cacheKeyFn: ([entityType, id]) => `${entityType}/${id}`,
cacheMap: cache, cacheMap: cache
} }
); )
return loader; return loader
} }

View file

@ -1,64 +1,64 @@
const imageResolvers = { const imageResolvers = {
imageID: (image) => image.id, imageID: image => image.id,
url: (image, args) => { url: (image, args) => {
return args.size === 'PREVIEW' return args.size === 'PREVIEW'
? image.url.replace('/fanart/', '/preview/') ? image.url.replace('/fanart/', '/preview/')
: image.url; : image.url
}, },
likeCount: (image) => image.likes, likeCount: image => image.likes
}; }
export default { export default {
FanArtImage: { FanArtImage: {
...imageResolvers, ...imageResolvers
}, },
FanArtDiscImage: { FanArtDiscImage: {
...imageResolvers, ...imageResolvers,
discNumber: (image) => image.disc, discNumber: image => image.disc
}, },
FanArtLabelImage: { FanArtLabelImage: {
...imageResolvers, ...imageResolvers,
color: (image) => image.colour, color: image => image.colour
}, },
FanArtArtist: { FanArtArtist: {
backgrounds: (artist) => { backgrounds: artist => {
return artist.artistbackground || []; return artist.artistbackground
}, },
thumbnails: (artist) => { thumbnails: artist => {
return artist.artistthumb || []; return artist.artistthumb
}, },
logos: (artist) => { logos: artist => {
return artist.musiclogo || []; return artist.musiclogo
}, },
logosHD: (artist) => { logosHD: artist => {
return artist.hdmusiclogo || []; return artist.hdmusiclogo
},
banners: (artist) => {
return artist.musicbanner || [];
}, },
banners: artist => {
return artist.musicbanner
}
}, },
FanArtLabel: { FanArtLabel: {
logos: (label) => label.musiclabel || [], logos: label => label.musiclabel
}, },
FanArtAlbum: { FanArtAlbum: {
albumCovers: (album) => album.albumcover || [], albumCovers: album => album.albumcover || [],
discImages: (album) => album.cdart || [], discImages: album => album.cdart || []
}, },
Artist: { Artist: {
fanArt: (artist, args, context) => { fanArt: (artist, args, context) => {
return context.loaders.fanArt.load(['artist', artist.id]); return context.loaders.fanArt.load(['artist', artist.id])
}, }
}, },
Label: { Label: {
fanArt: (label, args, context) => { fanArt: (label, args, context) => {
return context.loaders.fanArt.load(['label', label.id]); return context.loaders.fanArt.load(['label', label.id])
}, }
}, },
ReleaseGroup: { ReleaseGroup: {
fanArt: (releaseGroup, args, context) => { fanArt: (releaseGroup, args, context) => {
return context.loaders.fanArt return context.loaders.fanArt
.load(['release-group', releaseGroup.id]) .load(['release-group', releaseGroup.id])
.then((artist) => artist.albums[releaseGroup.id]); .then(artist => artist.albums[releaseGroup.id])
}, }
}, }
}; }

View file

@ -1,197 +1,127 @@
import gql from '../../tag.js'; import gql from '../../tag'
export default gql` export default gql`
""" # The image sizes that may be requested at [fanart.tv](https://fanart.tv/).
The image sizes that may be requested at [fanart.tv](https://fanart.tv/).
"""
enum FanArtImageSize { enum FanArtImageSize {
""" # The images full original dimensions.
The images full original dimensions.
"""
FULL FULL
""" # A maximum dimension of 200px.
A maximum dimension of 200px.
"""
PREVIEW PREVIEW
} }
""" # A single image from [fanart.tv](https://fanart.tv/).
A single image from [fanart.tv](https://fanart.tv/).
"""
type FanArtImage { type FanArtImage {
""" # The ID of the image on fanart.tv.
The ID of the image on fanart.tv.
"""
imageID: ID imageID: ID
""" # The URL of the image.
The URL of the image.
"""
url( url(
""" # The size of the image to retrieve.
The size of the image to retrieve.
"""
size: FanArtImageSize = FULL size: FanArtImageSize = FULL
): URLString ): URLString
""" # The number of likes the image has received by fanart.tv users.
The number of likes the image has received by fanart.tv users.
"""
likeCount: Int likeCount: Int
} }
""" # A disc image from [fanart.tv](https://fanart.tv/).
A disc image from [fanart.tv](https://fanart.tv/).
"""
type FanArtDiscImage { type FanArtDiscImage {
""" # The ID of the image on fanart.tv.
The ID of the image on fanart.tv.
"""
imageID: ID imageID: ID
""" # The URL of the image.
The URL of the image.
"""
url( url(
""" # The size of the image to retrieve.
The size of the image to retrieve.
"""
size: FanArtImageSize = FULL size: FanArtImageSize = FULL
): URLString ): URLString
""" # The number of likes the image has received by fanart.tv users.
The number of likes the image has received by fanart.tv users.
"""
likeCount: Int likeCount: Int
""" # The disc number.
The disc number.
"""
discNumber: Int discNumber: Int
""" # The width and height of the (square) disc image.
The width and height of the (square) disc image.
"""
size: Int size: Int
} }
""" # A music label image from [fanart.tv](https://fanart.tv/).
A music label image from [fanart.tv](https://fanart.tv/).
"""
type FanArtLabelImage { type FanArtLabelImage {
""" # The ID of the image on fanart.tv.
The ID of the image on fanart.tv.
"""
imageID: ID imageID: ID
""" # The URL of the image.
The URL of the image.
"""
url( url(
""" # The size of the image to retrieve.
The size of the image to retrieve.
"""
size: FanArtImageSize = FULL size: FanArtImageSize = FULL
): URLString ): URLString
""" # The number of likes the image has received by fanart.tv users.
The number of likes the image has received by fanart.tv users.
"""
likeCount: Int likeCount: Int
""" # The type of color content in the image (usually white or colour).
The type of color content in the image (usually white or colour).
"""
color: String color: String
} }
""" # An object containing lists of the different types of artist images from
An object containing lists of the different types of artist images from # [fanart.tv](https://fanart.tv/).
[fanart.tv](https://fanart.tv/).
"""
type FanArtArtist { type FanArtArtist {
""" # A list of 1920x1080 JPG images picturing the artist, suitable for use as
A list of 1920x1080 JPG images picturing the artist, suitable for use as # backgrounds.
backgrounds.
"""
backgrounds: [FanArtImage] backgrounds: [FanArtImage]
""" # A list of 1000x185 JPG images containing the artist and their logo or name.
A list of 1000x185 JPG images containing the artist and their logo or name.
"""
banners: [FanArtImage] banners: [FanArtImage]
""" # A list of 400x155 PNG images containing the artists logo or name, with
A list of 400x155 PNG images containing the artists logo or name, with # transparent backgrounds.
transparent backgrounds.
"""
logos: [FanArtImage] logos: [FanArtImage]
""" # A list of 800x310 PNG images containing the artists logo or name, with
A list of 800x310 PNG images containing the artists logo or name, with # transparent backgrounds.
transparent backgrounds.
"""
logosHD: [FanArtImage] logosHD: [FanArtImage]
""" # A list of 1000x1000 JPG thumbnail images picturing the artist (usually
A list of 1000x1000 JPG thumbnail images picturing the artist (usually # containing every member of a band).
containing every member of a band).
"""
thumbnails: [FanArtImage] thumbnails: [FanArtImage]
} }
""" # An object containing lists of the different types of label images from
An object containing lists of the different types of label images from # [fanart.tv](https://fanart.tv/).
[fanart.tv](https://fanart.tv/).
"""
type FanArtLabel { type FanArtLabel {
""" # A list of 400x270 PNG images containing the labels logo. There will
A list of 400x270 PNG images containing the labels logo. There will # usually be a black version, a color version, and a white version, all with
usually be a black version, a color version, and a white version, all with # transparent backgrounds.
transparent backgrounds.
"""
logos: [FanArtLabelImage] logos: [FanArtLabelImage]
} }
""" # An object containing lists of the different types of release group images from
An object containing lists of the different types of release group images from # [fanart.tv](https://fanart.tv/).
[fanart.tv](https://fanart.tv/).
"""
type FanArtAlbum { type FanArtAlbum {
""" # A list of 1000x1000 JPG images of the cover artwork of the release group.
A list of 1000x1000 JPG images of the cover artwork of the release group.
"""
albumCovers: [FanArtImage] albumCovers: [FanArtImage]
""" # A list of 1000x1000 PNG images of the physical disc media for the release
A list of 1000x1000 PNG images of the physical disc media for the release # group, with transparent backgrounds.
group, with transparent backgrounds.
"""
discImages: [FanArtDiscImage] discImages: [FanArtDiscImage]
} }
extend type Artist { extend type Artist {
""" # Images of the artist from [fanart.tv](https://fanart.tv/).
Images of the artist from [fanart.tv](https://fanart.tv/). # This field is provided by the fanart.tv extension.
This field is provided by the fanart.tv extension.
"""
fanArt: FanArtArtist fanArt: FanArtArtist
} }
extend type Label { extend type Label {
""" # Images of the label from [fanart.tv](https://fanart.tv/).
Images of the label from [fanart.tv](https://fanart.tv/). # This field is provided by the fanart.tv extension.
This field is provided by the fanart.tv extension.
"""
fanArt: FanArtLabel fanArt: FanArtLabel
} }
extend type ReleaseGroup { extend type ReleaseGroup {
""" # Images of the release group from [fanart.tv](https://fanart.tv/).
Images of the release group from [fanart.tv](https://fanart.tv/). # This field is provided by the fanart.tv extension.
This field is provided by the fanart.tv extension.
"""
fanArt: FanArtAlbum fanArt: FanArtAlbum
} }
`; `

View file

@ -1,18 +1,18 @@
export async function loadExtension(extensionModule) { export function loadExtension(extensionModule) {
let extension; let extension
if (typeof extensionModule === 'string') { if (typeof extensionModule === 'string') {
extension = await import(extensionModule); extension = require(extensionModule)
} else { } else {
extension = extensionModule; extension = extensionModule
} }
if (extension == null || typeof extension !== 'object') { if (extension == null || typeof extension !== 'object') {
throw new Error( throw new Error(
`Expected ${extensionModule} to export an extension but instead ` + `Expected ${extensionModule} to export an extension but instead ` +
`got: ${extension}` `got: ${extension}`
); )
} else if (extension.default) { } else if (extension.default) {
// ECMAScript module interop. // ECMAScript module interop.
extension = extension.default; extension = extension.default
} }
return extension; return extension
} }

View file

@ -1,54 +1,51 @@
import ExtendableError from 'es6-error'; import URL from 'url'
import Client from '../../api/client.js'; import Client from '../../api/client'
export class MediaWikiError extends ExtendableError {}
export default class MediaWikiClient extends Client { export default class MediaWikiClient extends Client {
constructor({ limit = 10, period = 1000, ...options } = {}) { constructor({ limit = 10, period = 1000, ...options } = {}) {
super({ limit, period, ...options }); super({ limit, period, ...options })
} }
imageInfo(page) { imageInfo(page) {
const pageURL = new URL(page); const pageURL = URL.parse(page, true)
const ClientError = this.errorClass
if (!pageURL.pathname.startsWith('/wiki/')) { if (!pageURL.pathname.startsWith('/wiki/')) {
return Promise.reject( return Promise.reject(
new MediaWikiError( new ClientError(
`MediaWiki page URL does not have the expected /wiki/ prefix: ${page}` `MediaWiki page URL does not have the expected /wiki/ prefix: ${page}`
) )
); )
} }
const apiURL = new URL('/w/api.php', pageURL); const apiURL = URL.format({
apiURL.search = new URLSearchParams({ protocol: pageURL.protocol,
action: 'query', auth: pageURL.auth,
titles: decodeURI(pageURL.pathname.slice(6)), host: pageURL.host,
prop: 'imageinfo', pathname: '/w/api.php',
iiprop: 'url|size|canonicaltitle|user|extmetadata', query: {
format: 'json', action: 'query',
}).toString(); titles: pageURL.pathname.slice(6),
prop: 'imageinfo',
return this.get(apiURL.toString(), { resolveBodyOnly: true }).then( iiprop: 'url|size|canonicaltitle|user|extmetadata',
(body) => { format: 'json'
const pageIDs = Object.keys(body.query.pages);
if (pageIDs.length !== 1) {
throw new MediaWikiError(
`Query returned multiple pages: [${pageIDs.join(', ')}]`
);
}
if (pageIDs[0] === '-1') {
throw new MediaWikiError(
body.query.pages['-1'].invalidreason || 'Unknown error'
);
}
const imageInfo = body.query.pages[pageIDs[0]].imageinfo;
if (imageInfo.length !== 1) {
throw new MediaWikiError(
`Query returned info for ${imageInfo.length} images, expected 1.`
);
}
return imageInfo[0];
} }
); })
return this.get(apiURL, { json: true }).then(body => {
const pageIDs = Object.keys(body.query.pages)
if (pageIDs.length !== 1) {
throw new ClientError(
`Query returned multiple pages: [${pageIDs.join(', ')}]`
)
}
const imageInfo = body.query.pages[pageIDs[0]].imageinfo
if (imageInfo.length !== 1) {
throw new ClientError(
`Query returned info for ${imageInfo.length} images, expected 1.`
)
}
return imageInfo[0]
})
} }
} }

View file

@ -1,37 +1,37 @@
import schema from './schema.js'; import schema from './schema'
import resolvers from './resolvers.js'; import resolvers from './resolvers'
import createLoader from './loader.js'; import createLoader from './loader'
import MediaWikiClient from './client.js'; import MediaWikiClient from './client'
import { ONE_DAY } from '../../util.js'; import { ONE_DAY } from '../../util'
export default { export default {
name: 'MediaWiki', name: 'MediaWiki',
description: `Retrieve information from MediaWiki image pages, like the actual description: `Retrieve information from MediaWiki image pages, like the actual
image file URL and EXIF metadata.`, image file URL and EXIF metadata.`,
extendContext(context, { mediaWiki = {} } = {}) { extendContext(context, { mediaWiki = {} } = {}) {
const client = new MediaWikiClient(mediaWiki); const client = new MediaWikiClient(mediaWiki)
const cacheSize = parseInt( const cacheSize = parseInt(
process.env.MEDIAWIKI_CACHE_SIZE || process.env.MEDIAWIKI_CACHE_SIZE ||
process.env.GRAPHBRAINZ_CACHE_SIZE || process.env.GRAPHBRAINZ_CACHE_SIZE ||
8192, 8192,
10 10
); )
const cacheTTL = parseInt( const cacheTTL = parseInt(
process.env.MEDIAWIKI_CACHE_TTL || process.env.MEDIAWIKI_CACHE_TTL ||
process.env.GRAPHBRAINZ_CACHE_TTL || process.env.GRAPHBRAINZ_CACHE_TTL ||
ONE_DAY, ONE_DAY,
10 10
); )
return { return {
...context, ...context,
loaders: { loaders: {
...context.loaders, ...context.loaders,
mediaWiki: createLoader({ client, cacheSize, cacheTTL }), mediaWiki: createLoader({ client, cacheSize, cacheTTL })
}, }
}; }
}, },
extendSchema: { extendSchema: {
schemas: [schema], schemas: [schema],
resolvers, resolvers
}, }
}; }

View file

@ -1,30 +1,25 @@
import createDebug from 'debug'; import DataLoader from 'dataloader'
import DataLoader from 'dataloader'; import LRUCache from 'lru-cache'
import LRUCache from 'lru-cache';
const debug = createDebug('graphbrainz:extensions/mediawiki'); const debug = require('debug')('graphbrainz:extensions/mediawiki')
export default function createLoader(options) { export default function createLoader(options) {
const { client } = options; const { client } = options
const cache = new LRUCache({ const cache = LRUCache({
max: options.cacheSize, max: options.cacheSize,
maxAge: options.cacheTTL, maxAge: options.cacheTTL,
dispose(key) { dispose(key) {
debug(`Removed from cache. key=${key}`); debug(`Removed from cache. key=${key}`)
}, }
}); })
// Make the cache Map-like. // Make the cache Map-like.
cache.delete = cache.del; cache.delete = cache.del
cache.clear = cache.reset; cache.clear = cache.reset
return new DataLoader( return new DataLoader(
(keys) => { keys => {
return Promise.allSettled( return Promise.all(keys.map(key => client.imageInfo(key)))
keys.map((key) => client.imageInfo(key))
).then((results) =>
results.map((result) => result.reason || result.value)
);
}, },
{ batch: false, cacheMap: cache } { cacheMap: cache }
); )
} }

View file

@ -1,80 +1,84 @@
async function resolveMediaWikiImages(source, args, { loaders }) { import URL from 'url'
const isURL = (relation) => relation['target-type'] === 'url';
let rels = source.relations ? source.relations.filter(isURL) : []; function resolveMediaWikiImages(source, args, { loaders }) {
const isURL = relation => relation['target-type'] === 'url'
let rels = source.relations ? source.relations.filter(isURL) : []
if (!rels.length) { if (!rels.length) {
rels = await loaders.lookup rels = loaders.lookup
.load([source._type, source.id, { inc: 'url-rels' }]) .load([source._type, source.id, { inc: 'url-rels' }])
.then((source) => source.relations.filter(isURL)); .then(source => source.relations.filter(isURL))
} }
const pages = rels return Promise.resolve(rels).then(rels => {
.filter((rel) => { const pages = rels
if (rel.type === args.type) { .filter(rel => {
const url = new URL(rel.url.resource); if (rel.type === args.type) {
if (url.pathname.match(/^\/wiki\/(File|Image):/)) { const url = URL.parse(rel.url.resource)
return true; if (url.pathname.match(/^\/wiki\/(File|Image):/)) {
return true
}
} }
} return false
return false; })
}) .map(rel => rel.url.resource)
.map((rel) => rel.url.resource); return loaders.mediaWiki.loadMany(pages)
return Promise.all(pages.map((page) => loaders.mediaWiki.load(page))); })
} }
export default { export default {
MediaWikiImage: { MediaWikiImage: {
descriptionURL: (imageInfo) => imageInfo.descriptionurl, descriptionURL: imageInfo => imageInfo.descriptionurl,
canonicalTitle: (imageInfo) => imageInfo.canonicaltitle, canonicalTitle: imageInfo => imageInfo.canonicaltitle,
objectName: (imageInfo) => { objectName: imageInfo => {
const data = imageInfo.extmetadata.ObjectName; const data = imageInfo.extmetadata.ObjectName
return data ? data.value : null; return data ? data.value : null
}, },
descriptionHTML: (imageInfo) => { descriptionHTML: imageInfo => {
const data = imageInfo.extmetadata.ImageDescription; const data = imageInfo.extmetadata.ImageDescription
return data ? data.value : null; return data ? data.value : null
}, },
originalDateTimeHTML: (imageInfo) => { originalDateTimeHTML: imageInfo => {
const data = imageInfo.extmetadata.DateTimeOriginal; const data = imageInfo.extmetadata.DateTimeOriginal
return data ? data.value : null; return data ? data.value : null
}, },
categories: (imageInfo) => { categories: imageInfo => {
const data = imageInfo.extmetadata.Categories; const data = imageInfo.extmetadata.Categories
return data ? data.value.split('|') : []; return data ? data.value.split('|') : []
}, },
artistHTML: (imageInfo) => { artistHTML: imageInfo => {
const data = imageInfo.extmetadata.Artist; const data = imageInfo.extmetadata.Artist
return data ? data.value : null; return data ? data.value : null
}, },
creditHTML: (imageInfo) => { creditHTML: imageInfo => {
const data = imageInfo.extmetadata.Credit; const data = imageInfo.extmetadata.Credit
return data ? data.value : null; return data ? data.value : null
}, },
licenseShortName: (imageInfo) => { licenseShortName: imageInfo => {
const data = imageInfo.extmetadata.LicenseShortName; const data = imageInfo.extmetadata.LicenseShortName
return data ? data.value : null; return data ? data.value : null
}, },
licenseURL: (imageInfo) => { licenseURL: imageInfo => {
const data = imageInfo.extmetadata.LicenseUrl; const data = imageInfo.extmetadata.LicenseUrl
return data ? data.value : null; return data ? data.value : null
}, },
metadata: (imageInfo) => metadata: imageInfo =>
Object.keys(imageInfo.extmetadata).map((key) => { Object.keys(imageInfo.extmetadata).map(key => {
const data = imageInfo.extmetadata[key]; const data = imageInfo.extmetadata[key]
return { ...data, name: key }; return { ...data, name: key }
}), })
}, },
MediaWikiImageMetadata: { MediaWikiImageMetadata: {
value: (obj) => (obj.value == null ? obj.value : `${obj.value}`), value: obj => (obj.value == null ? obj.value : `${obj.value}`)
}, },
Artist: { Artist: {
mediaWikiImages: resolveMediaWikiImages, mediaWikiImages: resolveMediaWikiImages
}, },
Instrument: { Instrument: {
mediaWikiImages: resolveMediaWikiImages, mediaWikiImages: resolveMediaWikiImages
}, },
Label: { Label: {
mediaWikiImages: resolveMediaWikiImages, mediaWikiImages: resolveMediaWikiImages
}, },
Place: { Place: {
mediaWikiImages: resolveMediaWikiImages, mediaWikiImages: resolveMediaWikiImages
}, }
}; }

View file

@ -1,168 +1,110 @@
import gql from '../../tag.js'; import gql from '../../tag'
export default gql` export default gql`
""" # An object describing various properties of an image stored on a MediaWiki
An object describing various properties of an image stored on a MediaWiki # server. The information comes the [MediaWiki imageinfo API](https://www.mediawiki.org/wiki/API:Imageinfo).
server. The information comes the [MediaWiki imageinfo API](https://www.mediawiki.org/wiki/API:Imageinfo).
"""
type MediaWikiImage { type MediaWikiImage {
""" # The URL of the actual image file.
The URL of the actual image file.
"""
url: URLString! url: URLString!
""" # The URL of the wiki page describing the image.
The URL of the wiki page describing the image.
"""
descriptionURL: URLString descriptionURL: URLString
""" # The user who uploaded the file.
The user who uploaded the file.
"""
user: String user: String
""" # The size of the file in bytes.
The size of the file in bytes.
"""
size: Int size: Int
""" # The pixel width of the image.
The pixel width of the image.
"""
width: Int width: Int
""" # The pixel height of the image.
The pixel height of the image.
"""
height: Int height: Int
""" # The canonical title of the file.
The canonical title of the file.
"""
canonicalTitle: String canonicalTitle: String
""" # The image title, brief description, or file name.
The image title, brief description, or file name.
"""
objectName: String objectName: String
""" # A description of the image, potentially containing HTML.
A description of the image, potentially containing HTML.
"""
descriptionHTML: String descriptionHTML: String
""" # The original date of creation of the image. May be a description rather than
The original date of creation of the image. May be a description rather than # a parseable timestamp, and may contain HTML.
a parseable timestamp, and may contain HTML.
"""
originalDateTimeHTML: String originalDateTimeHTML: String
""" # A list of the categories of the image.
A list of the categories of the image.
"""
categories: [String]! categories: [String]!
""" # The name of the image author, potentially containing HTML.
The name of the image author, potentially containing HTML.
"""
artistHTML: String artistHTML: String
""" # The source of the image, potentially containing HTML.
The source of the image, potentially containing HTML.
"""
creditHTML: String creditHTML: String
""" # A short human-readable license name.
A short human-readable license name.
"""
licenseShortName: String licenseShortName: String
""" # A web address where the license is described.
A web address where the license is described.
"""
licenseURL: URLString licenseURL: URLString
""" # The full list of values in the \`extmetadata\` field.
The full list of values in the \`extmetadata\` field.
"""
metadata: [MediaWikiImageMetadata]! metadata: [MediaWikiImageMetadata]!
} }
""" # An entry in the \`extmetadata\` field of a MediaWiki image file.
An entry in the \`extmetadata\` field of a MediaWiki image file.
"""
type MediaWikiImageMetadata { type MediaWikiImageMetadata {
""" # The name of the metadata field.
The name of the metadata field.
"""
name: String! name: String!
""" # The value of the metadata field. All values will be converted to strings.
The value of the metadata field. All values will be converted to strings.
"""
value: String value: String
""" # The source of the value.
The source of the value.
"""
source: String source: String
} }
extend type Artist { extend type Artist {
""" # Artist images found at MediaWiki URLs in the artists URL relationships.
Artist images found at MediaWiki URLs in the artists URL relationships. # Defaults to URL relationships with the type image.
Defaults to URL relationships with the type image. # This field is provided by the MediaWiki extension.
This field is provided by the MediaWiki extension.
"""
mediaWikiImages( mediaWikiImages(
""" # The type of URL relationship that will be selected to find images. See
The type of URL relationship that will be selected to find images. See # the possible [Artist-URL relationship types](https://musicbrainz.org/relationships/artist-url).
the possible [Artist-URL relationship types](https://musicbrainz.org/relationships/artist-url).
"""
type: String = "image" type: String = "image"
): [MediaWikiImage]! ): [MediaWikiImage]!
} }
extend type Instrument { extend type Instrument {
""" # Instrument images found at MediaWiki URLs in the instruments URL
Instrument images found at MediaWiki URLs in the instruments URL # relationships. Defaults to URL relationships with the type image.
relationships. Defaults to URL relationships with the type image. # This field is provided by the MediaWiki extension.
This field is provided by the MediaWiki extension.
"""
mediaWikiImages( mediaWikiImages(
""" # The type of URL relationship that will be selected to find images. See the
The type of URL relationship that will be selected to find images. See the # possible [Instrument-URL relationship types](https://musicbrainz.org/relationships/instrument-url).
possible [Instrument-URL relationship types](https://musicbrainz.org/relationships/instrument-url).
"""
type: String = "image" type: String = "image"
): [MediaWikiImage]! ): [MediaWikiImage]!
} }
extend type Label { extend type Label {
""" # Label images found at MediaWiki URLs in the labels URL relationships.
Label images found at MediaWiki URLs in the labels URL relationships. # Defaults to URL relationships with the type logo.
Defaults to URL relationships with the type logo. # This field is provided by the MediaWiki extension.
This field is provided by the MediaWiki extension.
"""
mediaWikiImages( mediaWikiImages(
""" # The type of URL relationship that will be selected to find images. See the
The type of URL relationship that will be selected to find images. See the # possible [Label-URL relationship types](https://musicbrainz.org/relationships/label-url).
possible [Label-URL relationship types](https://musicbrainz.org/relationships/label-url).
"""
type: String = "logo" type: String = "logo"
): [MediaWikiImage]! ): [MediaWikiImage]!
} }
extend type Place { extend type Place {
""" # Place images found at MediaWiki URLs in the places URL relationships.
Place images found at MediaWiki URLs in the places URL relationships. # Defaults to URL relationships with the type image.
Defaults to URL relationships with the type image. # This field is provided by the MediaWiki extension.
This field is provided by the MediaWiki extension.
"""
mediaWikiImages( mediaWikiImages(
""" # The type of URL relationship that will be selected to find images. See the
The type of URL relationship that will be selected to find images. See the # possible [Place-URL relationship types](https://musicbrainz.org/relationships/place-url).
possible [Place-URL relationship types](https://musicbrainz.org/relationships/place-url).
"""
type: String = "image" type: String = "image"
): [MediaWikiImage]! ): [MediaWikiImage]!
} }
`; `

View file

@ -1,78 +1,70 @@
import ExtendableError from 'es6-error'; import Client from '../../api/client'
import Client from '../../api/client.js';
export class TheAudioDBError extends ExtendableError {}
export default class TheAudioDBClient extends Client { export default class TheAudioDBClient extends Client {
constructor({ constructor(
apiKey = process.env.THEAUDIODB_API_KEY, {
baseURL = process.env.THEAUDIODB_BASE_URL || apiKey = process.env.THEAUDIODB_API_KEY,
'https://www.theaudiodb.com/api/v1/json/', baseURL = process.env.THEAUDIODB_BASE_URL ||
limit = 10, 'http://www.theaudiodb.com/api/v1/json/',
period = 1000, limit = 10,
...options period = 1000,
} = {}) { ...options
super({ baseURL, limit, period, ...options }); } = {}
this.apiKey = apiKey; ) {
super({ baseURL, limit, period, ...options })
this.apiKey = apiKey
} }
get(path, options = {}) { get(path, options = {}) {
const ClientError = this.errorClass
if (!this.apiKey) { if (!this.apiKey) {
return Promise.reject( return Promise.reject(
new TheAudioDBError('No API key was configured for TheAudioDB client.') new ClientError('No API key was configured for TheAudioDB client.')
); )
} }
return super.get(`${this.apiKey}/${path}`, { return super.get(`${this.apiKey}/${path}`, { json: true, ...options })
resolveBodyOnly: true,
...options,
});
} }
entity(entityType, mbid) { entity(entityType, mbid) {
const ClientError = this.errorClass
switch (entityType) { switch (entityType) {
case 'artist': case 'artist':
return this.artist(mbid); return this.artist(mbid)
case 'release-group': case 'release-group':
return this.album(mbid); return this.album(mbid)
case 'recording': case 'recording':
return this.track(mbid); return this.track(mbid)
default: default:
return Promise.reject( return Promise.reject(
new TheAudioDBError(`Entity type unsupported: ${entityType}`) new ClientError(`Entity type unsupported: ${entityType}`)
); )
} }
} }
artist(mbid) { artist(mbid) {
return this.get('artist-mb.php', { searchParams: { i: mbid } }).then( return this.get('artist-mb.php', { qs: { i: mbid } }).then(body => {
(body) => { if (body.artists && body.artists.length === 1) {
if (body.artists && body.artists.length === 1) { return body.artists[0]
return body.artists[0];
}
return null;
} }
); return null
})
} }
album(mbid) { album(mbid) {
return this.get('album-mb.php', { searchParams: { i: mbid } }).then( return this.get('album-mb.php', { qs: { i: mbid } }).then(body => {
(body) => { if (body.album && body.album.length === 1) {
if (body.album && body.album.length === 1) { return body.album[0]
return body.album[0];
}
return null;
} }
); return null
})
} }
track(mbid) { track(mbid) {
return this.get('track-mb.php', { searchParams: { i: mbid } }).then( return this.get('track-mb.php', { qs: { i: mbid } }).then(body => {
(body) => { if (body.track && body.track.length === 1) {
if (body.track && body.track.length === 1) { return body.track[0]
return body.track[0];
}
return null;
} }
); return null
})
} }
} }

View file

@ -1,37 +1,37 @@
import schema from './schema.js'; import schema from './schema'
import resolvers from './resolvers.js'; import resolvers from './resolvers'
import createLoader from './loader.js'; import createLoader from './loader'
import TheAudioDBClient from './client.js'; import TheAudioDBClient from './client'
import { ONE_DAY } from '../../util.js'; import { ONE_DAY } from '../../util'
export default { export default {
name: 'TheAudioDB', name: 'TheAudioDB',
description: `Retrieve images and information about artists, releases, and description: `Retrieve images and information about artists, releases, and
recordings from [TheAudioDB.com](http://www.theaudiodb.com/).`, recordings from [TheAudioDB.com](http://www.theaudiodb.com/).`,
extendContext(context, { theAudioDB = {} } = {}) { extendContext(context, { theAudioDB = {} } = {}) {
const client = new TheAudioDBClient(theAudioDB); const client = new TheAudioDBClient(theAudioDB)
const cacheSize = parseInt( const cacheSize = parseInt(
process.env.THEAUDIODB_CACHE_SIZE || process.env.THEAUDIODB_CACHE_SIZE ||
process.env.GRAPHBRAINZ_CACHE_SIZE || process.env.GRAPHBRAINZ_CACHE_SIZE ||
8192, 8192,
10 10
); )
const cacheTTL = parseInt( const cacheTTL = parseInt(
process.env.THEAUDIODB_CACHE_TTL || process.env.THEAUDIODB_CACHE_TTL ||
process.env.GRAPHBRAINZ_CACHE_TTL || process.env.GRAPHBRAINZ_CACHE_TTL ||
ONE_DAY, ONE_DAY,
10 10
); )
return { return {
...context, ...context,
loaders: { loaders: {
...context.loaders, ...context.loaders,
theAudioDB: createLoader({ client, cacheSize, cacheTTL }), theAudioDB: createLoader({ client, cacheSize, cacheTTL })
}, }
}; }
}, },
extendSchema: { extendSchema: {
schemas: [schema], schemas: [schema],
resolvers, resolvers
}, }
}; }

View file

@ -1,35 +1,33 @@
import createDebug from 'debug'; import DataLoader from 'dataloader'
import DataLoader from 'dataloader'; import LRUCache from 'lru-cache'
import LRUCache from 'lru-cache';
const debug = createDebug('graphbrainz:extensions/the-audio-db'); const debug = require('debug')('graphbrainz:extensions/the-audio-db')
export default function createLoader(options) { export default function createLoader(options) {
const { client } = options; const { client } = options
const cache = new LRUCache({ const cache = LRUCache({
max: options.cacheSize, max: options.cacheSize,
maxAge: options.cacheTTL, maxAge: options.cacheTTL,
dispose(key) { dispose(key) {
debug(`Removed from cache. key=${key}`); debug(`Removed from cache. key=${key}`)
}, }
}); })
// Make the cache Map-like. // Make the cache Map-like.
cache.delete = cache.del; cache.delete = cache.del
cache.clear = cache.reset; cache.clear = cache.reset
return new DataLoader( return new DataLoader(
(keys) => { keys => {
return Promise.all( return Promise.all(
keys.map((key) => { keys.map(key => {
const [entityType, id] = key; const [entityType, id] = key
return client.entity(entityType, id); return client.entity(entityType, id)
}) })
); )
}, },
{ {
batch: false,
cacheKeyFn: ([entityType, id]) => `${entityType}/${id}`, cacheKeyFn: ([entityType, id]) => `${entityType}/${id}`,
cacheMap: cache, cacheMap: cache
} }
); )
} }

View file

@ -1,118 +1,107 @@
function handleImageSize(resolver) { function handleImageSize(resolver) {
return (source, args, context, info) => { return (source, args, context, info) => {
const getURL = (url) => (args.size === 'PREVIEW' ? `${url}/preview` : url); const getURL = url => (args.size === 'PREVIEW' ? `${url}/preview` : url)
const url = resolver(source, args, context, info); const url = resolver(source, args, context, info)
if (!url) { if (!url) {
return null; return null
} else if (Array.isArray(url)) { } else if (Array.isArray(url)) {
return url.map(getURL); return url.map(getURL)
} else { } else {
return getURL(url); return getURL(url)
} }
}; }
} }
export default { export default {
TheAudioDBArtist: { TheAudioDBArtist: {
artistID: (artist) => artist.idArtist, artistID: artist => artist.idArtist,
biography: (artist, args) => { biography: (artist, args) => {
const lang = args.lang.toUpperCase(); const lang = args.lang.toUpperCase()
return artist[`strBiography${lang}`] || null; return artist[`strBiography${lang}`] || null
}, },
memberCount: (artist) => artist.intMembers, memberCount: artist => artist.intMembers,
banner: handleImageSize((artist) => artist.strArtistBanner), banner: handleImageSize(artist => artist.strArtistBanner),
fanArt: handleImageSize((artist) => { fanArt: handleImageSize(artist => {
return [ return [
artist.strArtistFanart, artist.strArtistFanart,
artist.strArtistFanart2, artist.strArtistFanart2,
artist.strArtistFanart3, artist.strArtistFanart3
].filter(Boolean); ].filter(Boolean)
}), }),
logo: handleImageSize((artist) => artist.strArtistLogo), logo: handleImageSize(artist => artist.strArtistLogo),
thumbnail: handleImageSize((artist) => artist.strArtistThumb), thumbnail: handleImageSize(artist => artist.strArtistThumb),
genre: (artist) => artist.strGenre || null, genre: artist => artist.strGenre || null,
mood: (artist) => artist.strMood || null, mood: artist => artist.strMood || null,
style: (artist) => artist.strStyle || null, style: artist => artist.strStyle || null
}, },
TheAudioDBAlbum: { TheAudioDBAlbum: {
albumID: (album) => album.idAlbum, albumID: album => album.idAlbum,
artistID: (album) => album.idArtist, artistID: album => album.idArtist,
description: (album, args) => { description: (album, args) => {
const lang = args.lang.toUpperCase(); const lang = args.lang.toUpperCase()
return album[`strDescription${lang}`] || null; return album[`strDescription${lang}`] || null
}, },
salesCount: (album) => album.intSales, salesCount: album => album.intSales,
score: (album) => album.intScore, score: album => album.intScore,
scoreVotes: (album) => album.intScoreVotes, scoreVotes: album => album.intScoreVotes,
discImage: handleImageSize((album) => album.strAlbumCDart), discImage: handleImageSize(album => album.strAlbumCDart),
spineImage: handleImageSize((album) => album.strAlbumSpine), spineImage: handleImageSize(album => album.strAlbumSpine),
frontImage: handleImageSize((album) => album.strAlbumThumb), frontImage: handleImageSize(album => album.strAlbumThumb),
backImage: handleImageSize((album) => album.strAlbumThumbBack), backImage: handleImageSize(album => album.strAlbumThumbBack),
review: (album) => album.strReview || null, review: album => album.strReview || null,
genre: (album) => album.strGenre || null, genre: album => album.strGenre || null,
mood: (album) => album.strMood || null, mood: album => album.strMood || null,
style: (album) => album.strStyle || null, style: album => album.strStyle || null,
speed: (album) => album.strSpeed || null, speed: album => album.strSpeed || null,
theme: (album) => album.strTheme || null, theme: album => album.strTheme || null
}, },
TheAudioDBTrack: { TheAudioDBTrack: {
trackID: (track) => track.idTrack, trackID: track => track.idTrack,
albumID: (track) => track.idAlbum, albumID: track => track.idAlbum,
artistID: (track) => track.idArtist, artistID: track => track.idArtist,
description: (track, args) => { description: (track, args) => {
const lang = args.lang.toUpperCase(); const lang = args.lang.toUpperCase()
return track[`strDescription${lang}`] || null; return track[`strDescription${lang}`] || null
}, },
thumbnail: handleImageSize((track) => track.strTrackThumb), thumbnail: handleImageSize(track => track.strTrackThumb),
score: (track) => track.intScore, score: track => track.intScore,
scoreVotes: (track) => track.intScoreVotes, scoreVotes: track => track.intScoreVotes,
trackNumber: (track) => track.intTrackNumber, trackNumber: track => track.intTrackNumber,
musicVideo: (track) => track, musicVideo: track => track,
genre: (track) => track.strGenre || null, genre: track => track.strGenre || null,
mood: (track) => track.strMood || null, mood: track => track.strMood || null,
style: (track) => track.strStyle || null, style: track => track.strStyle || null,
theme: (track) => track.strTheme || null, theme: track => track.strTheme || null
}, },
TheAudioDBMusicVideo: { TheAudioDBMusicVideo: {
url: (track) => { url: track => track.strMusicVid || null,
let url = track.strMusicVid || null; companyName: track => track.strMusicVidCompany || null,
// Many of these are missing the protocol and start with www, so add it directorName: track => track.strMusicVidDirector || null,
// in that case. screenshots: handleImageSize(track => {
if (url && url.startsWith('www.')) {
url = `https://${url}`;
}
return url;
},
companyName: (track) => track.strMusicVidCompany || null,
directorName: (track) => track.strMusicVidDirector || null,
screenshots: handleImageSize((track) => {
return [ return [
track.strMusicVidScreen1, track.strMusicVidScreen1,
track.strMusicVidScreen2, track.strMusicVidScreen2,
track.strMusicVidScreen3, track.strMusicVidScreen3
].filter(Boolean); ].filter(Boolean)
}), }),
viewCount: (track) => track.intMusicVidViews, viewCount: track => track.intMusicVidViews,
likeCount: (track) => track.intMusicVidLikes, likeCount: track => track.intMusicVidLikes,
dislikeCount: (track) => track.intMusicVidDislikes, dislikeCount: track => track.intMusicVidDislikes,
commentCount: (track) => track.intMusicVidComments, commentCount: track => track.intMusicVidComments
}, },
Artist: { Artist: {
theAudioDB: (artist, args, context) => { theAudioDB: (artist, args, context) => {
return context.loaders.theAudioDB.load(['artist', artist.id]); return context.loaders.theAudioDB.load(['artist', artist.id])
}, }
}, },
Recording: { Recording: {
theAudioDB: (recording, args, context) => { theAudioDB: (recording, args, context) => {
return context.loaders.theAudioDB.load(['recording', recording.id]); return context.loaders.theAudioDB.load(['recording', recording.id])
}, }
}, },
ReleaseGroup: { ReleaseGroup: {
theAudioDB: (releaseGroup, args, context) => { theAudioDB: (releaseGroup, args, context) => {
return context.loaders.theAudioDB.load([ return context.loaders.theAudioDB.load(['release-group', releaseGroup.id])
'release-group', }
releaseGroup.id, }
]); }
},
},
};

View file

@ -1,372 +1,232 @@
import gql from '../../tag.js'; import gql from '../../tag'
export default gql` export default gql`
""" # The image sizes that may be requested at [TheAudioDB](http://www.theaudiodb.com/).
The image sizes that may be requested at [TheAudioDB](http://www.theaudiodb.com/).
"""
enum TheAudioDBImageSize { enum TheAudioDBImageSize {
""" # The images full original dimensions.
The images full original dimensions.
"""
FULL FULL
""" # A maximum dimension of 200px.
A maximum dimension of 200px.
"""
PREVIEW PREVIEW
} }
""" # An artist on [TheAudioDB](http://www.theaudiodb.com/).
An artist on [TheAudioDB](http://www.theaudiodb.com/).
"""
type TheAudioDBArtist { type TheAudioDBArtist {
""" # TheAudioDB ID of the artist.
TheAudioDB ID of the artist.
"""
artistID: ID artistID: ID
""" # A biography of the artist, often available in several languages.
A biography of the artist, often available in several languages.
"""
biography( biography(
""" # The two-letter code for the language in which to retrieve the biography.
The two-letter code for the language in which to retrieve the biography.
"""
lang: String = "en" lang: String = "en"
): String ): String
""" # The number of members in the musical group, if applicable.
The number of members in the musical group, if applicable.
"""
memberCount: Int memberCount: Int
""" # A 1000x185 JPG banner image containing the artist and their logo or name.
A 1000x185 JPG banner image containing the artist and their logo or name.
"""
banner( banner(
""" # The size of the image to retrieve.
The size of the image to retrieve.
"""
size: TheAudioDBImageSize = FULL size: TheAudioDBImageSize = FULL
): URLString ): URLString
""" # A list of 1280x720 or 1920x1080 JPG images depicting the artist.
A list of 1280x720 or 1920x1080 JPG images depicting the artist.
"""
fanArt( fanArt(
""" # The size of the images to retrieve.
The size of the images to retrieve.
"""
size: TheAudioDBImageSize = FULL size: TheAudioDBImageSize = FULL
): [URLString]! ): [URLString]!
""" # A 400x155 PNG image containing the artists logo or name, with a transparent
A 400x155 PNG image containing the artists logo or name, with a transparent # background.
background.
"""
logo( logo(
""" # The size of the image to retrieve.
The size of the image to retrieve.
"""
size: TheAudioDBImageSize = FULL size: TheAudioDBImageSize = FULL
): URLString ): URLString
""" # A 1000x1000 JPG thumbnail image picturing the artist (usually containing
A 1000x1000 JPG thumbnail image picturing the artist (usually containing # every member of a band).
every member of a band).
"""
thumbnail( thumbnail(
""" # The size of the image to retrieve.
The size of the image to retrieve.
"""
size: TheAudioDBImageSize = FULL size: TheAudioDBImageSize = FULL
): URLString ): URLString
""" # The primary musical genre of the artist (e.g. Alternative Rock).
The primary musical genre of the artist (e.g. Alternative Rock).
"""
genre: String genre: String
""" # The primary musical mood of the artist (e.g. Sad).
The primary musical mood of the artist (e.g. Sad).
"""
mood: String mood: String
""" # The primary musical style of the artist (e.g. Rock/Pop).
The primary musical style of the artist (e.g. Rock/Pop).
"""
style: String style: String
} }
""" # An album on [TheAudioDB](http://www.theaudiodb.com/) corresponding with a
An album on [TheAudioDB](http://www.theaudiodb.com/) corresponding with a # MusicBrainz Release Group.
MusicBrainz Release Group.
"""
type TheAudioDBAlbum { type TheAudioDBAlbum {
""" # TheAudioDB ID of the album.
TheAudioDB ID of the album.
"""
albumID: ID albumID: ID
""" # TheAudioDB ID of the artist who released the album.
TheAudioDB ID of the artist who released the album.
"""
artistID: ID artistID: ID
""" # A description of the album, often available in several languages.
A description of the album, often available in several languages.
"""
description( description(
""" # The two-letter code for the language in which to retrieve the biography.
The two-letter code for the language in which to retrieve the biography.
"""
lang: String = "en" lang: String = "en"
): String ): String
""" # A review of the album.
A review of the album.
"""
review: String review: String
""" # The worldwide sales figure.
The worldwide sales figure. salesCount: Int
"""
salesCount: Float
""" # The albums rating as determined by user votes, out of 10.
The albums rating as determined by user votes, out of 10.
"""
score: Float score: Float
""" # The number of users who voted to determine the albums score.
The number of users who voted to determine the albums score. scoreVotes: Int
"""
scoreVotes: Float
""" # An image of the physical disc media for the album.
An image of the physical disc media for the album.
"""
discImage( discImage(
""" # The size of the image to retrieve.
The size of the image to retrieve.
"""
size: TheAudioDBImageSize = FULL size: TheAudioDBImageSize = FULL
): URLString ): URLString
""" # An image of the spine of the album packaging.
An image of the spine of the album packaging.
"""
spineImage( spineImage(
""" # The size of the image to retrieve.
The size of the image to retrieve.
"""
size: TheAudioDBImageSize = FULL size: TheAudioDBImageSize = FULL
): URLString ): URLString
""" # An image of the front of the album packaging.
An image of the front of the album packaging.
"""
frontImage( frontImage(
""" # The size of the image to retrieve.
The size of the image to retrieve.
"""
size: TheAudioDBImageSize = FULL size: TheAudioDBImageSize = FULL
): URLString ): URLString
""" # An image of the back of the album packaging.
An image of the back of the album packaging.
"""
backImage( backImage(
""" # The size of the image to retrieve.
The size of the image to retrieve.
"""
size: TheAudioDBImageSize = FULL size: TheAudioDBImageSize = FULL
): URLString ): URLString
""" # The primary musical genre of the album (e.g. Alternative Rock).
The primary musical genre of the album (e.g. Alternative Rock).
"""
genre: String genre: String
""" # The primary musical mood of the album (e.g. Sad).
The primary musical mood of the album (e.g. Sad).
"""
mood: String mood: String
""" # The primary musical style of the album (e.g. Rock/Pop).
The primary musical style of the album (e.g. Rock/Pop).
"""
style: String style: String
""" # A rough description of the primary musical speed of the album (e.g. Medium).
A rough description of the primary musical speed of the album (e.g. Medium).
"""
speed: String speed: String
""" # The primary musical theme of the album (e.g. In Love).
The primary musical theme of the album (e.g. In Love).
"""
theme: String theme: String
} }
""" # A track on [TheAudioDB](http://www.theaudiodb.com/) corresponding with a
A track on [TheAudioDB](http://www.theaudiodb.com/) corresponding with a # MusicBrainz Recording.
MusicBrainz Recording.
"""
type TheAudioDBTrack { type TheAudioDBTrack {
""" # TheAudioDB ID of the track.
TheAudioDB ID of the track.
"""
trackID: ID trackID: ID
""" # TheAudioDB ID of the album on which the track appears.
TheAudioDB ID of the album on which the track appears.
"""
albumID: ID albumID: ID
""" # TheAudioDB ID of the artist who released the track.
TheAudioDB ID of the artist who released the track.
"""
artistID: ID artistID: ID
""" # A description of the track.
A description of the track.
"""
description( description(
""" # The two-letter code for the language in which to retrieve the description.
The two-letter code for the language in which to retrieve the description.
"""
lang: String = "en" lang: String = "en"
): String ): String
""" # A thumbnail image for the track.
A thumbnail image for the track.
"""
thumbnail( thumbnail(
""" # The size of the image to retrieve.
The size of the image to retrieve.
"""
size: TheAudioDBImageSize = FULL size: TheAudioDBImageSize = FULL
): URLString ): URLString
""" # The tracks rating as determined by user votes, out of 10.
The tracks rating as determined by user votes, out of 10.
"""
score: Float score: Float
""" # The number of users who voted to determine the albums score.
The number of users who voted to determine the albums score. scoreVotes: Int
"""
scoreVotes: Float
""" # The track number of the song on the album.
The track number of the song on the album.
"""
trackNumber: Int trackNumber: Int
""" # The official music video for the track.
The official music video for the track.
"""
musicVideo: TheAudioDBMusicVideo musicVideo: TheAudioDBMusicVideo
""" # The primary musical genre of the track (e.g. Alternative Rock).
The primary musical genre of the track (e.g. Alternative Rock).
"""
genre: String genre: String
""" # The primary musical mood of the track (e.g. Sad).
The primary musical mood of the track (e.g. Sad).
"""
mood: String mood: String
""" # The primary musical style of the track (e.g. Rock/Pop).
The primary musical style of the track (e.g. Rock/Pop).
"""
style: String style: String
""" # The primary musical theme of the track (e.g. In Love).
The primary musical theme of the track (e.g. In Love).
"""
theme: String theme: String
} }
""" # Details of a music video associated with a track on [TheAudioDB](http://www.theaudiodb.com/).
Details of a music video associated with a track on [TheAudioDB](http://www.theaudiodb.com/).
"""
type TheAudioDBMusicVideo { type TheAudioDBMusicVideo {
""" # The URL where the music video can be found.
The URL where the music video can be found.
"""
url: URLString url: URLString
""" # The video production company of the music video.
The video production company of the music video.
"""
companyName: String companyName: String
""" # The director of the music video.
The director of the music video.
"""
directorName: String directorName: String
""" # A list of still images from the music video.
A list of still images from the music video.
"""
screenshots( screenshots(
""" # The size of the images to retrieve.
The size of the images to retrieve.
"""
size: TheAudioDBImageSize = FULL size: TheAudioDBImageSize = FULL
): [URLString]! ): [URLString]!
""" # The number of views the video has received at the given URL. This will rarely
The number of views the video has received at the given URL. This will rarely # be up to date, so use cautiously.
be up to date, so use cautiously. viewCount: Int
"""
viewCount: Float
""" # The number of likes the video has received at the given URL. This will rarely
The number of likes the video has received at the given URL. This will rarely # be up to date, so use cautiously.
be up to date, so use cautiously. likeCount: Int
"""
likeCount: Float
""" # The number of dislikes the video has received at the given URL. This will
The number of dislikes the video has received at the given URL. This will # rarely be up to date, so use cautiously.
rarely be up to date, so use cautiously. dislikeCount: Int
"""
dislikeCount: Float
""" # The number of comments the video has received at the given URL. This will
The number of comments the video has received at the given URL. This will # rarely be up to date, so use cautiously.
rarely be up to date, so use cautiously. commentCount: Int
"""
commentCount: Float
} }
extend type Artist { extend type Artist {
""" # Data about the artist from [TheAudioDB](http://www.theaudiodb.com/), a good
Data about the artist from [TheAudioDB](http://www.theaudiodb.com/), a good # source of biographical information and images.
source of biographical information and images. # This field is provided by TheAudioDB extension.
This field is provided by TheAudioDB extension.
"""
theAudioDB: TheAudioDBArtist theAudioDB: TheAudioDBArtist
} }
extend type Recording { extend type Recording {
""" # Data about the recording from [TheAudioDB](http://www.theaudiodb.com/).
Data about the recording from [TheAudioDB](http://www.theaudiodb.com/). # This field is provided by TheAudioDB extension.
This field is provided by TheAudioDB extension.
"""
theAudioDB: TheAudioDBTrack theAudioDB: TheAudioDBTrack
} }
extend type ReleaseGroup { extend type ReleaseGroup {
""" # Data about the release group from [TheAudioDB](http://www.theaudiodb.com/),
Data about the release group from [TheAudioDB](http://www.theaudiodb.com/), # a good source of descriptive information, reviews, and images.
a good source of descriptive information, reviews, and images. # This field is provided by TheAudioDB extension.
This field is provided by TheAudioDB extension.
"""
theAudioDB: TheAudioDBAlbum theAudioDB: TheAudioDBAlbum
} }
`; `

View file

@ -1,104 +1,73 @@
import express from 'express'; import express from 'express'
import ExpressGraphQL from 'express-graphql'; import graphqlHTTP from 'express-graphql'
import compression from 'compression'; import compression from 'compression'
import cors from 'cors'; import cors from 'cors'
import MusicBrainz from './api/index.js'; import MusicBrainz from './api'
import Client from './api/client.js'; import schema, { createSchema } from './schema'
import { baseSchema, createSchema } from './schema.js'; import { createContext } from './context'
import { createContext } from './context.js';
import { loadExtension } from './extensions/index.js';
import gql from './tag.js';
const { graphqlHTTP } = ExpressGraphQL; const formatError = err => ({
const formatError = (err) => ({
message: err.message, message: err.message,
locations: err.locations, locations: err.locations,
stack: err.stack, stack: err.stack
}); })
const defaultExtensions = [ export const defaultExtensions = [
'graphbrainz/extensions/cover-art-archive', require.resolve('./extensions/cover-art-archive'),
'graphbrainz/extensions/fanart-tv', require.resolve('./extensions/fanart-tv'),
'graphbrainz/extensions/mediawiki', require.resolve('./extensions/mediawiki'),
'graphbrainz/extensions/the-audio-db', require.resolve('./extensions/the-audio-db')
]; ]
function middleware({ const middleware = (
client = new MusicBrainz(), {
extensions = process.env.GRAPHBRAINZ_EXTENSIONS client = new MusicBrainz(),
? JSON.parse(process.env.GRAPHBRAINZ_EXTENSIONS) extensions = process.env.GRAPHBRAINZ_EXTENSIONS
: defaultExtensions, ? JSON.parse(process.env.GRAPHBRAINZ_EXTENSIONS)
...middlewareOptions : defaultExtensions,
} = {}) { ...middlewareOptions
const DEV = process.env.NODE_ENV !== 'production'; } = {}
const graphiql = DEV || process.env.GRAPHBRAINZ_GRAPHIQL === 'true'; ) => {
const getAsyncMiddleware = async () => { const options = { client, extensions, ...middlewareOptions }
const loadedExtensions = await Promise.all( const DEV = process.env.NODE_ENV !== 'production'
extensions.map((extensionSpecifier) => loadExtension(extensionSpecifier)) const graphiql = DEV || process.env.GRAPHBRAINZ_GRAPHIQL === 'true'
); return graphqlHTTP({
const options = { schema: createSchema(schema, options),
client, context: createContext(options),
extensions: loadedExtensions, pretty: DEV,
...middlewareOptions, graphiql,
}; formatError: DEV ? formatError : undefined,
const schema = createSchema(baseSchema, options); ...middlewareOptions
const context = createContext(options); })
return graphqlHTTP({
schema,
context,
pretty: DEV,
graphiql,
customFormatErrorFn: DEV ? formatError : undefined,
...middlewareOptions,
});
};
const asyncMiddleware = getAsyncMiddleware();
return async (req, res, next) => {
try {
const middleware = await asyncMiddleware;
middleware(req, res, next);
} catch (err) {
next(err);
}
};
} }
async function start(options) { export default middleware
const dotenv = await import('dotenv');
dotenv.config({ silent: true }); export function start(options) {
const app = express(); require('dotenv').config({ silent: true })
const port = process.env.PORT || 3000; const app = express()
const route = process.env.GRAPHBRAINZ_PATH || '/'; const port = process.env.PORT || 3000
const route = process.env.GRAPHBRAINZ_PATH || '/'
const corsOptions = { const corsOptions = {
origin: process.env.GRAPHBRAINZ_CORS_ORIGIN || false, origin: process.env.GRAPHBRAINZ_CORS_ORIGIN || false,
methods: 'HEAD,GET,POST', methods: 'HEAD,GET,POST'
}; }
switch (corsOptions.origin) { switch (corsOptions.origin) {
case 'true': case 'true':
corsOptions.origin = true; corsOptions.origin = true
break; break
case 'false': case 'false':
corsOptions.origin = false; corsOptions.origin = false
break; break
default: default:
break; break
} }
app.use(compression()); app.use(compression())
app.use(route, cors(corsOptions), middleware(options)); app.use(route, cors(corsOptions), middleware(options))
app.listen(port); app.listen(port)
console.log(`Listening on port ${port}.`); console.log(`Listening on port ${port}.`)
} }
export { if (require.main === module) {
Client, start()
MusicBrainz, }
gql,
baseSchema,
createContext,
createSchema,
defaultExtensions,
loadExtension,
middleware,
start,
};

View file

@ -1,92 +1,89 @@
import createDebug from 'debug'; import DataLoader from 'dataloader'
import DataLoader from 'dataloader'; import LRUCache from 'lru-cache'
import LRUCache from 'lru-cache'; import { toPlural } from './types/helpers'
import { ONE_DAY, toPlural } from './util.js'; import { ONE_DAY } from './util'
const debug = createDebug('graphbrainz:loaders'); const debug = require('debug')('graphbrainz:loaders')
export default function createLoaders(client) { export default function createLoaders(client) {
// All loaders share a single LRU cache that will remember 8192 responses, // All loaders share a single LRU cache that will remember 8192 responses,
// each cached for 1 day. // each cached for 1 day.
const cache = new LRUCache({ const cache = LRUCache({
max: parseInt(process.env.GRAPHBRAINZ_CACHE_SIZE || 8192, 10), max: parseInt(process.env.GRAPHBRAINZ_CACHE_SIZE || 8192, 10),
maxAge: parseInt(process.env.GRAPHBRAINZ_CACHE_TTL || ONE_DAY, 10), maxAge: parseInt(process.env.GRAPHBRAINZ_CACHE_TTL || ONE_DAY, 10),
dispose(key) { dispose(key) {
debug(`Removed from cache. key=${key}`); debug(`Removed from cache. key=${key}`)
}, }
}); })
// Make the cache Map-like. // Make the cache Map-like.
cache.delete = cache.del; cache.delete = cache.del
cache.clear = cache.reset; cache.clear = cache.reset
const lookup = new DataLoader( const lookup = new DataLoader(
(keys) => { keys => {
return Promise.all( return Promise.all(
keys.map((key) => { keys.map(key => {
const [entityType, id, params = {}] = key; const [entityType, id, params = {}] = key
return client.lookup(entityType, id, params).then((entity) => { return client.lookup(entityType, id, params).then(entity => {
if (entity) { if (entity) {
// Store the entity type so we can determine what type of object this // Store the entity type so we can determine what type of object this
// is elsewhere in the code. // is elsewhere in the code.
entity._type = entityType; entity._type = entityType
} }
return entity; return entity
}); })
}) })
); )
}, },
{ {
batch: false, cacheKeyFn: key => client.getLookupURL(...key),
cacheKeyFn: (key) => client.getLookupURL(...key), cacheMap: cache
cacheMap: cache,
} }
); )
const browse = new DataLoader( const browse = new DataLoader(
(keys) => { keys => {
return Promise.all( return Promise.all(
keys.map((key) => { keys.map(key => {
const [entityType, params = {}] = key; const [entityType, params = {}] = key
return client.browse(entityType, params).then((list) => { return client.browse(entityType, params).then(list => {
list[toPlural(entityType)].forEach((entity) => { list[toPlural(entityType)].forEach(entity => {
// Store the entity type so we can determine what type of object this // Store the entity type so we can determine what type of object this
// is elsewhere in the code. // is elsewhere in the code.
entity._type = entityType; entity._type = entityType
}); })
return list; return list
}); })
}) })
); )
}, },
{ {
batch: false, cacheKeyFn: key => client.getBrowseURL(...key),
cacheKeyFn: (key) => client.getBrowseURL(...key), cacheMap: cache
cacheMap: cache,
} }
); )
const search = new DataLoader( const search = new DataLoader(
(keys) => { keys => {
return Promise.all( return Promise.all(
keys.map((key) => { keys.map(key => {
const [entityType, query, params = {}] = key; const [entityType, query, params = {}] = key
return client.search(entityType, query, params).then((list) => { return client.search(entityType, query, params).then(list => {
list[toPlural(entityType)].forEach((entity) => { list[toPlural(entityType)].forEach(entity => {
// Store the entity type so we can determine what type of object this // Store the entity type so we can determine what type of object this
// is elsewhere in the code. // is elsewhere in the code.
entity._type = entityType; entity._type = entityType
}); })
return list; return list
}); })
}) })
); )
}, },
{ {
batch: false, cacheKeyFn: key => client.getSearchURL(...key),
cacheKeyFn: (key) => client.getSearchURL(...key), cacheMap: cache
cacheMap: cache,
} }
); )
return { lookup, browse, search }; return { lookup, browse, search }
} }

View file

@ -1,6 +1,6 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLString } from 'graphql'
import GraphQLRelay from 'graphql-relay'; import { forwardConnectionArgs } from 'graphql-relay'
import { resolveBrowse } from '../resolvers.js'; import { resolveBrowse } from '../resolvers'
import { import {
MBID, MBID,
AreaConnection, AreaConnection,
@ -15,66 +15,62 @@ import {
RecordingConnection, RecordingConnection,
ReleaseConnection, ReleaseConnection,
ReleaseGroupConnection, ReleaseGroupConnection,
WorkConnection, WorkConnection
} from '../types/index.js'; } from '../types'
import { releaseGroupType, releaseStatus } from '../types/helpers.js'; import { toWords, releaseGroupType, releaseStatus } from '../types/helpers'
import { toWords } from '../util.js';
const { GraphQLObjectType, GraphQLString } = GraphQL;
const { forwardConnectionArgs } = GraphQLRelay;
const area = { const area = {
type: MBID, type: MBID,
description: 'The MBID of an area to which the entity is linked.', description: 'The MBID of an area to which the entity is linked.'
}; }
const artist = { const artist = {
type: MBID, type: MBID,
description: 'The MBID of an artist to which the entity is linked.', description: 'The MBID of an artist to which the entity is linked.'
}; }
const collection = { const collection = {
type: MBID, type: MBID,
description: 'The MBID of a collection in which the entity is found.', description: 'The MBID of a collection in which the entity is found.'
}; }
const event = { const event = {
type: MBID, type: MBID,
description: 'The MBID of an event to which the entity is linked.', description: 'The MBID of an event to which the entity is linked.'
}; }
const label = { const label = {
type: MBID, type: MBID,
description: 'The MBID of a label to which the entity is linked.', description: 'The MBID of a label to which the entity is linked.'
}; }
const place = { const place = {
type: MBID, type: MBID,
description: 'The MBID of a place to which the entity is linked.', description: 'The MBID of a place to which the entity is linked.'
}; }
const recording = { const recording = {
type: MBID, type: MBID,
description: 'The MBID of a recording to which the entity is linked.', description: 'The MBID of a recording to which the entity is linked.'
}; }
const release = { const release = {
type: MBID, type: MBID,
description: 'The MBID of a release to which the entity is linked.', description: 'The MBID of a release to which the entity is linked.'
}; }
const releaseGroup = { const releaseGroup = {
type: MBID, type: MBID,
description: 'The MBID of a release group to which the entity is linked.', description: 'The MBID of a release group to which the entity is linked.'
}; }
const work = { const work = {
type: MBID, type: MBID,
description: 'The MBID of a work to which the entity is linked.', description: 'The MBID of a work to which the entity is linked.'
}; }
function createBrowseField(connectionType, args) { function createBrowseField(connectionType, args) {
const typeName = toWords(connectionType.name.slice(0, -10)); const typeName = toWords(connectionType.name.slice(0, -10))
return { return {
type: connectionType, type: connectionType,
description: `Browse ${typeName} entities linked to the given arguments.`, description: `Browse ${typeName} entities linked to the given arguments.`,
args: { args: {
...args, ...args,
...forwardConnectionArgs, ...forwardConnectionArgs
}, },
resolve: resolveBrowse, resolve: resolveBrowse
}; }
} }
export const BrowseQuery = new GraphQLObjectType({ export const BrowseQuery = new GraphQLObjectType({
@ -83,7 +79,7 @@ export const BrowseQuery = new GraphQLObjectType({
entity.`, entity.`,
fields: { fields: {
areas: createBrowseField(AreaConnection, { areas: createBrowseField(AreaConnection, {
collection, collection
}), }),
artists: createBrowseField(ArtistConnection, { artists: createBrowseField(ArtistConnection, {
area, area,
@ -91,14 +87,14 @@ entity.`,
recording, recording,
release, release,
releaseGroup, releaseGroup,
work, work
}), }),
collections: createBrowseField(CollectionConnection, { collections: createBrowseField(CollectionConnection, {
area, area,
artist, artist,
editor: { editor: {
type: GraphQLString, type: GraphQLString,
description: 'The username of the editor who created the collection.', description: 'The username of the editor who created the collection.'
}, },
event, event,
label, label,
@ -106,22 +102,22 @@ entity.`,
recording, recording,
release, release,
releaseGroup, releaseGroup,
work, work
}), }),
events: createBrowseField(EventConnection, { events: createBrowseField(EventConnection, {
area, area,
artist, artist,
collection, collection,
place, place
}), }),
labels: createBrowseField(LabelConnection, { labels: createBrowseField(LabelConnection, {
area, area,
collection, collection,
release, release
}), }),
places: createBrowseField(PlaceConnection, { places: createBrowseField(PlaceConnection, {
area, area,
collection, collection
}), }),
recordings: createBrowseField(RecordingConnection, { recordings: createBrowseField(RecordingConnection, {
artist, artist,
@ -129,9 +125,9 @@ entity.`,
isrc: { isrc: {
type: ISRC, type: ISRC,
description: `The [International Standard Recording Code](https://musicbrainz.org/doc/ISRC) description: `The [International Standard Recording Code](https://musicbrainz.org/doc/ISRC)
(ISRC) of the recording.`, (ISRC) of the recording.`
}, },
release, release
}), }),
releases: createBrowseField(ReleaseConnection, { releases: createBrowseField(ReleaseConnection, {
area, area,
@ -140,28 +136,28 @@ entity.`,
discID: { discID: {
type: DiscID, type: DiscID,
description: `A [disc ID](https://musicbrainz.org/doc/Disc_ID) description: `A [disc ID](https://musicbrainz.org/doc/Disc_ID)
associated with the release.`, associated with the release.`
}, },
label, label,
recording, recording,
releaseGroup, releaseGroup,
track: { track: {
type: MBID, type: MBID,
description: 'The MBID of a track that is included in the release.', description: 'The MBID of a track that is included in the release.'
}, },
trackArtist: { trackArtist: {
type: MBID, type: MBID,
description: `The MBID of an artist that appears on a track in the description: `The MBID of an artist that appears on a track in the
release, but is not included in the credits for the release itself.`, release, but is not included in the credits for the release itself.`
}, },
type: releaseGroupType, type: releaseGroupType,
status: releaseStatus, status: releaseStatus
}), }),
releaseGroups: createBrowseField(ReleaseGroupConnection, { releaseGroups: createBrowseField(ReleaseGroupConnection, {
artist, artist,
collection, collection,
release, release,
type: releaseGroupType, type: releaseGroupType
}), }),
works: createBrowseField(WorkConnection, { works: createBrowseField(WorkConnection, {
artist, artist,
@ -169,11 +165,11 @@ release, but is not included in the credits for the release itself.`,
iswc: { iswc: {
type: ISWC, type: ISWC,
description: `The [International Standard Musical Work Code](https://musicbrainz.org/doc/ISWC) description: `The [International Standard Musical Work Code](https://musicbrainz.org/doc/ISWC)
(ISWC) of the work.`, (ISWC) of the work.`
}, }
}), })
}, }
}); })
export const browse = { export const browse = {
type: BrowseQuery, type: BrowseQuery,
@ -181,7 +177,7 @@ export const browse = {
'Browse all MusicBrainz entities directly linked to another entity.', 'Browse all MusicBrainz entities directly linked to another entity.',
// We only have work to do once we know what entity types are being requested, // We only have work to do once we know what entity types are being requested,
// so this can just resolve to an empty object. // so this can just resolve to an empty object.
resolve: () => ({}), resolve: () => ({})
}; }
export default BrowseQuery; export default BrowseQuery

View file

@ -1,3 +1,3 @@
export { LookupQuery, lookup } from './lookup.js'; export { LookupQuery, lookup } from './lookup'
export { BrowseQuery, browse } from './browse.js'; export { BrowseQuery, browse } from './browse'
export { SearchQuery, search } from './search.js'; export { SearchQuery, search } from './search'

View file

@ -1,6 +1,6 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLNonNull } from 'graphql'
import { resolveLookup } from '../resolvers.js'; import { resolveLookup } from '../resolvers'
import { mbid } from '../types/helpers.js'; import { mbid, toWords } from '../types/helpers'
import { import {
Area, Area,
Artist, Artist,
@ -18,20 +18,17 @@ import {
Series, Series,
URL, URL,
URLString, URLString,
Work, Work
} from '../types/index.js'; } from '../types'
import { toWords } from '../util.js';
const { GraphQLObjectType, GraphQLNonNull } = GraphQL;
function createLookupField(entity, args) { function createLookupField(entity, args) {
const typeName = toWords(entity.name); const typeName = toWords(entity.name)
return { return {
type: entity, type: entity,
description: `Look up a specific ${typeName} by its MBID.`, description: `Look up a specific ${typeName} by its MBID.`,
args: { mbid, ...args }, args: { mbid, ...args },
resolve: resolveLookup, resolve: resolveLookup
}; }
} }
export const LookupQuery = new GraphQLObjectType({ export const LookupQuery = new GraphQLObjectType({
@ -48,12 +45,12 @@ export const LookupQuery = new GraphQLObjectType({
discID: { discID: {
type: new GraphQLNonNull(DiscID), type: new GraphQLNonNull(DiscID),
description: `The [disc ID](https://musicbrainz.org/doc/Disc_ID) description: `The [disc ID](https://musicbrainz.org/doc/Disc_ID)
of the disc.`, of the disc.`
}, }
}, },
resolve: (root, { discID }, { loaders }, info) => { resolve: (root, { discID }, { loaders }, info) => {
return loaders.lookup.load(['discid', discID]); return loaders.lookup.load(['discid', discID])
}, }
}, },
event: createLookupField(Event), event: createLookupField(Event),
instrument: createLookupField(Instrument), instrument: createLookupField(Instrument),
@ -68,23 +65,23 @@ of the disc.`,
...mbid, ...mbid,
// Remove the non-null requirement that is usually on the `mbid` field // Remove the non-null requirement that is usually on the `mbid` field
// so that URLs can be looked up by `resource`. // so that URLs can be looked up by `resource`.
type: MBID, type: MBID
}, },
resource: { resource: {
type: URLString, type: URLString,
description: 'The web address of the URL entity to look up.', description: 'The web address of the URL entity to look up.'
}, }
}), }),
work: createLookupField(Work), work: createLookupField(Work)
}, }
}); })
export const lookup = { export const lookup = {
type: LookupQuery, type: LookupQuery,
description: 'Perform a lookup of a MusicBrainz entity by its MBID.', description: 'Perform a lookup of a MusicBrainz entity by its MBID.',
// We only have work to do once we know what entity types are being requested, // We only have work to do once we know what entity types are being requested,
// so this can just resolve to an empty object. // so this can just resolve to an empty object.
resolve: () => ({}), resolve: () => ({})
}; }
export default LookupQuery; export default LookupQuery

View file

@ -1,6 +1,6 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLNonNull, GraphQLString } from 'graphql'
import GraphQLRelay from 'graphql-relay'; import { forwardConnectionArgs } from 'graphql-relay'
import { resolveSearch } from '../resolvers.js'; import { resolveSearch } from '../resolvers'
import { import {
AreaConnection, AreaConnection,
ArtistConnection, ArtistConnection,
@ -12,15 +12,12 @@ import {
ReleaseConnection, ReleaseConnection,
ReleaseGroupConnection, ReleaseGroupConnection,
SeriesConnection, SeriesConnection,
WorkConnection, WorkConnection
} from '../types/index.js'; } from '../types'
import { toWords } from '../util.js'; import { toWords } from '../types/helpers'
const { GraphQLObjectType, GraphQLNonNull, GraphQLString } = GraphQL;
const { forwardConnectionArgs } = GraphQLRelay;
function createSearchField(connectionType) { function createSearchField(connectionType) {
const typeName = toWords(connectionType.name.slice(0, -10)); const typeName = toWords(connectionType.name.slice(0, -10))
return { return {
type: connectionType, type: connectionType,
description: `Search for ${typeName} entities matching the given query.`, description: `Search for ${typeName} entities matching the given query.`,
@ -28,12 +25,12 @@ function createSearchField(connectionType) {
query: { query: {
type: new GraphQLNonNull(GraphQLString), type: new GraphQLNonNull(GraphQLString),
description: `The query terms, in Lucene search syntax. See [examples description: `The query terms, in Lucene search syntax. See [examples
and search fields](https://musicbrainz.org/doc/Development/XML_Web_Service/Version_2/Search).`, and search fields](https://musicbrainz.org/doc/Development/XML_Web_Service/Version_2/Search).`
}, },
...forwardConnectionArgs, ...forwardConnectionArgs
}, },
resolve: resolveSearch, resolve: resolveSearch
}; }
} }
export const SearchQuery = new GraphQLObjectType({ export const SearchQuery = new GraphQLObjectType({
@ -50,16 +47,16 @@ export const SearchQuery = new GraphQLObjectType({
releases: createSearchField(ReleaseConnection), releases: createSearchField(ReleaseConnection),
releaseGroups: createSearchField(ReleaseGroupConnection), releaseGroups: createSearchField(ReleaseGroupConnection),
series: createSearchField(SeriesConnection), series: createSearchField(SeriesConnection),
works: createSearchField(WorkConnection), works: createSearchField(WorkConnection)
}, }
}); })
export const search = { export const search = {
type: SearchQuery, type: SearchQuery,
description: 'Search for MusicBrainz entities using Lucene query syntax.', description: 'Search for MusicBrainz entities using Lucene query syntax.',
// We only have work to do once we know what entity types are being requested, // We only have work to do once we know what entity types are being requested,
// so this can just resolve to an empty object. // so this can just resolve to an empty object.
resolve: () => ({}), resolve: () => ({})
}; }
export default SearchQuery; export default SearchQuery

View file

@ -1,103 +1,103 @@
import createDebug from 'debug'; const debug = require('debug')('graphbrainz:rate-limit')
const debug = createDebug('graphbrainz:rate-limit');
export default class RateLimit { export default class RateLimit {
constructor({ constructor(
limit = 1, {
period = 1000, limit = 1,
concurrency = limit || 1, period = 1000,
defaultPriority = 1, concurrency = limit || 1,
} = {}) { defaultPriority = 1
this.limit = limit; } = {}
this.period = period; ) {
this.defaultPriority = defaultPriority; this.limit = limit
this.concurrency = concurrency; this.period = period
this.queues = []; this.defaultPriority = defaultPriority
this.numPending = 0; this.concurrency = concurrency
this.periodStart = null; this.queues = []
this.periodCapacity = this.limit; this.numPending = 0
this.timer = null; this.periodStart = null
this.pendingFlush = false; this.periodCapacity = this.limit
this.prevTaskID = null; this.timer = null
this.pendingFlush = false
this.prevTaskID = null
} }
nextTaskID(prevTaskID = this.prevTaskID) { nextTaskID(prevTaskID = this.prevTaskID) {
const id = (prevTaskID || 0) + 1; const id = (prevTaskID || 0) + 1
this.prevTaskID = id; this.prevTaskID = id
return id; return id
} }
enqueue(fn, args, priority = this.defaultPriority) { enqueue(fn, args, priority = this.defaultPriority) {
priority = Math.max(0, priority); priority = Math.max(0, priority)
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const queue = (this.queues[priority] = this.queues[priority] || []); const queue = (this.queues[priority] = this.queues[priority] || [])
const id = this.nextTaskID(); const id = this.nextTaskID()
debug(`Enqueuing task. id=${id} priority=${priority}`); debug(`Enqueuing task. id=${id} priority=${priority}`)
queue.push({ fn, args, resolve, reject, id }); queue.push({ fn, args, resolve, reject, id })
if (!this.pendingFlush) { if (!this.pendingFlush) {
this.pendingFlush = true; this.pendingFlush = true
process.nextTick(() => { process.nextTick(() => {
this.pendingFlush = false; this.pendingFlush = false
this.flush(); this.flush()
}); })
} }
}); })
} }
dequeue() { dequeue() {
let task; let task
for (let i = this.queues.length - 1; i >= 0; i--) { for (let i = this.queues.length - 1; i >= 0; i--) {
const queue = this.queues[i]; const queue = this.queues[i]
if (queue && queue.length) { if (queue && queue.length) {
task = queue.shift(); task = queue.shift()
} }
if (!queue || !queue.length) { if (!queue || !queue.length) {
this.queues.length = i; this.queues.length = i
} }
if (task) { if (task) {
break; break
} }
} }
return task; return task
} }
flush() { flush() {
if (this.numPending < this.concurrency && this.periodCapacity > 0) { if (this.numPending < this.concurrency && this.periodCapacity > 0) {
const task = this.dequeue(); const task = this.dequeue()
if (task) { if (task) {
const { resolve, reject, fn, args, id } = task; const { resolve, reject, fn, args, id } = task
if (this.timer == null) { if (this.timer == null) {
const now = Date.now(); const now = Date.now()
let timeout = this.period; let timeout = this.period
if (this.periodStart != null) { if (this.periodStart != null) {
const delay = now - (this.periodStart + timeout); const delay = now - (this.periodStart + timeout)
if (delay > 0 && delay <= timeout) { if (delay > 0 && delay <= timeout) {
timeout -= delay; timeout -= delay
} }
} }
this.periodStart = now; this.periodStart = now
this.timer = setTimeout(() => { this.timer = setTimeout(() => {
this.timer = null; this.timer = null
this.periodCapacity = this.limit; this.periodCapacity = this.limit
this.flush(); this.flush()
}, timeout); }, timeout)
} }
this.numPending += 1; this.numPending += 1
this.periodCapacity -= 1; this.periodCapacity -= 1
const onResolve = (value) => { const onResolve = value => {
this.numPending -= 1; this.numPending -= 1
resolve(value); resolve(value)
this.flush(); this.flush()
}; }
const onReject = (err) => { const onReject = err => {
this.numPending -= 1; this.numPending -= 1
reject(err); reject(err)
this.flush(); this.flush()
}; }
debug(`Running task. id=${id}`); debug(`Running task. id=${id}`)
fn(...args).then(onResolve, onReject); fn(...args).then(onResolve, onReject)
this.flush(); this.flush()
} }
} }
} }

View file

@ -1,40 +1,39 @@
import GraphQLRelay from 'graphql-relay'; import { toDashed, toSingular } from './types/helpers'
import { getFields, extendIncludes, toDashed, toSingular } from './util.js'; import {
const {
getOffsetWithDefault, getOffsetWithDefault,
connectionFromArray, connectionFromArray,
connectionFromArraySlice, connectionFromArraySlice
} = GraphQLRelay; } from 'graphql-relay'
import { getFields, extendIncludes } from './util'
export function includeRelationships(params, info, fragments = info.fragments) { export function includeRelationships(params, info, fragments = info.fragments) {
let fields = getFields(info, fragments); let fields = getFields(info, fragments)
if (info.fieldName !== 'relationships') { if (info.fieldName !== 'relationships') {
if (fields.relationships) { if (fields.relationships) {
fields = getFields(fields.relationships, fragments); fields = getFields(fields.relationships, fragments)
} else { } else {
if (fields.edges) { if (fields.edges) {
fields = getFields(fields.edges, fragments); fields = getFields(fields.edges, fragments)
if (fields.node) { if (fields.node) {
return includeRelationships(params, fields.node, fragments); return includeRelationships(params, fields.node, fragments)
} }
} }
return params; return params
} }
} }
if (fields) { if (fields) {
const relationships = Object.keys(fields); const relationships = Object.keys(fields)
const includeRels = relationships.map((field) => { const includeRels = relationships.map(field => {
return `${toDashed(toSingular(field))}-rels`; return `${toDashed(toSingular(field))}-rels`
}); })
if (includeRels.length) { if (includeRels.length) {
params = { params = {
...params, ...params,
inc: extendIncludes(params.inc, includeRels), inc: extendIncludes(params.inc, includeRels)
}; }
} }
} }
return params; return params
} }
export function includeSubqueries(params, info, fragments = info.fragments) { export function includeSubqueries(params, info, fragments = info.fragments) {
@ -43,41 +42,39 @@ export function includeSubqueries(params, info, fragments = info.fragments) {
artistCredit: ['artist-credits'], artistCredit: ['artist-credits'],
artistCredits: ['artist-credits'], artistCredits: ['artist-credits'],
isrcs: ['isrcs'], isrcs: ['isrcs'],
media: ['media'], media: ['media', 'discids'],
'media.discs': ['discids'],
'media.tracks': ['recordings'],
rating: ['ratings'], rating: ['ratings'],
tags: ['tags'], tags: ['tags']
}; }
let fields = getFields(info, fragments, 1); let fields = getFields(info, fragments)
const include = []; const include = []
for (const key in subqueryIncludes) { for (const key in subqueryIncludes) {
const field = fields[key]; if (fields[key]) {
if (field) { const value = subqueryIncludes[key]
const value = subqueryIncludes[key]; include.push(...value)
include.push(...value);
} }
} }
params = { params = {
...params, ...params,
inc: extendIncludes(params.inc, include), inc: extendIncludes(params.inc, include)
};
if (fields['edges.node']) {
params = includeSubqueries(params, fields['edges.node'], fragments);
} }
return params; if (fields.edges) {
fields = getFields(fields.edges, fragments)
if (fields.node) {
params = includeSubqueries(params, fields.node, fragments)
}
}
return params
} }
export function resolveLookup(root, { mbid, ...params }, { loaders }, info) { export function resolveLookup(root, { mbid, ...params }, { loaders }, info) {
if (!mbid && !params.resource) { if (!mbid && !params.resource) {
throw new Error( throw new Error('Lookups by a field other than MBID must provide: resource')
'Lookups by a field other than MBID must provide: resource'
);
} }
const entityType = toDashed(info.fieldName); const entityType = toDashed(info.fieldName)
params = includeSubqueries(params, info); params = includeSubqueries(params, info)
params = includeRelationships(params, info); params = includeRelationships(params, info)
return loaders.lookup.load([entityType, mbid, params]); return loaders.lookup.load([entityType, mbid, params])
} }
export function resolveBrowse( export function resolveBrowse(
@ -86,62 +83,57 @@ export function resolveBrowse(
{ loaders }, { loaders },
info info
) { ) {
const pluralName = toDashed(info.fieldName); const pluralName = toDashed(info.fieldName)
const singularName = toSingular(pluralName); const singularName = toSingular(pluralName)
let params = { let params = {
...args, ...args,
type, type,
status, status,
limit: first, limit: first,
offset: getOffsetWithDefault(after, -1) + 1 || undefined, offset: getOffsetWithDefault(after, -1) + 1 || undefined
}; }
params = includeSubqueries(params, info); params = includeSubqueries(params, info)
params = includeRelationships(params, info, info.fragments); params = includeRelationships(params, info, info.fragments)
const formatParam = (value) => value.toLowerCase().replace(/ /g, ''); const formatParam = value => value.toLowerCase().replace(/ /g, '')
params.type = params.type.map(formatParam); params.type = params.type.map(formatParam)
params.status = params.status.map(formatParam); params.status = params.status.map(formatParam)
let request; let request
if (discID) { if (discID) {
request = loaders.lookup.load(['discid', discID, params]); request = loaders.lookup.load(['discid', discID, params])
// If fetching releases by disc ID, they will already include the `media` // If fetching releases by disc ID, they will already include the `media`
// and `discids` subqueries, and it is invalid to specify them. // and `discids` subqueries, and it is invalid to specify them.
if (params.inc) { if (params.inc) {
params.inc = params.inc.filter((value) => { params.inc = params.inc.filter(value => {
return value !== 'media' && value !== 'discids'; return value !== 'media' && value !== 'discids'
}); })
} }
} else if (isrc) { } else if (isrc) {
request = loaders.lookup.load(['isrc', isrc, params]).then((result) => { request = loaders.lookup.load(['isrc', isrc, params])
result[pluralName].forEach((entity) => {
entity._type = singularName;
});
return result;
});
} else if (iswc) { } else if (iswc) {
request = loaders.lookup.load(['iswc', iswc, params]); request = loaders.lookup.load(['iswc', iswc, params])
} else { } else {
request = loaders.browse.load([singularName, params]); request = loaders.browse.load([singularName, params])
} }
return request.then((list) => { return request.then(list => {
// Grab the list, offet, and count from the response and use them to build // Grab the list, offet, and count from the response and use them to build
// a Relay connection object. // a Relay connection object.
const { const {
[pluralName]: arraySlice, [pluralName]: arraySlice,
[`${singularName}-offset`]: sliceStart = 0, [`${singularName}-offset`]: sliceStart = 0,
[`${singularName}-count`]: arrayLength = arraySlice.length, [`${singularName}-count`]: arrayLength = arraySlice.length
} = list; } = list
const meta = { sliceStart, arrayLength }; const meta = { sliceStart, arrayLength }
const connection = connectionFromArraySlice( const connection = connectionFromArraySlice(
arraySlice, arraySlice,
{ first, after }, { first, after },
meta meta
); )
return { return {
nodes: connection.edges.map((edge) => edge.node), nodes: connection.edges.map(edge => edge.node),
totalCount: arrayLength, totalCount: arrayLength,
...connection, ...connection
}; }
}); })
} }
export function resolveSearch( export function resolveSearch(
@ -150,67 +142,67 @@ export function resolveSearch(
{ loaders }, { loaders },
info info
) { ) {
const pluralName = toDashed(info.fieldName); const pluralName = toDashed(info.fieldName)
const singularName = toSingular(pluralName); const singularName = toSingular(pluralName)
let params = { let params = {
...args, ...args,
limit: first, limit: first,
offset: getOffsetWithDefault(after, -1) + 1 || undefined, offset: getOffsetWithDefault(after, -1) + 1 || undefined
}; }
params = includeSubqueries(params, info); params = includeSubqueries(params, info)
return loaders.search.load([singularName, query, params]).then((list) => { return loaders.search.load([singularName, query, params]).then(list => {
const { const {
[pluralName]: arraySlice, [pluralName]: arraySlice,
offset: sliceStart, offset: sliceStart,
count: arrayLength, count: arrayLength
} = list; } = list
const meta = { sliceStart, arrayLength }; const meta = { sliceStart, arrayLength }
const connection = connectionFromArraySlice( const connection = connectionFromArraySlice(
arraySlice, arraySlice,
{ first, after }, { first, after },
meta meta
); )
// Move the `score` field up to the edge object and make sure it's a // Move the `score` field up to the edge object and make sure it's a
// number (MusicBrainz returns a string). // number (MusicBrainz returns a string).
const edges = connection.edges.map((edge) => ({ const edges = connection.edges.map(edge => ({
...edge, ...edge,
score: +edge.node.score, score: +edge.node.score
})); }))
const connectionWithExtras = { const connectionWithExtras = {
nodes: edges.map((edge) => edge.node), nodes: edges.map(edge => edge.node),
totalCount: arrayLength, totalCount: arrayLength,
...connection, ...connection,
edges, edges
}; }
return connectionWithExtras; return connectionWithExtras
}); })
} }
export function resolveRelationship(rels, args, context, info) { export function resolveRelationship(rels, args, context, info) {
const targetType = toDashed(toSingular(info.fieldName)).replace('-', '_'); const targetType = toDashed(toSingular(info.fieldName)).replace('-', '_')
let matches = rels.filter((rel) => rel['target-type'] === targetType); let matches = rels.filter(rel => rel['target-type'] === targetType)
// There's no way to filter these at the API level, so do it here. // There's no way to filter these at the API level, so do it here.
if (args.direction != null) { if (args.direction != null) {
matches = matches.filter((rel) => rel.direction === args.direction); matches = matches.filter(rel => rel.direction === args.direction)
} }
if (args.type != null) { if (args.type != null) {
matches = matches.filter((rel) => rel.type === args.type); matches = matches.filter(rel => rel.type === args.type)
} }
if (args.typeID != null) { if (args.typeID != null) {
matches = matches.filter((rel) => rel['type-id'] === args.typeID); matches = matches.filter(rel => rel['type-id'] === args.typeID)
} }
const connection = connectionFromArray(matches, args); const connection = connectionFromArray(matches, args)
return { return {
nodes: connection.edges.map((edge) => edge.node), nodes: connection.edges.map(edge => edge.node),
totalCount: matches.length, totalCount: matches.length,
...connection, ...connection
}; }
} }
export function resolveLinked(entity, args, context, info) { export function resolveLinked(entity, args, context, info) {
const parentEntity = toDashed(info.parentType.name); const parentEntity = toDashed(info.parentType.name)
args = { ...args, [parentEntity]: entity.id }; args = { ...args, [parentEntity]: entity.id }
return resolveBrowse(entity, args, context, info); return resolveBrowse(entity, args, context, info)
} }
/** /**
@ -220,32 +212,32 @@ export function resolveLinked(entity, args, context, info) {
*/ */
export function createSubqueryResolver( export function createSubqueryResolver(
{ inc, key } = {}, { inc, key } = {},
handler = (value) => value handler = value => value
) { ) {
return (entity, args, { loaders }, info) => { return (entity, args, { loaders }, info) => {
key = key || toDashed(info.fieldName); key = key || toDashed(info.fieldName)
let promise; let promise
if (key in entity) { if (key in entity) {
promise = Promise.resolve(entity); promise = Promise.resolve(entity)
} else { } else {
const { _type: entityType, id } = entity; const { _type: entityType, id } = entity
const params = { inc: [inc || key] }; const params = { inc: [inc || key] }
promise = loaders.lookup.load([entityType, id, params]); promise = loaders.lookup.load([entityType, id, params])
} }
return promise.then((entity) => handler(entity[key], args)); return promise.then(entity => handler(entity[key], args))
}; }
} }
export function resolveDiscReleases(disc, args, context, info) { export function resolveDiscReleases(disc, args, context, info) {
const { releases } = disc; const { releases } = disc
if (releases != null) { if (releases != null) {
const connection = connectionFromArray(releases, args); const connection = connectionFromArray(releases, args)
return { return {
nodes: connection.edges.map((edge) => edge.node), nodes: connection.edges.map(edge => edge.node),
totalCount: releases.length, totalCount: releases.length,
...connection, ...connection
}; }
} }
args = { ...args, discID: disc.id }; args = { ...args, discID: disc.id }
return resolveBrowse(disc, args, context, info); return resolveBrowse(disc, args, context, info)
} }

View file

@ -1,62 +1,58 @@
import createDebug from 'debug'; import { GraphQLSchema, GraphQLObjectType, extendSchema, parse } from 'graphql'
import GraphQL from 'graphql'; import { addResolveFunctionsToSchema } from 'graphql-tools'
import GraphQLToolsSchema from '@graphql-tools/schema'; import { lookup, browse, search } from './queries'
import { lookup, browse, search } from './queries/index.js'; import { nodeField } from './types/node'
import { nodeField } from './types/node.js'; import { loadExtension } from './extensions'
const { GraphQLSchema, GraphQLObjectType, extendSchema, parse } = GraphQL; const debug = require('debug')('graphbrainz:schema')
const { addResolversToSchema } = GraphQLToolsSchema;
const debug = createDebug('graphbrainz:schema');
export function applyExtension(extension, schema, options = {}) { export function applyExtension(extension, schema, options = {}) {
let outputSchema = schema; let outputSchema = schema
if (extension.extendSchema) { if (extension.extendSchema) {
if (typeof extension.extendSchema === 'object') { if (typeof extension.extendSchema === 'object') {
debug( debug(
`Extending schema via an object from the “${extension.name}” extension.` `Extending schema via an object from the “${extension.name}” extension.`
); )
const { schemas = [], resolvers } = extension.extendSchema; const { schemas = [], resolvers } = extension.extendSchema
outputSchema = schemas.reduce((updatedSchema, extensionSchema) => { outputSchema = schemas.reduce((updatedSchema, extensionSchema) => {
if (typeof extensionSchema === 'string') { if (typeof extensionSchema === 'string') {
extensionSchema = parse(extensionSchema); extensionSchema = parse(extensionSchema)
} }
return extendSchema(updatedSchema, extensionSchema); return extendSchema(updatedSchema, extensionSchema)
}, outputSchema); }, outputSchema)
if (resolvers) { if (resolvers) {
outputSchema = addResolversToSchema({ addResolveFunctionsToSchema(outputSchema, resolvers)
schema: outputSchema,
resolvers,
});
} }
} else if (typeof extension.extendSchema === 'function') { } else if (typeof extension.extendSchema === 'function') {
debug( debug(
`Extending schema via a function from the “${extension.name}” extension.` `Extending schema via a function from the “${
); extension.name
outputSchema = extension.extendSchema(schema, options); } extension.`
)
outputSchema = extension.extendSchema(schema, options)
} else { } else {
throw new Error( throw new Error(
`The “${extension.name}” extension contains an invalid ` + `The “${extension.name}” extension contains an invalid ` +
`\`extendSchema\` value: ${extension.extendSchema}` `\`extendSchema\` value: ${extension.extendSchema}`
); )
} }
} }
// Fix for `graphql-tools` creating a new Query type with no description. // Fix for `graphql-tools` creating a new Query type with no description.
if (outputSchema._queryType.description === undefined) { if (outputSchema._queryType.description === undefined) {
outputSchema._queryType.description = schema._queryType.description; outputSchema._queryType.description = schema._queryType.description
} }
return outputSchema; return outputSchema
} }
export function createSchema(schema, options = {}) { export function createSchema(schema, options = {}) {
const { extensions = [] } = options; const { extensions = [] } = options
return extensions.reduce((updatedSchema, extension) => { return extensions.reduce((updatedSchema, extension) => {
return applyExtension(extension, updatedSchema, options); return applyExtension(loadExtension(extension), updatedSchema, options)
}, schema); }, schema)
} }
export const baseSchema = new GraphQLSchema({ export default new GraphQLSchema({
query: new GraphQLObjectType({ query: new GraphQLObjectType({
name: 'Query', name: 'Query',
description: `The query root, from which multiple types of MusicBrainz description: `The query root, from which multiple types of MusicBrainz
@ -65,7 +61,7 @@ requests can be made.`,
lookup, lookup,
browse, browse,
search, search,
node: nodeField, node: nodeField
}), })
}), })
}); })

View file

@ -5,13 +5,11 @@
* whatsoever, but will parse the GraphQL document, allow syntax highlighting, * whatsoever, but will parse the GraphQL document, allow syntax highlighting,
* and enable Prettier formatting. * and enable Prettier formatting.
*/ */
import GraphQL from 'graphql'; import { parse } from 'graphql'
const { parse } = GraphQL;
export default function gql(literals, ...interpolations) { export default function gql(literals, ...interpolations) {
if (literals.length !== 1 || interpolations.length) { if (literals.length !== 1 || interpolations.length) {
throw new Error('The gql template tag does not support interpolation.'); throw new Error('The gql template tag does not support interpolation.')
} }
return parse(literals[0]); return parse(literals[0])
} }

View file

@ -1,11 +1,8 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLBoolean } from 'graphql/type'
import { Locale } from './scalars.js'; import { Locale } from './scalars'
import { name, sortName, fieldWithID } from './helpers.js'; import { name, sortName, fieldWithID } from './helpers'
import { createSubqueryResolver } from '../resolvers.js';
const { GraphQLObjectType, GraphQLBoolean, GraphQLList } = GraphQL; export default new GraphQLObjectType({
export const Alias = new GraphQLObjectType({
name: 'Alias', name: 'Alias',
description: `[Aliases](https://musicbrainz.org/doc/Aliases) are variant names description: `[Aliases](https://musicbrainz.org/doc/Aliases) are variant names
that are mostly used as search help: if a search matches an entitys alias, the that are mostly used as search help: if a search matches an entitys alias, the
@ -13,29 +10,22 @@ entity will be given as a result even if the actual name wouldnt be.`,
fields: () => ({ fields: () => ({
name: { name: {
...name, ...name,
description: 'The aliased name of the entity.', description: 'The aliased name of the entity.'
}, },
sortName, sortName,
locale: { locale: {
type: Locale, type: Locale,
description: `The locale (language and/or country) in which the alias is description: `The locale (language and/or country) in which the alias is
used.`, used.`
}, },
primary: { primary: {
type: GraphQLBoolean, type: GraphQLBoolean,
description: `Whether this is the main alias for the entity in the description: `Whether this is the main alias for the entity in the
specified locale (this could mean the most recent or the most common).`, specified locale (this could mean the most recent or the most common).`
}, },
...fieldWithID('type', { ...fieldWithID('type', {
description: `The type or purpose of the alias whether it is a variant, description: `The type or purpose of the alias whether it is a variant,
search hint, etc.`, search hint, etc.`
}), })
}), })
}); })
export const aliases = {
type: new GraphQLList(Alias),
description: `[Aliases](https://musicbrainz.org/doc/Aliases) are used to store
alternate names or misspellings.`,
resolve: createSubqueryResolver(),
};

View file

@ -1,29 +1,26 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLString, GraphQLList } from 'graphql'
import { Node } from './node.js'; import Node from './node'
import { Entity } from './entity.js'; import Entity from './entity'
import { import {
id, id,
mbid, mbid,
name, name,
sortName, sortName,
disambiguation, disambiguation,
aliases,
artists,
events,
labels,
places,
releases,
relationships,
collections,
tags,
fieldWithID, fieldWithID,
connectionWithExtras, connectionWithExtras
linkedQuery, } from './helpers'
} from './helpers.js';
import { events } from './event.js';
import { aliases } from './alias.js';
import { artists } from './artist.js';
import { labels } from './label.js';
import { places } from './place.js';
import { releases } from './release.js';
import { relationships } from './relationship.js';
import { collections } from './collection.js';
import { tags } from './tag.js';
const { GraphQLObjectType, GraphQLString, GraphQLList } = GraphQL; const Area = new GraphQLObjectType({
export const Area = new GraphQLObjectType({
name: 'Area', name: 'Area',
description: `[Areas](https://musicbrainz.org/doc/Area) are geographic regions description: `[Areas](https://musicbrainz.org/doc/Area) are geographic regions
or settlements (countries, cities, or the like).`, or settlements (countries, cities, or the like).`,
@ -39,22 +36,11 @@ or settlements (countries, cities, or the like).`,
type: new GraphQLList(GraphQLString), type: new GraphQLList(GraphQLString),
description: `[ISO 3166 codes](https://en.wikipedia.org/wiki/ISO_3166) are description: `[ISO 3166 codes](https://en.wikipedia.org/wiki/ISO_3166) are
the codes assigned by ISO to countries and subdivisions.`, the codes assigned by ISO to countries and subdivisions.`,
args: { resolve: data => data['iso-3166-1-codes']
standard: {
type: GraphQLString,
description: `Specify the particular ISO standard codes to retrieve.
Available ISO standards are 3166-1, 3166-2, and 3166-3.`,
defaultValue: '3166-1',
},
},
resolve: (data, args) => {
const { standard = '3166-1' } = args;
return data[`iso-${standard}-codes`];
},
}, },
...fieldWithID('type', { ...fieldWithID('type', {
description: `The type of area (country, city, etc. see the [possible description: `The type of area (country, city, etc. see the [possible
values](https://musicbrainz.org/doc/Area)).`, values](https://musicbrainz.org/doc/Area)).`
}), }),
artists, artists,
events, events,
@ -63,10 +49,9 @@ values](https://musicbrainz.org/doc/Area)).`,
releases, releases,
relationships, relationships,
collections, collections,
tags, tags
}), })
}); })
export const AreaConnection = connectionWithExtras(Area); export const AreaConnection = connectionWithExtras(Area)
export default Area
export const areas = linkedQuery(AreaConnection);

View file

@ -1,10 +1,7 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLString } from 'graphql/type'
import { Artist } from './artist.js'; import Artist from './artist'
import { createSubqueryResolver } from '../resolvers.js';
const { GraphQLObjectType, GraphQLString, GraphQLList } = GraphQL; export default new GraphQLObjectType({
export const ArtistCredit = new GraphQLObjectType({
name: 'ArtistCredit', name: 'ArtistCredit',
description: `[Artist credits](https://musicbrainz.org/doc/Artist_Credits) description: `[Artist credits](https://musicbrainz.org/doc/Artist_Credits)
indicate who is the main credited artist (or artists) for releases, release indicate who is the main credited artist (or artists) for releases, release
@ -16,42 +13,24 @@ track, etc., and join phrases between them.`,
type: Artist, type: Artist,
description: `The entity representing the artist referenced in the description: `The entity representing the artist referenced in the
credits.`, credits.`,
resolve: (source) => { resolve: source => {
const { artist } = source; const { artist } = source
if (artist) { if (artist) {
artist._type = 'artist'; artist._type = 'artist'
} }
return artist; return artist
}, }
}, },
name: { name: {
type: GraphQLString, type: GraphQLString,
description: `The name of the artist as credited in the specific release, description: `The name of the artist as credited in the specific release,
track, etc.`, track, etc.`
}, },
joinPhrase: { joinPhrase: {
type: GraphQLString, type: GraphQLString,
description: `Join phrases might include words and/or punctuation to description: `Join phrases might include words and/or punctuation to
separate artist names as they appear on the release, track, etc.`, separate artist names as they appear on the release, track, etc.`,
resolve: (data) => data.joinphrase, resolve: data => data.joinphrase
}, }
}), })
}); })
export const artistCredits = {
type: new GraphQLList(ArtistCredit),
description: 'The main credited artist(s).',
resolve: createSubqueryResolver({
inc: 'artist-credits',
key: 'artist-credit',
}),
};
export const artistCredit = {
...artistCredits,
deprecationReason: `The \`artistCredit\` field has been renamed to
\`artistCredits\`, since it is a list of credits and is referred to in the
plural form throughout the MusicBrainz documentation. This field is deprecated
and will be removed in a major release in the future. Use the equivalent
\`artistCredits\` field.`,
};

View file

@ -1,18 +1,8 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLString, GraphQLList } from 'graphql/type'
import { Node } from './node.js'; import Node from './node'
import { Entity } from './entity.js'; import Entity from './entity'
import { Area } from './area.js'; import Area from './area'
import { aliases } from './alias.js'; import { IPI, ISNI } from './scalars'
import { collections } from './collection.js';
import { lifeSpan } from './life-span.js';
import { recordings } from './recording.js';
import { releases } from './release.js';
import { releaseGroups } from './release-group.js';
import { works } from './work.js';
import { relationships } from './relationship.js';
import { rating } from './rating.js';
import { tags } from './tag.js';
import { IPI, ISNI } from './scalars.js';
import { import {
resolveWithFallback, resolveWithFallback,
fieldWithID, fieldWithID,
@ -21,13 +11,20 @@ import {
name, name,
sortName, sortName,
disambiguation, disambiguation,
connectionWithExtras, aliases,
linkedQuery, lifeSpan,
} from './helpers.js'; recordings,
releases,
releaseGroups,
works,
relationships,
collections,
rating,
tags,
connectionWithExtras
} from './helpers'
const { GraphQLObjectType, GraphQLString, GraphQLList } = GraphQL; const Artist = new GraphQLObjectType({
export const Artist = new GraphQLObjectType({
name: 'Artist', name: 'Artist',
description: `An [artist](https://musicbrainz.org/doc/Artist) is generally a description: `An [artist](https://musicbrainz.org/doc/Artist) is generally a
musician, group of musicians, or other music professional (like a producer or musician, group of musicians, or other music professional (like a producer or
@ -45,42 +42,42 @@ even a fictional character.`,
country: { country: {
type: GraphQLString, type: GraphQLString,
description: `The country with which an artist is primarily identified. It description: `The country with which an artist is primarily identified. It
is often, but not always, its birth/formation country.`, is often, but not always, its birth/formation country.`
}, },
area: { area: {
type: Area, type: Area,
description: `The area with which an artist is primarily identified. It description: `The area with which an artist is primarily identified. It
is often, but not always, its birth/formation country.`, is often, but not always, its birth/formation country.`
}, },
beginArea: { beginArea: {
type: Area, type: Area,
description: `The area in which an artist began their career (or where description: `The area in which an artist began their career (or where
they were born, if the artist is a person).`, they were born, if the artist is a person).`,
resolve: resolveWithFallback(['begin-area', 'begin_area']), resolve: resolveWithFallback(['begin-area', 'begin_area'])
}, },
endArea: { endArea: {
type: Area, type: Area,
description: `The area in which an artist ended their career (or where description: `The area in which an artist ended their career (or where
they died, if the artist is a person).`, they died, if the artist is a person).`,
resolve: resolveWithFallback(['end-area', 'end_area']), resolve: resolveWithFallback(['end-area', 'end_area'])
}, },
lifeSpan, lifeSpan,
...fieldWithID('gender', { ...fieldWithID('gender', {
description: `Whether a person or character identifies as male, female, or description: `Whether a person or character identifies as male, female, or
neither. Groups do not have genders.`, neither. Groups do not have genders.`
}), }),
...fieldWithID('type', { ...fieldWithID('type', {
description: 'Whether an artist is a person, a group, or something else.', description: 'Whether an artist is a person, a group, or something else.'
}), }),
ipis: { ipis: {
type: new GraphQLList(IPI), type: new GraphQLList(IPI),
description: `List of [Interested Parties Information](https://musicbrainz.org/doc/IPI) description: `List of [Interested Parties Information](https://musicbrainz.org/doc/IPI)
(IPI) codes for the artist.`, (IPI) codes for the artist.`
}, },
isnis: { isnis: {
type: new GraphQLList(ISNI), type: new GraphQLList(ISNI),
description: `List of [International Standard Name Identifier](https://musicbrainz.org/doc/ISNI) description: `List of [International Standard Name Identifier](https://musicbrainz.org/doc/ISNI)
(ISNI) codes for the artist.`, (ISNI) codes for the artist.`
}, },
recordings, recordings,
releases, releases,
@ -89,10 +86,9 @@ neither. Groups do not have genders.`,
relationships, relationships,
collections, collections,
rating, rating,
tags, tags
}), })
}); })
export const ArtistConnection = connectionWithExtras(Artist); export const ArtistConnection = connectionWithExtras(Artist)
export default Artist
export const artists = linkedQuery(ArtistConnection);

View file

@ -1,31 +1,28 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLNonNull, GraphQLString } from 'graphql/type'
import { Node } from './node.js'; import Node from './node'
import { Entity } from './entity.js'; import Entity from './entity'
import { import {
id, id,
mbid, mbid,
name, name,
areas,
artists,
events,
instruments,
labels,
places,
recordings,
releases,
releaseGroups,
series,
works,
fieldWithID, fieldWithID,
resolveHyphenated, resolveHyphenated,
createCollectionField, createCollectionField,
connectionWithExtras, connectionWithExtras
linkedQuery, } from './helpers'
} from './helpers.js';
import { areas } from './area.js';
import { artists } from './artist.js';
import { events } from './event.js';
import { instruments } from './instrument.js';
import { labels } from './label.js';
import { places } from './place.js';
import { recordings } from './recording.js';
import { releases } from './release.js';
import { releaseGroups } from './release-group.js';
import { series } from './series.js';
import { works } from './work.js';
const { GraphQLObjectType, GraphQLNonNull, GraphQLString } = GraphQL; const Collection = new GraphQLObjectType({
export const Collection = new GraphQLObjectType({
name: 'Collection', name: 'Collection',
description: `[Collections](https://musicbrainz.org/doc/Collections) are description: `[Collections](https://musicbrainz.org/doc/Collections) are
lists of entities that users can create.`, lists of entities that users can create.`,
@ -36,15 +33,15 @@ lists of entities that users can create.`,
name, name,
editor: { editor: {
type: new GraphQLNonNull(GraphQLString), type: new GraphQLNonNull(GraphQLString),
description: 'The username of the editor who created the collection.', description: 'The username of the editor who created the collection.'
}, },
entityType: { entityType: {
type: new GraphQLNonNull(GraphQLString), type: new GraphQLNonNull(GraphQLString),
description: 'The type of entity listed in the collection.', description: 'The type of entity listed in the collection.',
resolve: resolveHyphenated, resolve: resolveHyphenated
}, },
...fieldWithID('type', { ...fieldWithID('type', {
description: 'The type of collection.', description: 'The type of collection.'
}), }),
areas: createCollectionField(areas), areas: createCollectionField(areas),
artists: createCollectionField(artists), artists: createCollectionField(artists),
@ -56,12 +53,9 @@ lists of entities that users can create.`,
releases: createCollectionField(releases), releases: createCollectionField(releases),
releaseGroups: createCollectionField(releaseGroups), releaseGroups: createCollectionField(releaseGroups),
series: createCollectionField(series), series: createCollectionField(series),
works: createCollectionField(works), works: createCollectionField(works)
}), })
}); })
export const CollectionConnection = connectionWithExtras(Collection); export const CollectionConnection = connectionWithExtras(Collection)
export default Collection
export const collections = linkedQuery(CollectionConnection, {
description: 'A list of collections containing this entity.',
});

View file

@ -1,15 +1,17 @@
import GraphQL from 'graphql'; import {
import GraphQLRelay from 'graphql-relay'; GraphQLObjectType,
import { Node } from './node.js'; GraphQLNonNull,
import { DiscID } from './scalars.js'; GraphQLList,
import { ReleaseConnection } from './release.js'; GraphQLInt
import { resolveDiscReleases } from '../resolvers.js'; } from 'graphql/type'
import { id, resolveHyphenated } from './helpers.js'; import { forwardConnectionArgs } from 'graphql-relay'
import Node from './node'
import { DiscID } from './scalars'
import { ReleaseConnection } from './release'
import { resolveDiscReleases } from '../resolvers'
import { id, resolveHyphenated } from './helpers'
const { GraphQLObjectType, GraphQLNonNull, GraphQLList, GraphQLInt } = GraphQL; export default new GraphQLObjectType({
const { forwardConnectionArgs } = GraphQLRelay;
export const Disc = new GraphQLObjectType({
name: 'Disc', name: 'Disc',
description: `Information about the physical CD and releases associated with a description: `Information about the physical CD and releases associated with a
particular [disc ID](https://musicbrainz.org/doc/Disc_ID).`, particular [disc ID](https://musicbrainz.org/doc/Disc_ID).`,
@ -19,26 +21,26 @@ particular [disc ID](https://musicbrainz.org/doc/Disc_ID).`,
discID: { discID: {
type: new GraphQLNonNull(DiscID), type: new GraphQLNonNull(DiscID),
description: `The [disc ID](https://musicbrainz.org/doc/Disc_ID) of this disc.`, description: `The [disc ID](https://musicbrainz.org/doc/Disc_ID) of this disc.`,
resolve: (disc) => disc.id, resolve: disc => disc.id
}, },
offsetCount: { offsetCount: {
type: new GraphQLNonNull(GraphQLInt), type: new GraphQLNonNull(GraphQLInt),
description: 'The number of offsets (tracks) on the disc.', description: 'The number of offsets (tracks) on the disc.',
resolve: resolveHyphenated, resolve: resolveHyphenated
}, },
offsets: { offsets: {
type: new GraphQLList(GraphQLInt), type: new GraphQLList(GraphQLInt),
description: 'The sector offset of each track on the disc.', description: 'The sector offset of each track on the disc.'
}, },
sectors: { sectors: {
type: new GraphQLNonNull(GraphQLInt), type: new GraphQLNonNull(GraphQLInt),
description: 'The sector offset of the lead-out (the end of the disc).', description: 'The sector offset of the lead-out (the end of the disc).'
}, },
releases: { releases: {
type: ReleaseConnection, type: ReleaseConnection,
description: 'The list of releases linked to this disc ID.', description: 'The list of releases linked to this disc ID.',
args: forwardConnectionArgs, args: forwardConnectionArgs,
resolve: resolveDiscReleases, resolve: resolveDiscReleases
}, }
}), })
}); })

View file

@ -1,13 +1,29 @@
import GraphQL from 'graphql'; import { GraphQLInterfaceType } from 'graphql'
import { mbid, connectionWithExtras, resolveType } from './helpers.js'; import { mbid, connectionWithExtras } from './helpers'
const { GraphQLInterfaceType } = GraphQL; const debug = require('debug')('graphbrainz:types/entity')
export const Entity = new GraphQLInterfaceType({ const Entity = new GraphQLInterfaceType({
name: 'Entity', name: 'Entity',
description: 'An entity in the MusicBrainz schema.', description: 'An entity in the MusicBrainz schema.',
resolveType, resolveType(value, context, info) {
fields: () => ({ mbid }), if (value._type) {
}); let originalType
try {
originalType = require(`./${value._type}`).default
} catch (err) {
debug(`Failed to load type: ${value._type}`)
return
}
// Don't use `originalType`! The schema may have been extended in which
// case the types have all been replaced. Instead, find the current type
// of the same name.
const typeMap = info.schema.getTypeMap()
return typeMap[originalType.name]
}
},
fields: () => ({ mbid })
})
export const EntityConnection = connectionWithExtras(Entity); export const EntityConnection = connectionWithExtras(Entity)
export default Entity

View file

@ -1,6 +1,4 @@
import GraphQL from 'graphql'; import { GraphQLEnumType } from 'graphql/type'
const { GraphQLEnumType } = GraphQL;
export const ArtistType = new GraphQLEnumType({ export const ArtistType = new GraphQLEnumType({
name: 'ArtistType', name: 'ArtistType',
@ -10,37 +8,37 @@ etc.`,
PERSON: { PERSON: {
name: 'Person', name: 'Person',
description: 'This indicates an individual person.', description: 'This indicates an individual person.',
value: 'Person', value: 'Person'
}, },
GROUP: { GROUP: {
name: 'Group', name: 'Group',
description: `This indicates a group of people that may or may not have a description: `This indicates a group of people that may or may not have a
distinctive name.`, distinctive name.`,
value: 'Group', value: 'Group'
}, },
ORCHESTRA: { ORCHESTRA: {
name: 'Orchestra', name: 'Orchestra',
description: description:
'This indicates an orchestra (a large instrumental ensemble).', 'This indicates an orchestra (a large instrumental ensemble).',
value: 'Orchestra', value: 'Orchestra'
}, },
CHOIR: { CHOIR: {
name: 'Choir', name: 'Choir',
description: 'This indicates a choir/chorus (a large vocal ensemble).', description: 'This indicates a choir/chorus (a large vocal ensemble).',
value: 'Choir', value: 'Choir'
}, },
CHARACTER: { CHARACTER: {
name: 'Character', name: 'Character',
description: 'This indicates an individual fictional character.', description: 'This indicates an individual fictional character.',
value: 'Character', value: 'Character'
}, },
OTHER: { OTHER: {
name: 'Other', name: 'Other',
description: 'An artist which does not fit into the other categories.', description: 'An artist which does not fit into the other categories.',
value: 'Other', value: 'Other'
}, }
}, }
}); })
export const CoverArtImageSize = new GraphQLEnumType({ export const CoverArtImageSize = new GraphQLEnumType({
name: 'CoverArtImageSize', name: 'CoverArtImageSize',
@ -50,20 +48,20 @@ Archive](https://musicbrainz.org/doc/Cover_Art_Archive).`,
SMALL: { SMALL: {
name: 'Small', name: 'Small',
description: 'A maximum dimension of 250px.', description: 'A maximum dimension of 250px.',
value: 250, value: 250
}, },
LARGE: { LARGE: {
name: 'Large', name: 'Large',
description: 'A maximum dimension of 500px.', description: 'A maximum dimension of 500px.',
value: 500, value: 500
}, },
FULL: { FULL: {
name: 'Full', name: 'Full',
description: 'The images original dimensions, with no maximum.', description: 'The images original dimensions, with no maximum.',
value: null, value: null
}, }
}, }
}); })
export const ReleaseStatus = new GraphQLEnumType({ export const ReleaseStatus = new GraphQLEnumType({
name: 'ReleaseStatus', name: 'ReleaseStatus',
@ -74,29 +72,29 @@ bootleg, etc.`,
name: 'Official', name: 'Official',
description: `Any release officially sanctioned by the artist and/or their description: `Any release officially sanctioned by the artist and/or their
record company. (Most releases will fit into this category.)`, record company. (Most releases will fit into this category.)`,
value: 'Official', value: 'Official'
}, },
PROMOTION: { PROMOTION: {
name: 'Promotion', name: 'Promotion',
description: `A giveaway release or a release intended to promote an description: `A giveaway release or a release intended to promote an
upcoming official release, e.g. prerelease albums or releases included with a upcoming official release, e.g. prerelease albums or releases included with a
magazine.`, magazine.`,
value: 'Promotion', value: 'Promotion'
}, },
BOOTLEG: { BOOTLEG: {
name: 'Bootleg', name: 'Bootleg',
description: `An unofficial/underground release that was not sanctioned by description: `An unofficial/underground release that was not sanctioned by
the artist and/or the record company.`, the artist and/or the record company.`,
value: 'Bootleg', value: 'Bootleg'
}, },
PSEUDORELEASE: { PSEUDORELEASE: {
name: 'Pseudo-Release', name: 'Pseudo-Release',
description: `A pseudo-release is a duplicate release for description: `A pseudo-release is a duplicate release for
translation/transliteration purposes.`, translation/transliteration purposes.`,
value: 'Pseudo-Release', value: 'Pseudo-Release'
}, }
}, }
}); })
export const ReleaseGroupType = new GraphQLEnumType({ export const ReleaseGroupType = new GraphQLEnumType({
name: 'ReleaseGroupType', name: 'ReleaseGroupType',
@ -109,14 +107,14 @@ etc.`,
release, generally consists of previously unreleased material (unless this type release, generally consists of previously unreleased material (unless this type
is combined with secondary types which change that, such as Compilation). This is combined with secondary types which change that, such as Compilation). This
includes album re-issues, with or without bonus tracks.`, includes album re-issues, with or without bonus tracks.`,
value: 'Album', value: 'Album'
}, },
SINGLE: { SINGLE: {
name: 'Single', name: 'Single',
description: `A single typically has one main song and possibly a handful description: `A single typically has one main song and possibly a handful
of additional tracks or remixes of the main track. A single is usually named of additional tracks or remixes of the main track. A single is usually named
after its main song.`, after its main song.`,
value: 'Single', value: 'Single'
}, },
EP: { EP: {
name: 'EP', name: 'EP',
@ -126,57 +124,57 @@ full length release (an LP or “Long Play”) and the tracks are usually exclus
to the EP, in other words the tracks dont come from a previously issued to the EP, in other words the tracks dont come from a previously issued
release. EP is fairly difficult to define; usually it should only be assumed release. EP is fairly difficult to define; usually it should only be assumed
that a release is an EP if the artist defines it as such.`, that a release is an EP if the artist defines it as such.`,
value: 'EP', value: 'EP'
}, },
OTHER: { OTHER: {
name: 'Other', name: 'Other',
description: 'Any release that does not fit any of the other categories.', description: 'Any release that does not fit any of the other categories.',
value: 'Other', value: 'Other'
}, },
BROADCAST: { BROADCAST: {
name: 'Broadcast', name: 'Broadcast',
description: `An episodic release that was originally broadcast via radio, description: `An episodic release that was originally broadcast via radio,
television, or the Internet, including podcasts.`, television, or the Internet, including podcasts.`,
value: 'Broadcast', value: 'Broadcast'
}, },
COMPILATION: { COMPILATION: {
name: 'Compilation', name: 'Compilation',
description: `A compilation is a collection of previously released tracks description: `A compilation is a collection of previously released tracks
by one or more artists.`, by one or more artists.`,
value: 'Compilation', value: 'Compilation'
}, },
SOUNDTRACK: { SOUNDTRACK: {
name: 'Soundtrack', name: 'Soundtrack',
description: `A soundtrack is the musical score to a movie, TV series, description: `A soundtrack is the musical score to a movie, TV series,
stage show, computer game, etc.`, stage show, computer game, etc.`,
value: 'Soundtrack', value: 'Soundtrack'
}, },
SPOKENWORD: { SPOKENWORD: {
name: 'Spoken Word', name: 'Spoken Word',
description: 'A non-music spoken word release.', description: 'A non-music spoken word release.',
value: 'Spoken Word', value: 'Spoken Word'
}, },
INTERVIEW: { INTERVIEW: {
name: 'Interview', name: 'Interview',
description: `An interview release contains an interview, generally with description: `An interview release contains an interview, generally with
an artist.`, an artist.`,
value: 'Interview', value: 'Interview'
}, },
AUDIOBOOK: { AUDIOBOOK: {
name: 'Audiobook', name: 'Audiobook',
description: 'An audiobook is a book read by a narrator without music.', description: 'An audiobook is a book read by a narrator without music.',
value: 'Audiobook', value: 'Audiobook'
}, },
LIVE: { LIVE: {
name: 'Live', name: 'Live',
description: 'A release that was recorded live.', description: 'A release that was recorded live.',
value: 'Live', value: 'Live'
}, },
REMIX: { REMIX: {
name: 'Remix', name: 'Remix',
description: `A release that was (re)mixed from previously released description: `A release that was (re)mixed from previously released
material.`, material.`,
value: 'Remix', value: 'Remix'
}, },
DJMIX: { DJMIX: {
name: 'DJ-mix', name: 'DJ-mix',
@ -185,7 +183,7 @@ after the other, each one modified so that they blend together into a continuous
flow of music. A DJ mix release requires that the recordings be modified in some flow of music. A DJ mix release requires that the recordings be modified in some
manner, and the DJ who does this modification is usually (although not always) manner, and the DJ who does this modification is usually (although not always)
credited in a fairly prominent way.`, credited in a fairly prominent way.`,
value: 'DJ-mix', value: 'DJ-mix'
}, },
MIXTAPE: { MIXTAPE: {
name: 'Mixtape/Street', name: 'Mixtape/Street',
@ -201,18 +199,18 @@ significant proportion of new material, including original production or
original vocals over top of other artists instrumentals. They are distinct from original vocals over top of other artists instrumentals. They are distinct from
demos in that they are designed for release directly to the public and fans, not demos in that they are designed for release directly to the public and fans, not
to labels.`, to labels.`,
value: 'Mixtape/Street', value: 'Mixtape/Street'
}, },
DEMO: { DEMO: {
name: 'Demo', name: 'Demo',
description: `A release that was recorded for limited circulation or description: `A release that was recorded for limited circulation or
reference use rather than for general public release.`, reference use rather than for general public release.`,
value: 'Demo', value: 'Demo'
}, },
NAT: { NAT: {
name: 'Non-Album Track', name: 'Non-Album Track',
description: 'A non-album track (special case).', description: 'A non-album track (special case).',
value: 'NAT', value: 'NAT'
}, }
}, }
}); })

View file

@ -1,26 +1,23 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLString, GraphQLBoolean } from 'graphql/type'
import { Node } from './node.js'; import Node from './node'
import { Entity } from './entity.js'; import Entity from './entity'
import { Time } from './scalars.js'; import { Time } from './scalars'
import { import {
fieldWithID, fieldWithID,
id, id,
mbid, mbid,
name, name,
disambiguation, disambiguation,
connectionWithExtras, aliases,
linkedQuery, lifeSpan,
} from './helpers.js'; relationships,
import { aliases } from './alias.js'; collections,
import { collections } from './collection.js'; rating,
import { lifeSpan } from './life-span.js'; tags,
import { rating } from './rating.js'; connectionWithExtras
import { relationships } from './relationship.js'; } from './helpers'
import { tags } from './tag.js';
const { GraphQLObjectType, GraphQLString, GraphQLBoolean } = GraphQL; const Event = new GraphQLObjectType({
export const Event = new GraphQLObjectType({
name: 'Event', name: 'Event',
description: `An [event](https://musicbrainz.org/doc/Event) refers to an description: `An [event](https://musicbrainz.org/doc/Event) refers to an
organised event which people can attend, and is relevant to MusicBrainz. organised event which people can attend, and is relevant to MusicBrainz.
@ -35,29 +32,28 @@ Generally this means live performances, like concerts and festivals.`,
lifeSpan, lifeSpan,
time: { time: {
type: Time, type: Time,
description: 'The start time of the event.', description: 'The start time of the event.'
}, },
cancelled: { cancelled: {
type: GraphQLBoolean, type: GraphQLBoolean,
description: 'Whether or not the event took place.', description: 'Whether or not the event took place.'
}, },
setlist: { setlist: {
type: GraphQLString, type: GraphQLString,
description: `A list of songs performed, optionally including links to description: `A list of songs performed, optionally including links to
artists and works. See the [setlist documentation](https://musicbrainz.org/doc/Event/Setlist) artists and works. See the [setlist documentation](https://musicbrainz.org/doc/Event/Setlist)
for syntax and examples.`, for syntax and examples.`
}, },
...fieldWithID('type', { ...fieldWithID('type', {
description: description:
'What kind of event the event is, e.g. concert, festival, etc.', 'What kind of event the event is, e.g. concert, festival, etc.'
}), }),
relationships, relationships,
collections, collections,
rating, rating,
tags, tags
}), })
}); })
export const EventConnection = connectionWithExtras(Event); export const EventConnection = connectionWithExtras(Event)
export default Event
export const events = linkedQuery(EventConnection);

View file

@ -1,134 +1,298 @@
import GraphQL from 'graphql'; import dashify from 'dashify'
import GraphQLRelay from 'graphql-relay'; import pascalCase from 'pascalcase'
import { MBID } from './scalars.js'; import {
import { ReleaseGroupType, ReleaseStatus } from './enums.js'; GraphQLObjectType,
import { resolveLinked } from '../resolvers.js'; GraphQLString,
import { toDashed, toPascal, toSingular, toPlural, toWords } from '../util.js'; GraphQLInt,
GraphQLList,
const { GraphQLString, GraphQLInt, GraphQLList, GraphQLNonNull } = GraphQL; GraphQLNonNull
const { } from 'graphql'
import {
globalIdField, globalIdField,
connectionArgs,
connectionDefinitions, connectionDefinitions,
forwardConnectionArgs, connectionFromArray,
} = GraphQLRelay; forwardConnectionArgs
} from 'graphql-relay'
import { MBID } from './scalars'
import { ReleaseGroupType, ReleaseStatus } from './enums'
import Alias from './alias'
import ArtistCredit from './artist-credit'
import { AreaConnection } from './area'
import { ArtistConnection } from './artist'
import { CollectionConnection } from './collection'
import { EventConnection } from './event'
import { InstrumentConnection } from './instrument'
import { LabelConnection } from './label'
import LifeSpan from './life-span'
import { PlaceConnection } from './place'
import Rating from './rating'
import { RecordingConnection } from './recording'
import { RelationshipConnection } from './relationship'
import { ReleaseConnection } from './release'
import { ReleaseGroupConnection } from './release-group'
import { SeriesConnection } from './series'
import { TagConnection } from './tag'
import { WorkConnection } from './work'
import {
resolveLinked,
resolveRelationship,
createSubqueryResolver,
includeRelationships
} from '../resolvers'
const TYPE_NAMES = { export const toPascal = pascalCase
discid: 'Disc', export const toDashed = dashify
url: 'URL',
};
export function resolveType(value, context, info) { export function toPlural(name) {
const typeName = TYPE_NAMES[value._type] || toPascal(value._type); return name.endsWith('s') ? name : name + 's'
const typeMap = info.schema.getTypeMap(); }
return typeMap[typeName];
export function toSingular(name) {
return name.endsWith('s') && !/series/i.test(name) ? name.slice(0, -1) : name
}
export function toWords(name) {
return toPascal(name).replace(/([^A-Z])?([A-Z]+)/g, (match, tail, head) => {
tail = tail ? tail + ' ' : ''
head = head.length > 1 ? head : head.toLowerCase()
return `${tail}${head}`
})
} }
export function resolveHyphenated(obj, args, context, info) { export function resolveHyphenated(obj, args, context, info) {
const name = toDashed(info.fieldName); const name = toDashed(info.fieldName)
return obj[name]; return obj[name]
} }
export function resolveWithFallback(keys) { export function resolveWithFallback(keys) {
return (obj) => { return obj => {
for (let i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
const key = keys[i]; const key = keys[i]
if (key in obj) { if (key in obj) {
return obj[key]; return obj[key]
} }
} }
}; }
} }
export function fieldWithID(name, config = {}) { export function fieldWithID(name, config = {}) {
config = { config = {
type: GraphQLString, type: GraphQLString,
resolve: resolveHyphenated, resolve: resolveHyphenated,
...config, ...config
}; }
const isPlural = config.type instanceof GraphQLList; const isPlural = config.type instanceof GraphQLList
const singularName = isPlural ? toSingular(name) : name; const singularName = isPlural ? toSingular(name) : name
const idName = isPlural ? `${singularName}IDs` : `${name}ID`; const idName = isPlural ? `${singularName}IDs` : `${name}ID`
const s = isPlural ? 's' : ''; const s = isPlural ? 's' : ''
const idConfig = { const idConfig = {
type: isPlural ? new GraphQLList(MBID) : MBID, type: isPlural ? new GraphQLList(MBID) : MBID,
description: `The MBID${s} associated with the value${s} of the \`${name}\` description: `The MBID${s} associated with the value${s} of the \`${name}\`
field.`, field.`,
resolve: (entity, args, { loaders }) => { resolve: (entity, args, { loaders }) => {
const fieldName = toDashed(idName); const fieldName = toDashed(idName)
if (fieldName in entity) { if (fieldName in entity) {
return entity[fieldName]; return entity[fieldName]
} }
return loaders.lookup return loaders.lookup
.load([entity._type, entity.id]) .load([entity._type, entity.id])
.then((data) => data[fieldName]); .then(data => data[fieldName])
}, }
}; }
return { return {
[name]: config, [name]: config,
[idName]: idConfig, [idName]: idConfig
}; }
} }
export function createCollectionField(config) { export function createCollectionField(config) {
const typeName = toPlural(toWords(config.type.name.slice(0, -10))); const typeName = toPlural(toWords(config.type.name.slice(0, -10)))
return { return {
...config, ...config,
description: `The list of ${typeName} found in this collection.`, description: `The list of ${typeName} found in this collection.`
}; }
} }
export const id = globalIdField(); export const id = globalIdField()
export const mbid = { export const mbid = {
type: new GraphQLNonNull(MBID), type: new GraphQLNonNull(MBID),
description: 'The MBID of the entity.', description: 'The MBID of the entity.',
resolve: (entity) => entity.id, resolve: entity => entity.id
}; }
export const name = { export const name = {
type: GraphQLString, type: GraphQLString,
description: 'The official name of the entity.', description: 'The official name of the entity.'
}; }
export const sortName = { export const sortName = {
type: GraphQLString, type: GraphQLString,
description: `The string to use for the purpose of ordering by name (for description: `The string to use for the purpose of ordering by name (for
example, by moving articles like the to the end or a persons last name to example, by moving articles like the to the end or a persons last name to
the front).`, the front).`,
resolve: resolveHyphenated, resolve: resolveHyphenated
}; }
export const title = { export const title = {
type: GraphQLString, type: GraphQLString,
description: 'The official title of the entity.', description: 'The official title of the entity.'
}; }
export const disambiguation = { export const disambiguation = {
type: GraphQLString, type: GraphQLString,
description: description: 'A comment used to help distinguish identically named entitites.'
'A comment used to help distinguish identically named entitites.', }
}; export const lifeSpan = {
type: LifeSpan,
description: `The begin and end dates of the entitys existence. Its exact
meaning depends on the type of entity.`,
resolve: resolveHyphenated
}
export function linkedQuery(connectionType, { args, ...config } = {}) { function linkedQuery(connectionType, { args, ...config } = {}) {
const typeName = toPlural(toWords(connectionType.name.slice(0, -10))); const typeName = toPlural(toWords(connectionType.name.slice(0, -10)))
return { return {
type: connectionType, type: connectionType,
description: `A list of ${typeName} linked to this entity.`, description: `A list of ${typeName} linked to this entity.`,
args: { args: {
...args, ...args,
...forwardConnectionArgs, ...forwardConnectionArgs
}, },
resolve: resolveLinked, resolve: resolveLinked,
...config, ...config
}; }
} }
export const relationship = {
type: RelationshipConnection,
description: 'A list of relationships between these two entity types.',
args: {
direction: {
type: GraphQLString,
description: 'Filter by the relationship direction.'
},
...fieldWithID('type', {
description: 'Filter by the relationship type.'
}),
...connectionArgs
},
resolve: resolveRelationship
}
export const relationships = {
type: new GraphQLObjectType({
name: 'Relationships',
description: 'Lists of entity relationships for each entity type.',
fields: () => ({
areas: relationship,
artists: relationship,
events: relationship,
instruments: relationship,
labels: relationship,
places: relationship,
recordings: relationship,
releases: relationship,
releaseGroups: relationship,
series: relationship,
urls: relationship,
works: relationship
})
}),
description: 'Relationships between this entity and other entitites.',
resolve: (entity, args, { loaders }, info) => {
let promise
if (entity.relations != null) {
promise = Promise.resolve(entity)
} else {
const entityType = toDashed(info.parentType.name)
const id = entity.id
const params = includeRelationships({}, info)
promise = loaders.lookup.load([entityType, id, params])
}
return promise.then(entity => entity.relations)
}
}
export const aliases = {
type: new GraphQLList(Alias),
description: `[Aliases](https://musicbrainz.org/doc/Aliases) are used to store
alternate names or misspellings.`,
resolve: createSubqueryResolver()
}
export const artistCredits = {
type: new GraphQLList(ArtistCredit),
description: 'The main credited artist(s).',
resolve: createSubqueryResolver({ key: 'artist-credit' })
}
export const artistCredit = {
...artistCredits,
deprecationReason: `The \`artistCredit\` field has been renamed to
\`artistCredits\`, since it is a list of credits and is referred to in the
plural form throughout the MusicBrainz documentation. This field is deprecated
and will be removed in a major release in the future. Use the equivalent
\`artistCredits\` field.`
}
export const rating = {
type: Rating,
description: 'The rating users have given to this entity.',
resolve: createSubqueryResolver({ inc: 'ratings' })
}
export const releaseGroupType = {
type: new GraphQLList(ReleaseGroupType),
description: 'Filter by one or more release group types.'
}
export const releaseStatus = {
type: new GraphQLList(ReleaseStatus),
description: 'Filter by one or more release statuses.'
}
export const areas = linkedQuery(AreaConnection)
export const artists = linkedQuery(ArtistConnection)
export const collections = linkedQuery(CollectionConnection, {
description: 'A list of collections containing this entity.'
})
export const events = linkedQuery(EventConnection)
export const instruments = linkedQuery(InstrumentConnection)
export const labels = linkedQuery(LabelConnection)
export const places = linkedQuery(PlaceConnection)
export const recordings = linkedQuery(RecordingConnection)
export const releases = linkedQuery(ReleaseConnection, {
args: {
type: releaseGroupType,
status: releaseStatus
}
})
export const releaseGroups = linkedQuery(ReleaseGroupConnection, {
args: {
type: releaseGroupType
}
})
export const series = linkedQuery(SeriesConnection)
export const tags = linkedQuery(TagConnection, {
resolve: createSubqueryResolver({}, (value = [], args) => {
const connection = connectionFromArray(value, args)
return {
nodes: connection.edges.map(edge => edge.node),
totalCount: value.length,
...connection
}
})
})
export const works = linkedQuery(WorkConnection)
export const totalCount = { export const totalCount = {
type: GraphQLInt, type: GraphQLInt,
description: `A count of the total number of items in this connection, description: `A count of the total number of items in this connection,
ignoring pagination.`, ignoring pagination.`
}; }
export const score = { export const score = {
type: GraphQLInt, type: GraphQLInt,
description: `The relevancy score (0100) assigned by the search engine, if description: `The relevancy score (0100) assigned by the search engine, if
these results were found through a search.`, these results were found through a search.`
}; }
export function connectionWithExtras(nodeType) { export function connectionWithExtras(nodeType) {
return connectionDefinitions({ return connectionDefinitions({
@ -137,20 +301,10 @@ export function connectionWithExtras(nodeType) {
nodes: { nodes: {
type: new GraphQLList(nodeType), type: new GraphQLList(nodeType),
description: `A list of nodes in the connection (without going through the description: `A list of nodes in the connection (without going through the
\`edges\` field).`, \`edges\` field).`
}, },
totalCount, totalCount
}), }),
edgeFields: () => ({ score }), edgeFields: () => ({ score })
}).connectionType; }).connectionType
} }
export const releaseGroupType = {
type: new GraphQLList(ReleaseGroupType),
description: 'Filter by one or more release group types.',
};
export const releaseStatus = {
type: new GraphQLList(ReleaseStatus),
description: 'Filter by one or more release statuses.',
};

View file

@ -1,27 +1,22 @@
export { DateType, DiscID, IPI, ISRC, ISWC, MBID, URLString } from './scalars'
export { ReleaseGroupType, ReleaseStatus } from './enums'
export { default as Node } from './node'
export { default as Entity, EntityConnection } from './entity'
export { default as Area, AreaConnection } from './area'
export { default as Artist, ArtistConnection } from './artist'
export { default as Collection, CollectionConnection } from './collection'
export { default as Disc } from './disc'
export { default as Event, EventConnection } from './event'
export { default as Instrument, InstrumentConnection } from './instrument'
export { default as Label, LabelConnection } from './label'
export { default as Place, PlaceConnection } from './place'
export { default as Recording, RecordingConnection } from './recording'
export { default as Release, ReleaseConnection } from './release'
export { export {
DateType, default as ReleaseGroup,
DiscID, ReleaseGroupConnection
IPI, } from './release-group'
ISRC, export { default as Series, SeriesConnection } from './series'
ISWC, export { default as Tag, TagConnection } from './tag'
MBID, export { default as URL, URLConnection } from './url'
URLString, export { default as Work, WorkConnection } from './work'
} from './scalars.js';
export { ReleaseGroupType, ReleaseStatus } from './enums.js';
export { Node } from './node.js';
export { Entity, EntityConnection } from './entity.js';
export { Area, AreaConnection } from './area.js';
export { Artist, ArtistConnection } from './artist.js';
export { Collection, CollectionConnection } from './collection.js';
export { Disc } from './disc.js';
export { Event, EventConnection } from './event.js';
export { Instrument, InstrumentConnection } from './instrument.js';
export { Label, LabelConnection } from './label.js';
export { Place, PlaceConnection } from './place.js';
export { Recording, RecordingConnection } from './recording.js';
export { Release, ReleaseConnection } from './release.js';
export { ReleaseGroup, ReleaseGroupConnection } from './release-group.js';
export { Series, SeriesConnection } from './series.js';
export { Tag, TagConnection } from './tag.js';
export { URL, URLConnection } from './url.js';
export { Work, WorkConnection } from './work.js';

View file

@ -1,23 +1,20 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLString } from 'graphql/type'
import { Node } from './node.js'; import Node from './node'
import { Entity } from './entity.js'; import Entity from './entity'
import { import {
fieldWithID, fieldWithID,
id, id,
mbid, mbid,
name, name,
disambiguation, disambiguation,
connectionWithExtras, aliases,
linkedQuery, relationships,
} from './helpers.js'; collections,
import { aliases } from './alias.js'; tags,
import { collections } from './collection.js'; connectionWithExtras
import { relationships } from './relationship.js'; } from './helpers'
import { tags } from './tag.js';
const { GraphQLObjectType, GraphQLString } = GraphQL; const Instrument = new GraphQLObjectType({
export const Instrument = new GraphQLObjectType({
name: 'Instrument', name: 'Instrument',
description: `[Instruments](https://musicbrainz.org/doc/Instrument) are description: `[Instruments](https://musicbrainz.org/doc/Instrument) are
devices created or adapted to make musical sounds. Instruments are primarily devices created or adapted to make musical sounds. Instruments are primarily
@ -32,19 +29,18 @@ used in relationships between two other entities.`,
description: { description: {
type: GraphQLString, type: GraphQLString,
description: `A brief description of the main characteristics of the description: `A brief description of the main characteristics of the
instrument.`, instrument.`
}, },
...fieldWithID('type', { ...fieldWithID('type', {
description: `The type categorises the instrument by the way the sound is description: `The type categorises the instrument by the way the sound is
created, similar to the [Hornbostel-Sachs](https://en.wikipedia.org/wiki/Hornbostel%E2%80%93Sachs) created, similar to the [Hornbostel-Sachs](https://en.wikipedia.org/wiki/Hornbostel%E2%80%93Sachs)
classification.`, classification.`
}), }),
relationships, relationships,
collections, collections,
tags, tags
}), })
}); })
export const InstrumentConnection = connectionWithExtras(Instrument); export const InstrumentConnection = connectionWithExtras(Instrument)
export default Instrument
export const instruments = linkedQuery(InstrumentConnection);

View file

@ -1,29 +1,31 @@
import GraphQL from 'graphql'; import {
import { Node } from './node.js'; GraphQLObjectType,
import { Entity } from './entity.js'; GraphQLList,
import { IPI } from './scalars.js'; GraphQLString,
import { Area } from './area.js'; GraphQLInt
} from 'graphql/type'
import Node from './node'
import Entity from './entity'
import { IPI } from './scalars'
import Area from './area'
import { import {
id, id,
mbid, mbid,
name, name,
sortName, sortName,
disambiguation, disambiguation,
aliases,
lifeSpan,
releases,
relationships,
collections,
tags,
rating,
fieldWithID, fieldWithID,
connectionWithExtras, connectionWithExtras
linkedQuery, } from './helpers'
} from './helpers.js';
import { aliases } from './alias.js';
import { collections } from './collection.js';
import { lifeSpan } from './life-span.js';
import { tags } from './tag.js';
import { rating } from './rating.js';
import { relationships } from './relationship.js';
import { releases } from './release.js';
const { GraphQLObjectType, GraphQLList, GraphQLString, GraphQLInt } = GraphQL; const Label = new GraphQLObjectType({
export const Label = new GraphQLObjectType({
name: 'Label', name: 'Label',
description: `[Labels](https://musicbrainz.org/doc/Label) represent mostly description: `[Labels](https://musicbrainz.org/doc/Label) represent mostly
(but not only) imprints. To a lesser extent, a label entity may be created to (but not only) imprints. To a lesser extent, a label entity may be created to
@ -38,35 +40,34 @@ represent a record company.`,
aliases, aliases,
country: { country: {
type: GraphQLString, type: GraphQLString,
description: 'The country of origin for the label.', description: 'The country of origin for the label.'
}, },
area: { area: {
type: Area, type: Area,
description: 'The area in which the label is based.', description: 'The area in which the label is based.'
}, },
lifeSpan, lifeSpan,
labelCode: { labelCode: {
type: GraphQLInt, type: GraphQLInt,
description: `The [“LC” code](https://musicbrainz.org/doc/Label/Label_Code) description: `The [“LC” code](https://musicbrainz.org/doc/Label/Label_Code)
of the label.`, of the label.`
}, },
ipis: { ipis: {
type: new GraphQLList(IPI), type: new GraphQLList(IPI),
description: `List of [Interested Parties Information](https://musicbrainz.org/doc/IPI) description: `List of [Interested Parties Information](https://musicbrainz.org/doc/IPI)
codes for the label.`, codes for the label.`
}, },
...fieldWithID('type', { ...fieldWithID('type', {
description: `A type describing the main activity of the label, e.g. description: `A type describing the main activity of the label, e.g.
imprint, production, distributor, rights society, etc.`, imprint, production, distributor, rights society, etc.`
}), }),
releases, releases,
relationships, relationships,
collections, collections,
rating, rating,
tags, tags
}), })
}); })
export const LabelConnection = connectionWithExtras(Label); export const LabelConnection = connectionWithExtras(Label)
export default Label
export const labels = linkedQuery(LabelConnection);

View file

@ -1,32 +1,22 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLBoolean } from 'graphql/type'
import { DateType } from './scalars.js'; import { DateType } from './scalars'
import { resolveHyphenated } from './helpers.js';
const { GraphQLObjectType, GraphQLBoolean } = GraphQL; export default new GraphQLObjectType({
export const LifeSpan = new GraphQLObjectType({
name: 'LifeSpan', name: 'LifeSpan',
description: `Fields indicating the begin and end date of an entitys description: `Fields indicating the begin and end date of an entitys
lifetime, including whether it has ended (even if the date is unknown).`, lifetime, including whether it has ended (even if the date is unknown).`,
fields: () => ({ fields: () => ({
begin: { begin: {
type: DateType, type: DateType,
description: 'The start date of the entitys life span.', description: 'The start date of the entitys life span.'
}, },
end: { end: {
type: DateType, type: DateType,
description: 'The end date of the entitys life span.', description: 'The end date of the entitys life span.'
}, },
ended: { ended: {
type: GraphQLBoolean, type: GraphQLBoolean,
description: 'Whether or not the entitys life span has ended.', description: 'Whether or not the entitys life span has ended.'
}, }
}), })
}); })
export const lifeSpan = {
type: LifeSpan,
description: `The begin and end dates of the entitys existence. Its exact
meaning depends on the type of entity.`,
resolve: resolveHyphenated,
};

View file

@ -1,12 +1,13 @@
import GraphQL from 'graphql'; import {
import { Disc } from './disc.js'; GraphQLObjectType,
import { Track } from './track.js'; GraphQLList,
import { resolveHyphenated, fieldWithID } from './helpers.js'; GraphQLString,
import { createSubqueryResolver } from '../resolvers.js'; GraphQLInt
} from 'graphql/type'
import Disc from './disc'
import { resolveHyphenated, fieldWithID } from './helpers'
const { GraphQLObjectType, GraphQLList, GraphQLString, GraphQLInt } = GraphQL; export default new GraphQLObjectType({
export const Media = new GraphQLObjectType({
name: 'Medium', name: 'Medium',
description: `A medium is the actual physical medium the audio content is description: `A medium is the actual physical medium the audio content is
stored upon. This means that each CD in a multi-disc release will be entered as stored upon. This means that each CD in a multi-disc release will be entered as
@ -16,34 +17,26 @@ cassette) and can optionally also have a title.`,
fields: () => ({ fields: () => ({
title: { title: {
type: GraphQLString, type: GraphQLString,
description: 'The title of this particular medium.', description: 'The title of this particular medium.'
}, },
...fieldWithID('format', { ...fieldWithID('format', {
description: `The [format](https://musicbrainz.org/doc/Release/Format) of description: `The [format](https://musicbrainz.org/doc/Release/Format) of
the medium (e.g. CD, DVD, vinyl, cassette).`, the medium (e.g. CD, DVD, vinyl, cassette).`
}), }),
position: { position: {
type: GraphQLInt, type: GraphQLInt,
description: `The order of this medium in the release (for example, in a description: `The order of this medium in the release (for example, in a
multi-disc release).`, multi-disc release).`
}, },
trackCount: { trackCount: {
type: GraphQLInt, type: GraphQLInt,
description: 'The number of audio tracks on this medium.', description: 'The number of audio tracks on this medium.',
resolve: resolveHyphenated, resolve: resolveHyphenated
}, },
discs: { discs: {
type: new GraphQLList(Disc), type: new GraphQLList(Disc),
description: description:
'A list of physical discs and their disc IDs for this medium.', 'A list of physical discs and their disc IDs for this medium.'
}, }
tracks: { })
type: new GraphQLList(Track), })
description: 'The list of tracks on the given media.',
resolve: createSubqueryResolver({
inc: 'recordings',
key: 'tracks',
}),
},
}),
});

View file

@ -1,17 +1,36 @@
import GraphQLRelay from 'graphql-relay'; import { nodeDefinitions, fromGlobalId } from 'graphql-relay'
import { toDashed } from '../util.js'; import { toDashed } from './helpers'
import { resolveType } from './helpers.js';
const { nodeDefinitions, fromGlobalId } = GraphQLRelay; const debug = require('debug')('graphbrainz:types/node')
const TYPE_MODULES = {
discid: 'disc'
}
const { nodeInterface, nodeField } = nodeDefinitions( const { nodeInterface, nodeField } = nodeDefinitions(
(globalID, { loaders }) => { (globalID, { loaders }) => {
const { type, id } = fromGlobalId(globalID); const { type, id } = fromGlobalId(globalID)
const entityType = toDashed(type); const entityType = toDashed(type)
return loaders.lookup.load([entityType, id]); return loaders.lookup.load([entityType, id])
}, },
resolveType (obj, context, info) => {
); const type = TYPE_MODULES[obj._type] || obj._type
if (type) {
let originalType
try {
originalType = require(`./${type}`).default
} catch (err) {
debug(`Failed to load type: ${type}`)
return
}
// Don't use `originalType`! The schema may have been extended in which
// case the types have all been replaced. Instead, find the current type
// of the same name.
const typeMap = info.schema.getTypeMap()
return typeMap[originalType.name]
}
}
)
export const Node = nodeInterface; export default nodeInterface
export { nodeInterface, nodeField }; export { nodeInterface, nodeField }

View file

@ -1,25 +1,22 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLString } from 'graphql/type'
import { Node } from './node.js'; import Node from './node'
import { Entity } from './entity.js'; import Entity from './entity'
import { Degrees } from './scalars.js'; import { Degrees } from './scalars'
import { Area } from './area.js'; import Area from './area'
import { import {
id, id,
mbid, mbid,
name, name,
disambiguation, disambiguation,
aliases,
lifeSpan,
events,
fieldWithID, fieldWithID,
connectionWithExtras, relationships,
linkedQuery, collections,
} from './helpers.js'; tags,
import { aliases } from './alias.js'; connectionWithExtras
import { collections } from './collection.js'; } from './helpers'
import { events } from './event.js';
import { lifeSpan } from './life-span.js';
import { relationships } from './relationship.js';
import { tags } from './tag.js';
const { GraphQLObjectType, GraphQLString } = GraphQL;
export const Coordinates = new GraphQLObjectType({ export const Coordinates = new GraphQLObjectType({
name: 'Coordinates', name: 'Coordinates',
@ -27,17 +24,16 @@ export const Coordinates = new GraphQLObjectType({
fields: () => ({ fields: () => ({
latitude: { latitude: {
type: Degrees, type: Degrees,
description: description: 'The northsouth position of a point on the Earths surface.'
'The northsouth position of a point on the Earths surface.',
}, },
longitude: { longitude: {
type: Degrees, type: Degrees,
description: 'The eastwest position of a point on the Earths surface.', description: 'The eastwest position of a point on the Earths surface.'
}, }
}), })
}); })
export const Place = new GraphQLObjectType({ const Place = new GraphQLObjectType({
name: 'Place', name: 'Place',
description: `A [place](https://musicbrainz.org/doc/Place) is a venue, studio, description: `A [place](https://musicbrainz.org/doc/Place) is a venue, studio,
or other place where music is performed, recorded, engineered, etc.`, or other place where music is performed, recorded, engineered, etc.`,
@ -51,29 +47,28 @@ or other place where music is performed, recorded, engineered, etc.`,
address: { address: {
type: GraphQLString, type: GraphQLString,
description: `The address describes the location of the place using the description: `The address describes the location of the place using the
standard addressing format for the country it is located in.`, standard addressing format for the country it is located in.`
}, },
area: { area: {
type: Area, type: Area,
description: `The area entity representing the area, such as the city, in description: `The area entity representing the area, such as the city, in
which the place is located.`, which the place is located.`
}, },
coordinates: { coordinates: {
type: Coordinates, type: Coordinates,
description: 'The geographic coordinates of the place.', description: 'The geographic coordinates of the place.'
}, },
lifeSpan, lifeSpan,
...fieldWithID('type', { ...fieldWithID('type', {
description: `The type categorises the place based on its primary description: `The type categorises the place based on its primary
function.`, function.`
}), }),
events, events,
relationships, relationships,
collections, collections,
tags, tags
}), })
}); })
export const PlaceConnection = connectionWithExtras(Place); export const PlaceConnection = connectionWithExtras(Place)
export default Place
export const places = linkedQuery(PlaceConnection);

View file

@ -1,9 +1,11 @@
import GraphQL from 'graphql'; import {
import { createSubqueryResolver } from '../resolvers.js'; GraphQLObjectType,
GraphQLNonNull,
GraphQLInt,
GraphQLFloat
} from 'graphql/type'
const { GraphQLObjectType, GraphQLNonNull, GraphQLInt, GraphQLFloat } = GraphQL; export default new GraphQLObjectType({
export const Rating = new GraphQLObjectType({
name: 'Rating', name: 'Rating',
description: `[Ratings](https://musicbrainz.org/doc/Rating_System) allow users description: `[Ratings](https://musicbrainz.org/doc/Rating_System) allow users
to rate MusicBrainz entities. User may assign a value between 1 and 5; these to rate MusicBrainz entities. User may assign a value between 1 and 5; these
@ -13,17 +15,11 @@ for the entity.`,
voteCount: { voteCount: {
type: new GraphQLNonNull(GraphQLInt), type: new GraphQLNonNull(GraphQLInt),
description: 'The number of votes that have contributed to the rating.', description: 'The number of votes that have contributed to the rating.',
resolve: (rating) => rating['votes-count'], resolve: rating => rating['votes-count']
}, },
value: { value: {
type: GraphQLFloat, type: GraphQLFloat,
description: 'The average rating value based on the aggregated votes.', description: 'The average rating value based on the aggregated votes.'
}, }
}), })
}); })
export const rating = {
type: Rating,
description: 'The rating users have given to this entity.',
resolve: createSubqueryResolver({ inc: 'ratings' }),
};

View file

@ -1,27 +1,25 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLList, GraphQLBoolean } from 'graphql/type'
import { Node } from './node.js'; import Node from './node'
import { Entity } from './entity.js'; import Entity from './entity'
import { Duration, ISRC } from './scalars.js'; import { Duration, ISRC } from './scalars'
import { import {
id, id,
mbid, mbid,
title, title,
disambiguation, disambiguation,
connectionWithExtras, aliases,
linkedQuery, artistCredit,
} from './helpers.js'; artistCredits,
import { aliases } from './alias.js'; artists,
import { artists } from './artist.js'; releases,
import { artistCredit, artistCredits } from './artist-credit.js'; relationships,
import { collections } from './collection.js'; collections,
import { tags } from './tag.js'; rating,
import { rating } from './rating.js'; tags,
import { relationships } from './relationship.js'; connectionWithExtras
import { releases } from './release.js'; } from './helpers'
const { GraphQLObjectType, GraphQLList, GraphQLBoolean } = GraphQL; const Recording = new GraphQLObjectType({
export const Recording = new GraphQLObjectType({
name: 'Recording', name: 'Recording',
description: `A [recording](https://musicbrainz.org/doc/Recording) is an description: `A [recording](https://musicbrainz.org/doc/Recording) is an
entity in MusicBrainz which can be linked to tracks on releases. Each track must entity in MusicBrainz which can be linked to tracks on releases. Each track must
@ -47,37 +45,25 @@ or mixing.`,
isrcs: { isrcs: {
type: new GraphQLList(ISRC), type: new GraphQLList(ISRC),
description: `A list of [International Standard Recording Codes](https://musicbrainz.org/doc/ISRC) description: `A list of [International Standard Recording Codes](https://musicbrainz.org/doc/ISRC)
(ISRCs) for this recording.`, (ISRCs) for this recording.`
resolve: (source, args, context) => {
if (source.isrcs) {
return source.isrcs;
}
// TODO: Add support for parent entities knowing to include this `inc`
// parameter in their own calls by inspecting what fields are requested
// or batching things at the loader level.
return context.loaders.lookup
.load(['recording', source.id, { inc: 'isrcs' }])
.then((recording) => recording.isrcs);
},
}, },
length: { length: {
type: Duration, type: Duration,
description: `An approximation to the length of the recording, calculated description: `An approximation to the length of the recording, calculated
from the lengths of the tracks using it.`, from the lengths of the tracks using it.`
}, },
video: { video: {
type: GraphQLBoolean, type: GraphQLBoolean,
description: 'Whether this is a video recording.', description: 'Whether this is a video recording.'
}, },
artists, artists,
releases, releases,
relationships, relationships,
collections, collections,
rating, rating,
tags, tags
}), })
}); })
export const RecordingConnection = connectionWithExtras(Recording); export const RecordingConnection = connectionWithExtras(Recording)
export default Recording
export const recordings = linkedQuery(RecordingConnection);

View file

@ -1,25 +1,15 @@
import GraphQL from 'graphql';
import GraphQLRelay from 'graphql-relay';
import { DateType } from './scalars.js';
import { Entity } from './entity.js';
import { import {
resolveHyphenated,
fieldWithID,
connectionWithExtras,
} from './helpers.js';
import { resolveRelationship, includeRelationships } from '../resolvers.js';
import { toDashed } from '../util.js';
const {
GraphQLObjectType, GraphQLObjectType,
GraphQLNonNull, GraphQLNonNull,
GraphQLString, GraphQLString,
GraphQLList, GraphQLList,
GraphQLBoolean, GraphQLBoolean
} = GraphQL; } from 'graphql/type'
const { connectionArgs } = GraphQLRelay; import { DateType } from './scalars'
import Entity from './entity'
import { resolveHyphenated, fieldWithID, connectionWithExtras } from './helpers'
export const Relationship = new GraphQLObjectType({ const Relationship = new GraphQLObjectType({
name: 'Relationship', name: 'Relationship',
description: `[Relationships](https://musicbrainz.org/doc/Relationships) are a description: `[Relationships](https://musicbrainz.org/doc/Relationships) are a
way to represent all the different ways in which entities are connected to each way to represent all the different ways in which entities are connected to each
@ -28,112 +18,63 @@ other and to URLs outside MusicBrainz.`,
target: { target: {
type: new GraphQLNonNull(Entity), type: new GraphQLNonNull(Entity),
description: 'The target entity.', description: 'The target entity.',
resolve: (source) => { resolve: source => {
const targetType = source['target-type']; const targetType = source['target-type']
const target = source[targetType]; const target = source[targetType]
target._type = targetType.replace('_', '-'); target._type = targetType.replace('_', '-')
return target; return target
}, }
}, },
direction: { direction: {
type: new GraphQLNonNull(GraphQLString), type: new GraphQLNonNull(GraphQLString),
description: 'The direction of the relationship.', description: 'The direction of the relationship.'
}, },
targetType: { targetType: {
type: new GraphQLNonNull(GraphQLString), type: new GraphQLNonNull(GraphQLString),
description: description:
'The type of entity on the receiving end of the relationship.', 'The type of entity on the receiving end of the relationship.',
resolve: resolveHyphenated, resolve: resolveHyphenated
}, },
sourceCredit: { sourceCredit: {
type: GraphQLString, type: GraphQLString,
description: `How the source entity was actually credited, if different description: `How the source entity was actually credited, if different
from its main (performance) name.`, from its main (performance) name.`,
resolve: resolveHyphenated, resolve: resolveHyphenated
}, },
targetCredit: { targetCredit: {
type: GraphQLString, type: GraphQLString,
description: `How the target entity was actually credited, if different description: `How the target entity was actually credited, if different
from its main (performance) name.`, from its main (performance) name.`,
resolve: resolveHyphenated, resolve: resolveHyphenated
}, },
begin: { begin: {
type: DateType, type: DateType,
description: 'The date on which the relationship became applicable.', description: 'The date on which the relationship became applicable.'
}, },
end: { end: {
type: DateType, type: DateType,
description: description:
'The date on which the relationship became no longer applicable.', 'The date on which the relationship became no longer applicable.'
}, },
ended: { ended: {
type: GraphQLBoolean, type: GraphQLBoolean,
description: 'Whether the relationship still applies.', description: 'Whether the relationship still applies.'
}, },
attributes: { attributes: {
type: new GraphQLList(GraphQLString), type: new GraphQLList(GraphQLString),
description: `Attributes which modify the relationship. There is a [list description: `Attributes which modify the relationship. There is a [list
of all attributes](https://musicbrainz.org/relationship-attributes), but the of all attributes](https://musicbrainz.org/relationship-attributes), but the
attributes which are available, and how they should be used, depends on the attributes which are available, and how they should be used, depends on the
relationship type.`, relationship type.`
}, },
// There doesn't seem to be any documentation for the `attribute-values` // There doesn't seem to be any documentation for the `attribute-values`
// field. // field.
// attributeValues: {}, // attributeValues: {},
...fieldWithID('type', { ...fieldWithID('type', {
description: 'The type of relationship.', description: 'The type of relationship.'
}), })
}), })
}); })
export const RelationshipConnection = connectionWithExtras(Relationship); export const RelationshipConnection = connectionWithExtras(Relationship)
export default Relationship
export const relationship = {
type: RelationshipConnection,
description: 'A list of relationships between these two entity types.',
args: {
direction: {
type: GraphQLString,
description: 'Filter by the relationship direction.',
},
...fieldWithID('type', {
description: 'Filter by the relationship type.',
}),
...connectionArgs,
},
resolve: resolveRelationship,
};
export const relationships = {
type: new GraphQLObjectType({
name: 'Relationships',
description: 'Lists of entity relationships for each entity type.',
fields: () => ({
areas: relationship,
artists: relationship,
events: relationship,
instruments: relationship,
labels: relationship,
places: relationship,
recordings: relationship,
releases: relationship,
releaseGroups: relationship,
series: relationship,
urls: relationship,
works: relationship,
}),
}),
description: 'Relationships between this entity and other entitites.',
resolve: (entity, args, { loaders }, info) => {
let promise;
if (entity.relations != null) {
promise = Promise.resolve(entity);
} else {
const entityType = toDashed(info.parentType.name);
const id = entity.id;
const params = includeRelationships({}, info);
promise = loaders.lookup.load([entityType, id, params]);
}
return promise.then((entity) => entity.relations);
},
};

View file

@ -1,15 +1,13 @@
import GraphQL from 'graphql'; import { GraphQLObjectType } from 'graphql/type'
import { DateType } from './scalars.js'; import { DateType } from './scalars'
import { Area } from './area.js'; import Area from './area'
const { GraphQLObjectType } = GraphQL; export default new GraphQLObjectType({
export const ReleaseEvent = new GraphQLObjectType({
name: 'ReleaseEvent', name: 'ReleaseEvent',
description: `The date on which a release was issued in a country/region with description: `The date on which a release was issued in a country/region with
a particular label, catalog number, barcode, and format.`, a particular label, catalog number, barcode, and format.`,
fields: () => ({ fields: () => ({
area: { type: Area }, area: { type: Area },
date: { type: DateType }, date: { type: DateType }
}), })
}); })

View file

@ -1,31 +1,28 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLList } from 'graphql/type'
import { Node } from './node.js'; import Node from './node'
import { Entity } from './entity.js'; import Entity from './entity'
import { DateType } from './scalars.js'; import { DateType } from './scalars'
import { ReleaseGroupType } from './enums.js'; import { ReleaseGroupType } from './enums'
import { import {
id, id,
mbid, mbid,
title, title,
disambiguation, disambiguation,
aliases,
artistCredit,
artistCredits,
artists,
releases,
relationships,
collections,
rating,
tags,
fieldWithID, fieldWithID,
releaseGroupType,
resolveHyphenated, resolveHyphenated,
connectionWithExtras, connectionWithExtras
linkedQuery, } from './helpers'
} from './helpers.js';
import { aliases } from './alias.js';
import { artistCredit, artistCredits } from './artist-credit.js';
import { artists } from './artist.js';
import { releases } from './release.js';
import { relationships } from './relationship.js';
import { collections } from './collection.js';
import { rating } from './rating.js';
import { tags } from './tag.js';
const { GraphQLObjectType, GraphQLList } = GraphQL; const ReleaseGroup = new GraphQLObjectType({
export const ReleaseGroup = new GraphQLObjectType({
name: 'ReleaseGroup', name: 'ReleaseGroup',
description: `A [release group](https://musicbrainz.org/doc/Release_Group) is description: `A [release group](https://musicbrainz.org/doc/Release_Group) is
used to group several different releases into a single logical entity. Every used to group several different releases into a single logical entity. Every
@ -47,33 +44,28 @@ album it doesnt matter how many CDs or editions/versions it had.`,
firstReleaseDate: { firstReleaseDate: {
type: DateType, type: DateType,
description: 'The date of the earliest release in the group.', description: 'The date of the earliest release in the group.',
resolve: resolveHyphenated, resolve: resolveHyphenated
}, },
...fieldWithID('primaryType', { ...fieldWithID('primaryType', {
type: ReleaseGroupType, type: ReleaseGroupType,
description: `The [type](https://musicbrainz.org/doc/Release_Group/Type) description: `The [type](https://musicbrainz.org/doc/Release_Group/Type)
of a release group describes what kind of releases the release group represents, of a release group describes what kind of releases the release group represents,
e.g. album, single, soundtrack, compilation, etc. A release group can have a e.g. album, single, soundtrack, compilation, etc. A release group can have a
main type and an unspecified number of additional types.`, main type and an unspecified number of additional types.`
}), }),
...fieldWithID('secondaryTypes', { ...fieldWithID('secondaryTypes', {
type: new GraphQLList(ReleaseGroupType), type: new GraphQLList(ReleaseGroupType),
description: `Additional [types](https://musicbrainz.org/doc/Release_Group/Type) description: `Additional [types](https://musicbrainz.org/doc/Release_Group/Type)
that apply to this release group.`, that apply to this release group.`
}), }),
artists, artists,
releases, releases,
relationships, relationships,
collections, collections,
rating, rating,
tags, tags
}), })
}); })
export const ReleaseGroupConnection = connectionWithExtras(ReleaseGroup); export const ReleaseGroupConnection = connectionWithExtras(ReleaseGroup)
export default ReleaseGroup
export const releaseGroups = linkedQuery(ReleaseGroupConnection, {
args: {
type: releaseGroupType,
},
});

View file

@ -1,35 +1,31 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLString, GraphQLList } from 'graphql/type'
import { Node } from './node.js'; import Node from './node'
import { Entity } from './entity.js'; import Entity from './entity'
import { ASIN, DateType } from './scalars.js'; import { ASIN, DateType } from './scalars'
import { Media } from './media.js'; import Media from './media'
import { ReleaseStatus } from './enums.js'; import { ReleaseStatus } from './enums'
import { ReleaseEvent } from './release-event.js'; import ReleaseEvent from './release-event'
import { import {
id, id,
mbid, mbid,
title, title,
disambiguation, disambiguation,
aliases,
artistCredit,
artistCredits,
artists,
labels,
recordings,
releaseGroups,
relationships,
collections,
tags,
fieldWithID, fieldWithID,
releaseGroupType,
releaseStatus,
resolveHyphenated, resolveHyphenated,
connectionWithExtras, connectionWithExtras
linkedQuery, } from './helpers'
} from './helpers.js';
import { aliases } from './alias.js';
import { artistCredit, artistCredits } from './artist-credit.js';
import { artists } from './artist.js';
import { collections } from './collection.js';
import { labels } from './label.js';
import { recordings } from './recording.js';
import { relationships } from './relationship.js';
import { releaseGroups } from './release-group.js';
import { tags } from './tag.js';
const { GraphQLObjectType, GraphQLString, GraphQLList } = GraphQL; const Release = new GraphQLObjectType({
export const Release = new GraphQLObjectType({
name: 'Release', name: 'Release',
description: `A [release](https://musicbrainz.org/doc/Release) represents the description: `A [release](https://musicbrainz.org/doc/Release) represents the
unique release (i.e. issuing) of a product on a specific date with specific unique release (i.e. issuing) of a product on a specific date with specific
@ -48,48 +44,48 @@ MusicBrainz as one release.`,
releaseEvents: { releaseEvents: {
type: new GraphQLList(ReleaseEvent), type: new GraphQLList(ReleaseEvent),
description: 'The release events for this release.', description: 'The release events for this release.',
resolve: resolveHyphenated, resolve: resolveHyphenated
}, },
date: { date: {
type: DateType, type: DateType,
description: `The [release date](https://musicbrainz.org/doc/Release/Date) description: `The [release date](https://musicbrainz.org/doc/Release/Date)
is the date in which a release was made available through some sort of is the date in which a release was made available through some sort of
distribution mechanism.`, distribution mechanism.`
}, },
country: { country: {
type: GraphQLString, type: GraphQLString,
description: 'The country in which the release was issued.', description: 'The country in which the release was issued.'
}, },
asin: { asin: {
type: ASIN, type: ASIN,
description: `The [Amazon Standard Identification Number](https://musicbrainz.org/doc/ASIN) description: `The [Amazon Standard Identification Number](https://musicbrainz.org/doc/ASIN)
of the release.`, of the release.`
}, },
barcode: { barcode: {
type: GraphQLString, type: GraphQLString,
description: `The [barcode](https://en.wikipedia.org/wiki/Barcode), if the description: `The [barcode](https://en.wikipedia.org/wiki/Barcode), if the
release has one. The most common types found on releases are 12-digit release has one. The most common types found on releases are 12-digit
[UPCs](https://en.wikipedia.org/wiki/Universal_Product_Code) and 13-digit [UPCs](https://en.wikipedia.org/wiki/Universal_Product_Code) and 13-digit
[EANs](https://en.wikipedia.org/wiki/International_Article_Number).`, [EANs](https://en.wikipedia.org/wiki/International_Article_Number).`
}, },
...fieldWithID('status', { ...fieldWithID('status', {
type: ReleaseStatus, type: ReleaseStatus,
description: 'The status describes how “official” a release is.', description: 'The status describes how “official” a release is.'
}), }),
...fieldWithID('packaging', { ...fieldWithID('packaging', {
description: `The physical packaging that accompanies the release. See description: `The physical packaging that accompanies the release. See
the [list of packaging](https://musicbrainz.org/doc/Release/Packaging) for more the [list of packaging](https://musicbrainz.org/doc/Release/Packaging) for more
information.`, information.`
}), }),
quality: { quality: {
type: GraphQLString, type: GraphQLString,
description: `Data quality indicates how good the data for a release is. description: `Data quality indicates how good the data for a release is.
It is not a mark of how good or bad the music itself is for that, use It is not a mark of how good or bad the music itself is for that, use
[ratings](https://musicbrainz.org/doc/Rating_System).`, [ratings](https://musicbrainz.org/doc/Rating_System).`
}, },
media: { media: {
type: new GraphQLList(Media), type: new GraphQLList(Media),
description: 'The media on which the release was distributed.', description: 'The media on which the release was distributed.'
}, },
artists, artists,
labels, labels,
@ -97,15 +93,9 @@ It is not a mark of how good or bad the music itself is for that, use
releaseGroups, releaseGroups,
relationships, relationships,
collections, collections,
tags, tags
}), })
}); })
export const ReleaseConnection = connectionWithExtras(Release); export const ReleaseConnection = connectionWithExtras(Release)
export default Release
export const releases = linkedQuery(ReleaseConnection, {
args: {
type: releaseGroupType,
status: releaseStatus,
},
});

View file

@ -1,71 +1,70 @@
import GraphQL from 'graphql'; import { Kind } from 'graphql/language'
import { GraphQLScalarType } from 'graphql/type'
const { Kind, GraphQLScalarType } = GraphQL;
function createScalar(config) { function createScalar(config) {
return new GraphQLScalarType({ return new GraphQLScalarType({
serialize: (value) => value, serialize: value => value,
parseValue: (value) => value, parseValue: value => value,
parseLiteral(ast) { parseLiteral(ast) {
if (ast.kind === Kind.STRING) { if (ast.kind === Kind.STRING) {
return ast.value; return ast.value
} }
return undefined; return null
}, },
...config, ...config
}); })
} }
const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/; const uuid = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/
const locale = /^([a-z]{2})(_[A-Z]{2})?(\.[a-zA-Z0-9-]+)?$/; const locale = /^([a-z]{2})(_[A-Z]{2})?(\.[a-zA-Z0-9-]+)?$/
// Be extremely lenient; just prevent major input errors. // Be extremely lenient; just prevent major input errors.
const url = /^\w+:\/\/[\w-]+\.\w+/; const url = /^\w+:\/\/[\w-]+\.\w+/
function validateMBID(value) { function validateMBID(value) {
if (typeof value === 'string' && uuid.test(value)) { if (typeof value === 'string' && uuid.test(value)) {
return value; return value
} }
throw new TypeError(`Malformed MBID: ${value}`); throw new TypeError(`Malformed MBID: ${value}`)
} }
function validatePositive(value) { function validatePositive(value) {
if (value >= 0) { if (value >= 0) {
return value; return value
} }
throw new TypeError(`Expected positive value: ${value}`); throw new TypeError(`Expected positive value: ${value}`)
} }
function validateLocale(value) { function validateLocale(value) {
if (typeof value === 'string' && locale.test(value)) { if (typeof value === 'string' && locale.test(value)) {
return value; return value
} }
throw new TypeError(`Malformed locale: ${value}`); throw new TypeError(`Malformed locale: ${value}`)
} }
function validateURL(value) { function validateURL(value) {
if (typeof value === 'string' && url.test(value)) { if (typeof value === 'string' && url.test(value)) {
return value; return value
} }
throw new TypeError(`Malformed URL: ${value}`); throw new TypeError(`Malformed URL: ${value}`)
} }
export const ASIN = createScalar({ export const ASIN = createScalar({
name: 'ASIN', name: 'ASIN',
description: `An [Amazon Standard Identification Number](https://musicbrainz.org/doc/ASIN) description: `An [Amazon Standard Identification Number](https://musicbrainz.org/doc/ASIN)
(ASIN) is a 10-character alphanumeric unique identifier assigned by Amazon.com (ASIN) is a 10-character alphanumeric unique identifier assigned by Amazon.com
and its partners for product identification within the Amazon organization.`, and its partners for product identification within the Amazon organization.`
}); })
export const DateType = createScalar({ export const DateType = createScalar({
name: 'Date', name: 'Date',
description: description:
'Year, month (optional), and day (optional) in YYYY-MM-DD format.', 'Year, month (optional), and day (optional) in YYYY-MM-DD format.'
}); })
export const Degrees = createScalar({ export const Degrees = createScalar({
name: 'Degrees', name: 'Degrees',
description: 'Decimal degrees, used for latitude and longitude.', description: 'Decimal degrees, used for latitude and longitude.'
}); })
export const DiscID = createScalar({ export const DiscID = createScalar({
name: 'DiscID', name: 'DiscID',
@ -81,8 +80,8 @@ Different pressing of a CD often have slightly different frame offsets, and
hence different disc IDs. hence different disc IDs.
Conversely, two different CDs may happen to have exactly the same set of frame Conversely, two different CDs may happen to have exactly the same set of frame
offsets and hence the same disc ID.`, offsets and hence the same disc ID.`
}); })
export const Duration = createScalar({ export const Duration = createScalar({
name: 'Duration', name: 'Duration',
@ -91,25 +90,25 @@ export const Duration = createScalar({
parseValue: validatePositive, parseValue: validatePositive,
parseLiteral(ast) { parseLiteral(ast) {
if (ast.kind === Kind.INT) { if (ast.kind === Kind.INT) {
return validatePositive(parseInt(ast.value, 10)); return validatePositive(parseInt(ast.value, 10))
} }
return undefined; return null
}, }
}); })
export const IPI = createScalar({ export const IPI = createScalar({
name: 'IPI', name: 'IPI',
description: `An [Interested Parties Information](https://musicbrainz.org/doc/IPI) description: `An [Interested Parties Information](https://musicbrainz.org/doc/IPI)
(IPI) code is an identifying number assigned by the CISAC database for musical (IPI) code is an identifying number assigned by the CISAC database for musical
rights management.`, rights management.`
}); })
export const ISNI = createScalar({ export const ISNI = createScalar({
name: 'ISNI', name: 'ISNI',
description: `The [International Standard Name Identifier](https://musicbrainz.org/doc/ISNI) description: `The [International Standard Name Identifier](https://musicbrainz.org/doc/ISNI)
(ISNI) is an ISO standard for uniquely identifying the public identities of (ISNI) is an ISO standard for uniquely identifying the public identities of
contributors to media content.`, contributors to media content.`
}); })
export const ISRC = createScalar({ export const ISRC = createScalar({
name: 'ISRC', name: 'ISRC',
@ -122,15 +121,15 @@ not the song itself. Therefore, different recordings, edits, remixes and
remasters of the same song will each be assigned their own ISRC. However, note remasters of the same song will each be assigned their own ISRC. However, note
that same recording should carry the same ISRC in all countries/territories. that same recording should carry the same ISRC in all countries/territories.
Songs are identified by analogous [International Standard Musical Work Codes](https://musicbrainz.org/doc/ISWC) Songs are identified by analogous [International Standard Musical Work Codes](https://musicbrainz.org/doc/ISWC)
(ISWCs).`, (ISWCs).`
}); })
export const ISWC = createScalar({ export const ISWC = createScalar({
name: 'ISWC', name: 'ISWC',
description: `The [International Standard Musical Work Code](https://musicbrainz.org/doc/ISWC) description: `The [International Standard Musical Work Code](https://musicbrainz.org/doc/ISWC)
(ISWC) is an ISO standard similar to ISBNs for identifying musical works / (ISWC) is an ISO standard similar to ISBNs for identifying musical works /
compositions.`, compositions.`
}); })
export const Locale = createScalar({ export const Locale = createScalar({
name: 'Locale', name: 'Locale',
@ -139,11 +138,11 @@ export const Locale = createScalar({
parseValue: validateLocale, parseValue: validateLocale,
parseLiteral(ast) { parseLiteral(ast) {
if (ast.kind === Kind.STRING) { if (ast.kind === Kind.STRING) {
return validateLocale(ast.value); return validateLocale(ast.value)
} }
return undefined; return null
}, }
}); })
export const MBID = createScalar({ export const MBID = createScalar({
name: 'MBID', name: 'MBID',
@ -153,16 +152,16 @@ export const MBID = createScalar({
parseValue: validateMBID, parseValue: validateMBID,
parseLiteral(ast) { parseLiteral(ast) {
if (ast.kind === Kind.STRING) { if (ast.kind === Kind.STRING) {
return validateMBID(ast.value); return validateMBID(ast.value)
} }
return undefined; return null
}, }
}); })
export const Time = createScalar({ export const Time = createScalar({
name: 'Time', name: 'Time',
description: 'A time of day, in 24-hour hh:mm notation.', description: 'A time of day, in 24-hour hh:mm notation.'
}); })
export const URLString = createScalar({ export const URLString = createScalar({
name: 'URLString', name: 'URLString',
@ -171,8 +170,8 @@ export const URLString = createScalar({
parseValue: validateURL, parseValue: validateURL,
parseLiteral(ast) { parseLiteral(ast) {
if (ast.kind === Kind.STRING) { if (ast.kind === Kind.STRING) {
return validateURL(ast.value); return validateURL(ast.value)
} }
return undefined; return null
}, }
}); })

View file

@ -1,22 +1,19 @@
import GraphQL from 'graphql'; import { GraphQLObjectType } from 'graphql/type'
import { Node } from './node.js'; import Node from './node'
import { Entity } from './entity.js'; import Entity from './entity'
import { import {
id, id,
mbid, mbid,
name, name,
disambiguation, disambiguation,
relationships,
collections,
tags,
fieldWithID, fieldWithID,
connectionWithExtras, connectionWithExtras
linkedQuery, } from './helpers'
} from './helpers.js';
import { collections } from './collection.js';
import { relationships } from './relationship.js';
import { tags } from './tag.js';
const { GraphQLObjectType } = GraphQL; const Series = new GraphQLObjectType({
export const Series = new GraphQLObjectType({
name: 'Series', name: 'Series',
description: `A [series](https://musicbrainz.org/doc/Series) is a sequence of description: `A [series](https://musicbrainz.org/doc/Series) is a sequence of
separate release groups, releases, recordings, works or events with a common separate release groups, releases, recordings, works or events with a common
@ -29,14 +26,13 @@ theme.`,
disambiguation, disambiguation,
...fieldWithID('type', { ...fieldWithID('type', {
description: `The type primarily describes what type of entity the series description: `The type primarily describes what type of entity the series
contains.`, contains.`
}), }),
relationships, relationships,
collections, collections,
tags, tags
}), })
}); })
export const SeriesConnection = connectionWithExtras(Series); export const SeriesConnection = connectionWithExtras(Series)
export default Series
export const series = linkedQuery(SeriesConnection);

View file

@ -1,17 +1,12 @@
import GraphQL from 'graphql'; import {
import GraphQLRelay from 'graphql-relay';
import { connectionWithExtras, linkedQuery } from './helpers.js';
import { createSubqueryResolver } from '../resolvers.js';
const {
GraphQLObjectType, GraphQLObjectType,
GraphQLNonNull, GraphQLNonNull,
GraphQLString, GraphQLString,
GraphQLInt, GraphQLInt
} = GraphQL; } from 'graphql/type'
const { connectionFromArray } = GraphQLRelay; import { connectionWithExtras } from './helpers'
export const Tag = new GraphQLObjectType({ const Tag = new GraphQLObjectType({
name: 'Tag', name: 'Tag',
description: `[Tags](https://musicbrainz.org/tags) are a way to mark entities description: `[Tags](https://musicbrainz.org/tags) are a way to mark entities
with extra information for example, the genres that apply to an artist, with extra information for example, the genres that apply to an artist,
@ -19,24 +14,14 @@ release, or recording.`,
fields: () => ({ fields: () => ({
name: { name: {
type: new GraphQLNonNull(GraphQLString), type: new GraphQLNonNull(GraphQLString),
description: 'The tag label.', description: 'The tag label.'
}, },
count: { count: {
type: GraphQLInt, type: GraphQLInt,
description: 'How many times this tag has been applied to the entity.', description: 'How many times this tag has been applied to the entity.'
}, }
}), })
}); })
export const TagConnection = connectionWithExtras(Tag); export const TagConnection = connectionWithExtras(Tag)
export default Tag
export const tags = linkedQuery(TagConnection, {
resolve: createSubqueryResolver({}, (value = [], args) => {
const connection = connectionFromArray(value, args);
return {
nodes: connection.edges.map((edge) => edge.node),
totalCount: value.length,
...connection,
};
}),
});

View file

@ -1,44 +0,0 @@
import GraphQL from 'graphql';
import { Entity } from './entity.js';
import { Duration } from './scalars.js';
import { Recording } from './recording.js';
import { mbid, title } from './helpers.js';
const { GraphQLObjectType, GraphQLInt, GraphQLString } = GraphQL;
export const Track = new GraphQLObjectType({
name: 'Track',
description: `A track is the way a recording is represented on a particular
release (or, more exactly, on a particular medium). Every track has a title
(see the guidelines for titles) and is credited to one or more artists.`,
interfaces: () => [Entity],
fields: () => ({
mbid,
title,
position: {
type: GraphQLInt,
description: `The tracks position on the overall release (including all
tracks from all discs).`,
},
number: {
type: GraphQLString,
description: `The track number, which may include information about the
disc or side it appears on, e.g. A1 or B3.`,
},
length: {
type: Duration,
description: 'The length of the track.',
},
recording: {
type: Recording,
description: 'The recording that appears on the track.',
resolve: (source) => {
const { recording } = source;
if (recording) {
recording._type = 'recording';
}
return recording;
},
},
}),
});

View file

@ -1,13 +1,10 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLNonNull } from 'graphql/type'
import { Node } from './node.js'; import Node from './node'
import { Entity } from './entity.js'; import Entity from './entity'
import { URLString } from './scalars.js'; import { URLString } from './scalars'
import { id, mbid, connectionWithExtras } from './helpers.js'; import { id, mbid, relationships, connectionWithExtras } from './helpers'
import { relationships } from './relationship.js';
const { GraphQLObjectType, GraphQLNonNull } = GraphQL; const URL = new GraphQLObjectType({
export const URL = new GraphQLObjectType({
name: 'URL', name: 'URL',
description: `A [URL](https://musicbrainz.org/doc/URL) pointing to a resource description: `A [URL](https://musicbrainz.org/doc/URL) pointing to a resource
external to MusicBrainz, i.e. an official homepage, a site where music can be external to MusicBrainz, i.e. an official homepage, a site where music can be
@ -18,10 +15,11 @@ acquired, an entry in another database, etc.`,
mbid, mbid,
resource: { resource: {
type: new GraphQLNonNull(URLString), type: new GraphQLNonNull(URLString),
description: 'The actual URL string.', description: 'The actual URL string.'
}, },
relationships, relationships
}), })
}); })
export const URLConnection = connectionWithExtras(URL); export const URLConnection = connectionWithExtras(URL)
export default URL

View file

@ -1,25 +1,22 @@
import GraphQL from 'graphql'; import { GraphQLObjectType, GraphQLString, GraphQLList } from 'graphql/type'
import { Node } from './node.js'; import Node from './node'
import { Entity } from './entity.js'; import Entity from './entity'
import { import {
id, id,
mbid, mbid,
title, title,
disambiguation, disambiguation,
aliases,
artists,
relationships,
collections,
rating,
tags,
fieldWithID, fieldWithID,
connectionWithExtras, connectionWithExtras
linkedQuery, } from './helpers'
} from './helpers.js';
import { aliases } from './alias.js';
import { artists } from './artist.js';
import { collections } from './collection.js';
import { rating } from './rating.js';
import { relationships } from './relationship.js';
import { tags } from './tag.js';
const { GraphQLObjectType, GraphQLString, GraphQLList } = GraphQL; const Work = new GraphQLObjectType({
export const Work = new GraphQLObjectType({
name: 'Work', name: 'Work',
description: `A [work](https://musicbrainz.org/doc/Work) is a distinct description: `A [work](https://musicbrainz.org/doc/Work) is a distinct
intellectual or artistic creation, which can be expressed in the form of one or intellectual or artistic creation, which can be expressed in the form of one or
@ -34,23 +31,22 @@ more audio recordings.`,
iswcs: { iswcs: {
type: new GraphQLList(GraphQLString), type: new GraphQLList(GraphQLString),
description: `A list of [ISWCs](https://musicbrainz.org/doc/ISWC) assigned description: `A list of [ISWCs](https://musicbrainz.org/doc/ISWC) assigned
to the work by copyright collecting agencies.`, to the work by copyright collecting agencies.`
}, },
language: { language: {
type: GraphQLString, type: GraphQLString,
description: 'The language in which the work was originally written.', description: 'The language in which the work was originally written.'
}, },
...fieldWithID('type', { ...fieldWithID('type', {
description: 'The type of work.', description: 'The type of work.'
}), }),
artists, artists,
relationships, relationships,
collections, collections,
rating, rating,
tags, tags
}), })
}); })
export const WorkConnection = connectionWithExtras(Work); export const WorkConnection = connectionWithExtras(Work)
export default Work
export const works = linkedQuery(WorkConnection);

View file

@ -1,99 +1,50 @@
import util from 'util'; import util from 'util'
import dashify from 'dashify';
import pascalCase from 'pascalcase';
export const ONE_DAY = 24 * 60 * 60 * 1000; export const ONE_DAY = 24 * 60 * 60 * 1000
export function getFields( export function getFields(info, fragments = info.fragments) {
info,
fragments = info.fragments,
depth = 0,
prefix = ''
) {
if (info.kind !== 'Field') { if (info.kind !== 'Field') {
info = info.fieldNodes[0]; info = info.fieldNodes[0]
} }
const selections = info.selectionSet.selections; const selections = info.selectionSet.selections
const reducer = (fields, selection) => { const reducer = (fields, selection) => {
if (selection.kind === 'FragmentSpread') { if (selection.kind === 'FragmentSpread') {
const name = selection.name.value; const name = selection.name.value
const fragment = fragments[name]; const fragment = fragments[name]
if (!fragment) { if (!fragment) {
throw new Error(`Fragment '${name}' was not passed to getFields()`); throw new Error(`Fragment '${name}' was not passed to getFields()`)
} }
fragment.selectionSet.selections.reduce(reducer, fields); fragment.selectionSet.selections.reduce(reducer, fields)
} else if (selection.kind === 'InlineFragment') { } else if (selection.kind === 'InlineFragment') {
selection.selectionSet.selections.reduce(reducer, fields); selection.selectionSet.selections.reduce(reducer, fields)
} else { } else {
const prefixedName = prefix + selection.name.value; fields[selection.name.value] = selection
fields[prefixedName] = selection;
if (depth > 0 && selection.selectionSet) {
const subFields = getFields(
selection,
fragments,
depth - 1,
`${prefixedName}.`
);
Object.assign(fields, subFields);
}
} }
return fields; return fields
}; }
return selections.reduce(reducer, {}); return selections.reduce(reducer, {})
} }
export function prettyPrint( export function prettyPrint(
obj, obj,
{ depth = 5, colors = true, breakLength = 120 } = {} { depth = 5, colors = true, breakLength = 120 } = {}
) { ) {
console.log(util.inspect(obj, { depth, colors, breakLength })); console.log(util.inspect(obj, { depth, colors, breakLength }))
} }
export function toFilteredArray(obj) { export function toFilteredArray(obj) {
return (Array.isArray(obj) ? obj : [obj]).filter((x) => x); return (Array.isArray(obj) ? obj : [obj]).filter(x => x)
} }
export function extendIncludes(includes, moreIncludes) { export function extendIncludes(includes, moreIncludes) {
includes = toFilteredArray(includes); includes = toFilteredArray(includes)
moreIncludes = toFilteredArray(moreIncludes); moreIncludes = toFilteredArray(moreIncludes)
const seen = {}; const seen = {}
return includes.concat(moreIncludes).filter((x) => { return includes.concat(moreIncludes).filter(x => {
if (seen[x]) { if (seen[x]) {
return false; return false
} }
seen[x] = true; seen[x] = true
return true; return true
}); })
}
export const toPascal = pascalCase;
export const toDashed = dashify;
export function toPlural(name) {
return name.endsWith('s') ? name : name + 's';
}
export function toSingular(name) {
return name.endsWith('s') && !/series/i.test(name) ? name.slice(0, -1) : name;
}
export function toWords(name) {
return toPascal(name).replace(/([^A-Z])?([A-Z]+)/g, (match, tail, head) => {
tail = tail ? tail + ' ' : '';
head = head.length > 1 ? head : head.toLowerCase();
return `${tail}${head}`;
});
}
export function filterObjectValues(obj, filter) {
return Object.entries(obj).reduce((obj, [key, value]) => {
if (filter(value)) {
obj[key] = value;
}
return obj;
}, {});
}
export function getTypeName(value) {
return Object.prototype.toString.call(value).slice(8, -1);
} }

View file

@ -1,25 +1,32 @@
import test from 'ava'; import test from 'ava'
import GraphQL from 'graphql'; import { graphql } from 'graphql'
import context from './helpers/context.js'; import schemas from './helpers/schema'
import context from './helpers/context'
const { graphql } = GraphQL; const TEST_SCHEMA = process.env.TEST_SCHEMA || 'baseSchema'
const schema = schemas[TEST_SCHEMA]
function testData(t, query, handler) { function testData(t, query, handler) {
return graphql(t.context.schema, query, null, context).then((result) => { return graphql(schema, query, null, context).then(result => {
if (result.errors !== undefined) { if (result.errors !== undefined) {
result.errors.forEach((error) => t.log(error)); console.log(result.errors)
} }
t.is(result.errors, undefined); t.is(result.errors, undefined)
return handler(t, result.data); return handler(t, result.data)
}); })
} }
function testError(t, query, handler) { function testError(t, query, handler) {
return graphql(t.context.schema, query, null, context).then((result) => { return graphql(schema, query, null, context).then(result => {
t.truthy(result.errors); t.truthy(result.errors)
t.true(result.errors.length > 0); t.true(result.errors.length > 0)
return handler(t, result.errors); return handler(t, result.errors)
}); })
}
function testThrows(t, query, handler) {
const error = t.throws(graphql(schema, query, null, context))
return handler(t, error)
} }
test( test(
@ -39,11 +46,11 @@ test(
t.deepEqual(data, { t.deepEqual(data, {
node: { node: {
__typename: 'ReleaseGroup', __typename: 'ReleaseGroup',
mbid: 'e37d2740-4503-4e3f-ab6d-e622a25e964d', mbid: 'e37d2740-4503-4e3f-ab6d-e622a25e964d'
}, }
}); })
} }
); )
test( test(
'schema has a lookup query', 'schema has a lookup query',
@ -65,12 +72,12 @@ test(
artist: { artist: {
mbid: 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8', mbid: 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8',
name: 'Lures', name: 'Lures',
type: 'Group', type: 'Group'
}, }
}, }
}); })
} }
); )
test( test(
'schema has a search query', 'schema has a search query',
@ -92,12 +99,12 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { recordings } = data.search; const { recordings } = data.search
t.true(recordings.totalCount > 0); t.true(recordings.totalCount > 0)
t.true(recordings.edges.length === 25); t.true(recordings.edges.length === 25)
recordings.edges.forEach((edge) => t.true(edge.score > 0)); recordings.edges.forEach(edge => t.true(edge.score > 0))
} }
); )
test( test(
'schema has a browse query', 'schema has a browse query',
@ -125,12 +132,12 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { releaseGroups } = data.browse; const { releaseGroups } = data.browse
t.true(releaseGroups.totalCount > 0); t.true(releaseGroups.totalCount > 0)
t.true(releaseGroups.edges.length > 0); t.true(releaseGroups.edges.length > 0)
releaseGroups.edges.forEach((edge) => t.truthy(edge.node.title)); releaseGroups.edges.forEach(edge => t.truthy(edge.node.title))
} }
); )
test( test(
'supports deeply nested queries', 'supports deeply nested queries',
@ -205,11 +212,11 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { labels } = data.search; const { labels } = data.search
t.true(labels.edges.length > 0); t.true(labels.edges.length > 0)
t.is(labels.edges[0].node.releases.edges.length, 1); t.is(labels.edges[0].node.releases.edges.length, 1)
} }
); )
test( test(
'connections have a nodes shortcut field', 'connections have a nodes shortcut field',
@ -274,15 +281,16 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { labels } = data.search; const { labels } = data.search
t.true(labels.nodes.length > 0); t.true(labels.nodes.length > 0)
t.is(labels.nodes[0].releases.nodes.length, 1); t.is(labels.nodes[0].releases.nodes.length, 1)
} }
); )
// FIXME: https://github.com/graphql/graphql-js/issues/910
test( test(
'throws an error if given a malformed MBID', 'throws an error if given a malformed MBID',
testError, testThrows,
` `
{ {
lookup { lookup {
@ -292,10 +300,12 @@ test(
} }
} }
`, `,
(t, errors) => { async (t, promise) => {
t.regex(errors[0].message, /Malformed MBID: ABC123/); const err = await promise
t.true(err instanceof TypeError)
t.is(err.message, 'Malformed MBID: ABC123')
} }
); )
test( test(
'artist areas access begin_area/end_area for lookup queries', 'artist areas access begin_area/end_area for lookup queries',
@ -315,11 +325,11 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { artist } = data.lookup; const { artist } = data.lookup
t.is(artist.beginArea.name, 'Westmount'); t.is(artist.beginArea.name, 'Westmount')
t.is(artist.endArea.name, 'Los Angeles'); t.is(artist.endArea.name, 'Los Angeles')
} }
); )
test( test(
'artist areas access begin_area/end_area for browse queries', 'artist areas access begin_area/end_area for browse queries',
@ -343,12 +353,12 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const artists = data.browse.artists.edges.map((edge) => edge.node); const artists = data.browse.artists.edges.map(edge => edge.node)
t.true(artists.length > 1); t.true(artists.length > 1)
t.true(artists.some((artist) => artist.beginArea)); t.true(artists.some(artist => artist.beginArea))
t.true(artists.some((artist) => artist.endArea)); t.true(artists.some(artist => artist.endArea))
} }
); )
test( test(
'artist areas access begin-area/end-area for search queries', 'artist areas access begin-area/end-area for search queries',
@ -372,12 +382,12 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const artists = data.search.artists.edges.map((edge) => edge.node); const artists = data.search.artists.edges.map(edge => edge.node)
t.true(artists.length === 1); t.true(artists.length === 1)
t.is(artists[0].beginArea.name, 'Westmount'); t.is(artists[0].beginArea.name, 'Westmount')
t.is(artists[0].endArea.name, 'Los Angeles'); t.is(artists[0].endArea.name, 'Los Angeles')
} }
); )
test( test(
'relationships are grouped by target type', 'relationships are grouped by target type',
@ -423,24 +433,24 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { relationships } = data.lookup.artist; const { relationships } = data.lookup.artist
t.is(relationships.artists.edges.length, 5); t.is(relationships.artists.edges.length, 5)
relationships.artists.edges.forEach((edge) => { relationships.artists.edges.forEach(edge => {
t.is(edge.node.targetType, 'artist'); t.is(edge.node.targetType, 'artist')
t.is(edge.node.target.__typename, 'Artist'); t.is(edge.node.target.__typename, 'Artist')
}); })
t.is(relationships.recordings.edges.length, 5); t.is(relationships.recordings.edges.length, 5)
relationships.recordings.edges.forEach((edge) => { relationships.recordings.edges.forEach(edge => {
t.is(edge.node.targetType, 'recording'); t.is(edge.node.targetType, 'recording')
t.is(edge.node.target.__typename, 'Recording'); t.is(edge.node.target.__typename, 'Recording')
}); })
t.is(relationships.releases.edges.length, 5); t.is(relationships.releases.edges.length, 5)
relationships.releases.edges.forEach((edge) => { relationships.releases.edges.forEach(edge => {
t.is(edge.node.targetType, 'release'); t.is(edge.node.targetType, 'release')
t.is(edge.node.target.__typename, 'Release'); t.is(edge.node.target.__typename, 'Release')
}); })
} }
); )
test( test(
'relationships can be filtered by type', 'relationships can be filtered by type',
@ -464,15 +474,15 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { artist } = data.lookup; const { artist } = data.lookup
const rels = artist.relationships.artists.edges.map((edge) => edge.node); const rels = artist.relationships.artists.edges.map(edge => edge.node)
t.is(rels.length, 2); t.is(rels.length, 2)
rels.forEach((rel) => { rels.forEach(rel => {
t.is(rel.targetType, 'artist'); t.is(rel.targetType, 'artist')
t.is(rel.type, 'parent'); t.is(rel.type, 'parent')
}); })
} }
); )
test( test(
'relationships can be filtered by type ID', 'relationships can be filtered by type ID',
@ -496,15 +506,15 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { artist } = data.lookup; const { artist } = data.lookup
const rels = artist.relationships.artists.edges.map((edge) => edge.node); const rels = artist.relationships.artists.edges.map(edge => edge.node)
t.is(rels.length, 1); t.is(rels.length, 1)
rels.forEach((rel) => { rels.forEach(rel => {
t.is(rel.targetType, 'artist'); t.is(rel.targetType, 'artist')
t.is(rel.type, 'involved with'); t.is(rel.type, 'involved with')
}); })
} }
); )
test( test(
'relationships can be filtered by direction', 'relationships can be filtered by direction',
@ -536,21 +546,21 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { area } = data.lookup; const { area } = data.lookup
const isPartOf = area.relationships.isPartOf.edges.map((edge) => edge.node); const isPartOf = area.relationships.isPartOf.edges.map(edge => edge.node)
const hasParts = area.relationships.hasParts.edges.map((edge) => edge.node); const hasParts = area.relationships.hasParts.edges.map(edge => edge.node)
t.true(isPartOf.length > 0); t.true(isPartOf.length > 0)
t.true(hasParts.length > 0); t.true(hasParts.length > 0)
isPartOf.forEach((rel) => { isPartOf.forEach(rel => {
t.is(rel.type, 'part of'); t.is(rel.type, 'part of')
t.is(rel.direction, 'backward'); t.is(rel.direction, 'backward')
}); })
hasParts.forEach((rel) => { hasParts.forEach(rel => {
t.is(rel.type, 'part of'); t.is(rel.type, 'part of')
t.is(rel.direction, 'forward'); t.is(rel.direction, 'forward')
}); })
} }
); )
test( test(
'area maps iso-3166-1-codes to isoCodes', 'area maps iso-3166-1-codes to isoCodes',
@ -566,45 +576,9 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
t.deepEqual(data.lookup.area.isoCodes, ['US']); t.deepEqual(data.lookup.area.isoCodes, ['US'])
} }
); )
test(
'area isoCodes accepts an argument to retrieve 3166-1, 3166-2, or 3166-3 codes',
testData,
`
{
lookup {
eastGermany: area(mbid: "d907b0ac-2956-386f-a246-62d55779aae1") {
name
isoDefault: isoCodes
iso3166_1: isoCodes(standard: "3166-1")
iso3166_2: isoCodes(standard: "3166-2")
iso3166_3: isoCodes(standard: "3166-3")
}
newYork: area(mbid: "75e398a3-5f3f-4224-9cd8-0fe44715bc95") {
name
isoDefault: isoCodes
iso3166_1: isoCodes(standard: "3166-1")
iso3166_2: isoCodes(standard: "3166-2")
iso3166_3: isoCodes(standard: "3166-3")
}
}
}
`,
(t, data) => {
t.deepEqual(data.lookup.eastGermany.isoDefault, ['XG']);
t.deepEqual(data.lookup.eastGermany.iso3166_1, ['XG']);
t.is(data.lookup.eastGermany.iso3166_2, null);
t.deepEqual(data.lookup.eastGermany.iso3166_3, ['DDDE']);
t.is(data.lookup.newYork.isoDefault, null);
t.is(data.lookup.newYork.iso3166_1, null);
t.deepEqual(data.lookup.newYork.iso3166_2, ['US-NY']);
t.is(data.lookup.newYork.iso3166_3, null);
}
);
test( test(
'areas have a type and typeID', 'areas have a type and typeID',
@ -623,9 +597,9 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
t.snapshot(data); t.snapshot(data)
} }
); )
test( test(
'alias locales use the locale scalar', 'alias locales use the locale scalar',
@ -643,15 +617,11 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { aliases } = data.lookup.artist; const { aliases } = data.lookup.artist
t.true( t.is(aliases.find(alias => alias.locale === 'en').name, 'PSY')
aliases.some((alias) => alias.locale === 'en' && alias.name === 'PSY') t.is(aliases.find(alias => alias.locale === 'ko').name, '싸이')
);
t.true(
aliases.some((alias) => alias.locale === 'ko' && alias.name === '싸이')
);
} }
); )
test( test(
'work ISWCs use the ISWC scalar', 'work ISWCs use the ISWC scalar',
@ -667,11 +637,11 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { work } = data.lookup; const { work } = data.lookup
t.is(work.title, 'The Partisan'); t.is(work.title, 'Song of the French Partisan')
t.deepEqual(work.iswcs, ['T-900.755.682-3']); t.deepEqual(work.iswcs, ['T-900.755.682-3'])
} }
); )
test( test(
'URLs may be looked up by resource', 'URLs may be looked up by resource',
@ -687,15 +657,16 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { url } = data.lookup; const { url } = data.lookup
t.is(url.mbid, '4347ffe2-82ec-4059-9520-6a1a3f73a304'); t.is(url.mbid, '4347ffe2-82ec-4059-9520-6a1a3f73a304')
t.is(url.resource, 'http://www.nirvana.com/'); t.is(url.resource, 'http://www.nirvana.com/')
} }
); )
// FIXME: https://github.com/graphql/graphql-js/issues/910
test( test(
'throws an error if given a malformed resource URL', 'throws an error if given a malformed resource URL',
testError, testThrows,
` `
{ {
lookup { lookup {
@ -706,10 +677,12 @@ test(
} }
} }
`, `,
(t, errors) => { async (t, promise) => {
t.regex(errors[0].message, /Malformed URL: http:foo/); const err = await promise
t.true(err instanceof TypeError)
t.is(err.message, 'Malformed URL: http:foo')
} }
); )
test( test(
'release groups can be browsed by type', 'release groups can be browsed by type',
@ -728,15 +701,11 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const releaseGroups = data.browse.releaseGroups.edges.map( const releaseGroups = data.browse.releaseGroups.edges.map(edge => edge.node)
(edge) => edge.node t.is(releaseGroups.length, 8)
); releaseGroups.forEach(releaseGroup => t.is(releaseGroup.primaryType, 'EP'))
t.is(releaseGroups.length, 8);
releaseGroups.forEach((releaseGroup) =>
t.is(releaseGroup.primaryType, 'EP')
);
} }
); )
test( test(
'releases can be browsed by type and status', 'releases can be browsed by type and status',
@ -755,11 +724,11 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const releases = data.browse.releases.edges.map((edge) => edge.node); const releases = data.browse.releases.edges.map(edge => edge.node)
t.is(releases.length, 6); t.is(releases.length, 6)
releases.forEach((release) => t.is(release.status, 'BOOTLEG')); releases.forEach(release => t.is(release.status, 'BOOTLEG'))
} }
); )
test( test(
'releases have an ASIN field', 'releases have an ASIN field',
@ -774,10 +743,10 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { release } = data.lookup; const { release } = data.lookup
t.is(release.asin, 'B01KN6XDS6'); t.is(release.asin, 'B01KN6XDS6')
} }
); )
test( test(
'artists have a list of ISNIs and IPIs', 'artists have a list of ISNIs and IPIs',
@ -793,11 +762,11 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { artist } = data.lookup; const { artist } = data.lookup
t.deepEqual(artist.ipis, ['00006457004']); t.deepEqual(artist.ipis, ['00006457004'])
t.deepEqual(artist.isnis, ['0000000110273481']); t.deepEqual(artist.isnis, ['0000000110273481'])
} }
); )
test( test(
'artistCredits is an alias for artistCredit', 'artistCredits is an alias for artistCredit',
@ -839,42 +808,25 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { recording, release, releaseGroup } = data.lookup; const { recording, release, releaseGroup } = data.lookup
t.deepEqual(recording.artistCredit, [ t.deepEqual(recording.artistCredit, [
{ name: 'Holly Golightly & The Brokeoffs', joinPhrase: '' }, { name: 'Holly Golightly', joinPhrase: ' & ' },
]); { name: 'The Brokeoffs', joinPhrase: '' }
t.deepEqual(recording.artistCredits, recording.artistCredit); ])
t.deepEqual(recording.artistCredits, recording.artistCredit)
t.deepEqual(release.artistCredit, [ t.deepEqual(release.artistCredit, [
{ name: 'Leonard Cohen', joinPhrase: '' }, { name: 'Leonard Cohen', joinPhrase: '' }
]); ])
t.deepEqual(release.artistCredits, release.artistCredit); t.deepEqual(release.artistCredits, release.artistCredit)
t.deepEqual(releaseGroup.artistCredit, [ t.deepEqual(releaseGroup.artistCredit, [
{ name: 'DJ Muggs', joinPhrase: ' vs. ' }, { name: 'DJ Muggs', joinPhrase: ' vs. ' },
{ name: 'Ill Bill', joinPhrase: '' }, { name: 'Ill Bill', joinPhrase: '' }
]); ])
t.deepEqual(releaseGroup.artistCredits, releaseGroup.artistCredit); t.deepEqual(releaseGroup.artistCredits, releaseGroup.artistCredit)
} }
); )
test(
'recordings have a list of ISRCs',
testData,
`
{
lookup {
recording(mbid: "9f9cf187-d6f9-437f-9d98-d59cdbd52757") {
isrcs
}
}
}
`,
(t, data) => {
t.true(data.lookup.recording.isrcs.includes('GBAYE9701376'));
t.true(data.lookup.recording.isrcs.includes('GBBKS1700108'));
}
);
test( test(
'recordings can be browsed by ISRC', 'recordings can be browsed by ISRC',
@ -895,13 +847,13 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const recordings = data.browse.recordings.edges.map((edge) => edge.node); const recordings = data.browse.recordings.edges.map(edge => edge.node)
t.true(data.browse.recordings.totalCount >= 1); t.is(data.browse.recordings.totalCount, 1)
t.true( t.deepEqual(recordings, [
recordings.every((recording) => recording.isrcs.includes('USSUB0200002')) { title: 'About a Girl', isrcs: ['USSUB0200002', 'USUG10200084'] }
); ])
} }
); )
test( test(
'releases can be browsed by Disc ID', 'releases can be browsed by Disc ID',
@ -922,20 +874,20 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const releases = data.browse.releases.edges.map((edge) => edge.node); const releases = data.browse.releases.edges.map(edge => edge.node)
t.true(data.browse.releases.totalCount >= 2); t.true(data.browse.releases.totalCount >= 2)
t.true( t.true(
releases.some( releases.some(
(release) => release.mbid === '5a6e5ad7-c2bd-3484-a20e-121bf981c883' release => release.mbid === '5a6e5ad7-c2bd-3484-a20e-121bf981c883'
) )
); )
t.true( t.true(
releases.some( releases.some(
(release) => release.mbid === '96f6f90e-d831-4f37-bf72-ce2982e459fb' release => release.mbid === '96f6f90e-d831-4f37-bf72-ce2982e459fb'
) )
); )
} }
); )
test( test(
'works can be browsed by ISWC', 'works can be browsed by ISWC',
@ -956,11 +908,13 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const works = data.browse.works.edges.map((edge) => edge.node); const works = data.browse.works.edges.map(edge => edge.node)
t.is(data.browse.works.totalCount, 1); t.is(data.browse.works.totalCount, 1)
t.deepEqual(works, [{ title: 'The Partisan', iswcs: ['T-900.755.682-3'] }]); t.deepEqual(works, [
{ title: 'Song of the French Partisan', iswcs: ['T-900.755.682-3'] }
])
} }
); )
test( test(
'recordings have a length in milliseconds', 'recordings have a length in milliseconds',
@ -975,10 +929,10 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { recording } = data.lookup; const { recording } = data.lookup
t.is(recording.length, 383813); t.is(recording.length, 383493)
} }
); )
test( test(
'collections can be browsed by the entities they contain', 'collections can be browsed by the entities they contain',
@ -1010,20 +964,20 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const collections = data.browse.collections.edges.map((edge) => edge.node); const collections = data.browse.collections.edges.map(edge => edge.node)
t.true(collections.length >= 2); t.true(collections.length >= 2)
t.true(collections.some((collection) => collection.editor === 'arist.on')); t.true(collections.some(collection => collection.editor === 'arist.on'))
t.true( t.true(
collections.some((collection) => collection.editor === 'ListMyCDs.com') collections.some(collection => collection.editor === 'ListMyCDs.com')
); )
collections.forEach((collection) => { collections.forEach(collection => {
t.is(collection.entityType, 'artist'); t.is(collection.entityType, 'artist')
t.is(collection.type, 'Artist'); t.is(collection.type, 'Artist')
t.true(collection.artists.totalCount > 0); t.true(collection.artists.totalCount > 0)
t.true(collection.artists.edges.length > 0); t.true(collection.artists.edges.length > 0)
}); })
} }
); )
test( test(
'collections can be looked up by MBID', 'collections can be looked up by MBID',
@ -1045,11 +999,11 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { collection } = data.lookup; const { collection } = data.lookup
t.is(collection.name, 'Beets Music Collection'); t.is(collection.name, 'Beets Music Collection')
t.is(collection.releases.edges.length, 25); t.is(collection.releases.edges.length, 25)
} }
); )
test( test(
'entities have a collections field', 'entities have a collections field',
@ -1083,12 +1037,12 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { release, artist } = data.lookup; const { release, artist } = data.lookup
t.true(release.collections.totalCount > 0); t.true(release.collections.totalCount > 0)
t.true(release.collections.edges.length > 0); t.true(release.collections.edges.length > 0)
t.true(artist.collections.edges.length > 0); t.true(artist.collections.edges.length > 0)
} }
); )
test( test(
'releases support a list of media', 'releases support a list of media',
@ -1109,25 +1063,25 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { release } = data.lookup; const { release } = data.lookup
t.deepEqual(release.media, [ t.deepEqual(release.media, [
{ {
title: 'Left', title: 'Left',
format: 'CD', format: 'CD',
formatID: '9712d52a-4509-3d4b-a1a2-67c88c643e31', formatID: '9712d52a-4509-3d4b-a1a2-67c88c643e31',
position: 1, position: 1,
trackCount: 12, trackCount: 12
}, },
{ {
title: 'Right', title: 'Right',
format: 'CD', format: 'CD',
formatID: '9712d52a-4509-3d4b-a1a2-67c88c643e31', formatID: '9712d52a-4509-3d4b-a1a2-67c88c643e31',
position: 2, position: 2,
trackCount: 11, trackCount: 11
}, }
]); ])
} }
); )
test( test(
'throws an error if looking up a URL without an argument', 'throws an error if looking up a URL without an argument',
@ -1145,9 +1099,9 @@ test(
t.is( t.is(
errors[0].message, errors[0].message,
'Lookups by a field other than MBID must provide: resource' 'Lookups by a field other than MBID must provide: resource'
); )
} }
); )
test( test(
'some entities support ratings', 'some entities support ratings',
@ -1215,29 +1169,27 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { event, work } = data.lookup; const { event, work } = data.lookup
const artists = data.browse.artists.edges.map((edge) => edge.node); const artists = data.browse.artists.edges.map(edge => edge.node)
const recordings = data.browse.recordings.edges.map((edge) => edge.node); const recordings = data.browse.recordings.edges.map(edge => edge.node)
const labels = data.search.labels.edges.map((edge) => edge.node); const labels = data.search.labels.edges.map(edge => edge.node)
const releaseGroups = data.search.releaseGroups.edges.map( const releaseGroups = data.search.releaseGroups.edges.map(edge => edge.node)
(edge) => edge.node t.is(event.rating.voteCount, 0)
); t.is(event.rating.value, null)
t.is(event.rating.voteCount, 0); t.true(work.rating.voteCount > 0)
t.is(event.rating.value, null); t.true(work.rating.value >= 4)
t.true(work.rating.voteCount > 0); t.true(artists.some(artist => artist.rating.voteCount > 0))
t.true(work.rating.value >= 4); t.true(artists.some(artist => artist.rating.value > 3))
t.true(artists.some((artist) => artist.rating.voteCount > 0)); t.true(recordings.some(recording => recording.rating.voteCount > 0))
t.true(artists.some((artist) => artist.rating.value > 3)); t.true(recordings.some(recording => recording.rating.value > 3))
t.true(recordings.some((recording) => recording.rating.voteCount > 0)); t.true(labels.some(label => label.rating.voteCount > 0))
t.true(recordings.some((recording) => recording.rating.value > 3)); t.true(labels.some(label => label.rating.value > 3))
t.true(labels.some((label) => label.rating.voteCount > 0));
t.true(labels.some((label) => label.rating.value > 3));
t.true( t.true(
releaseGroups.some((releaseGroup) => releaseGroup.rating.voteCount > 0) releaseGroups.some(releaseGroup => releaseGroup.rating.voteCount > 0)
); )
t.true(releaseGroups.some((releaseGroup) => releaseGroup.rating.value > 3)); t.true(releaseGroups.some(releaseGroup => releaseGroup.rating.value > 3))
} }
); )
test( test(
'discs can be looked up by disc ID', 'discs can be looked up by disc ID',
@ -1264,10 +1216,10 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { disc } = data.lookup; const { disc } = data.lookup
t.is(disc.discID, 'TMXdzZkTcc9Jq24PD0w5J9_AXms-'); t.is(disc.discID, 'TMXdzZkTcc9Jq24PD0w5J9_AXms-')
t.is(disc.offsetCount, 9); t.is(disc.offsetCount, 9)
t.is(disc.sectors, 193443); t.is(disc.sectors, 193443)
t.deepEqual(disc.offsets, [ t.deepEqual(disc.offsets, [
150, 150,
18190, 18190,
@ -1277,16 +1229,16 @@ test(
116853, 116853,
151413, 151413,
166833, 166833,
184123, 184123
]); ])
t.is(disc.releases.totalCount, 1); t.is(disc.releases.totalCount, 1)
t.is(disc.releases.edges.length, 1); t.is(disc.releases.edges.length, 1)
t.is( t.is(
disc.releases.edges[0].node.mbid, disc.releases.edges[0].node.mbid,
'7f6d3088-837d-495e-905f-be5c70ac2d82' '7f6d3088-837d-495e-905f-be5c70ac2d82'
); )
} }
); )
test( test(
'release media has a list of discs', 'release media has a list of discs',
@ -1313,13 +1265,12 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { release } = data.lookup; const { release } = data.lookup
t.is(release.media.length, 1); t.is(release.media.length, 1)
t.is(release.media[0].discs.length, 2); t.is(release.media[0].discs.length, 2)
} }
); )
// FIXME: API seems to have changed, potentially a bug in MusicBrainz.
test( test(
'disc queries can be deeply nested', 'disc queries can be deeply nested',
testData, testData,
@ -1370,25 +1321,25 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { disc } = data.lookup; const { disc } = data.lookup
t.true(disc.releases.edges.length > 0); t.true(disc.releases.edges.length > 0)
disc.releases.edges.forEach((release) => { disc.releases.edges.forEach(release => {
t.true(release.node.media.length > 0); t.true(release.node.media.length > 0)
release.node.media.forEach((medium) => { release.node.media.forEach(medium => {
t.true(medium.discs.length > 0); t.true(medium.discs.length > 0)
medium.discs.forEach((disc) => { medium.discs.forEach(disc => {
t.true(disc.releases.edges.length > 0); t.true(disc.releases.edges.length > 0)
disc.releases.edges.forEach((release) => { disc.releases.edges.forEach(release => {
t.true(release.node.media.length > 0); t.true(release.node.media.length > 0)
release.node.media.forEach((medium) => { release.node.media.forEach(medium => {
t.true(medium.discs.length > 0); t.true(medium.discs.length > 0)
}); })
}); })
}); })
}); })
}); })
} }
); )
test( test(
'entities support tags', 'entities support tags',
@ -1426,50 +1377,11 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { label } = data.lookup; const { label } = data.lookup
const artists = data.search.artists.edges.map((edge) => edge.node); const artists = data.search.artists.edges.map(edge => edge.node)
t.true(label.tags.edges.some((edge) => edge.node.name === 'indie folk')); t.true(label.tags.edges.some(edge => edge.node.name === 'indie folk'))
t.true(label.tags.edges.some((edge) => edge.node.count > 0)); t.true(label.tags.edges.some(edge => edge.node.count > 0))
t.true( t.true(artists[0].tags.edges.some(edge => edge.node.name === 'blues rock'))
artists[0].tags.edges.some((edge) => edge.node.name === 'blues rock') t.true(artists[0].tags.edges.some(edge => edge.node.count > 0))
);
t.true(artists[0].tags.edges.some((edge) => edge.node.count > 0));
} }
); )
test(
'releases can include tracks',
testData,
`
{
lookup {
release(mbid: "fba5f8fe-c6c8-4511-8562-c9febf482674") {
media {
trackCount
position
formatID
format
tracks {
mbid
title
position
number
length
recording {
title
}
}
}
}
}
}
`,
(t, data) => {
t.true(data.lookup.release.media.every((media) => media.tracks.length > 0));
t.true(
data.lookup.release.media.every((media) =>
media.tracks.every((track) => track.recording)
)
);
}
);

View file

@ -1,14 +1,12 @@
import test from 'ava'; import test from 'ava'
import Client from '../../src/api/client.js'; import Client from '../../src/api/client'
test('parseErrorMessage() returns the input error by default', (t) => { test('parseErrorMessage() returns the body or status code', t => {
const client = new Client(); const client = new Client()
const error = { t.is(
name: 'HTTPError', client.parseErrorMessage({ statusCode: 500 }, 'something went wrong'),
response: { 'something went wrong'
statusCode: 500, )
body: 'something went wrong', t.is(client.parseErrorMessage({ statusCode: 500 }, ''), '500')
}, t.is(client.parseErrorMessage({ statusCode: 404 }, {}), '404')
}; })
t.is(client.parseErrorMessage(error), error);
});

View file

@ -1,116 +0,0 @@
[
[
"getBrowseURL() generates a browse URL",
[
]
],
[
"getLookupURL() generates a lookup URL",
[
]
],
[
"getSearchURL() generates a search URL",
[
]
],
[
"lookup() sends a lookup query",
[
{
"body": "",
"method": "GET",
"path": "/ws/2/artist/c8da2e40-bd28-4d4e-813a-bd2f51958ba8?fmt=json",
"rawHeaders": [
"Date",
"Thu, 15 Apr 2021 09:07:10 GMT",
"Content-Type",
"application/json; charset=utf-8",
"Transfer-Encoding",
"chunked",
"Connection",
"close",
"Vary",
"Accept-Encoding",
"X-RateLimit-Limit",
"1200",
"X-RateLimit-Remaining",
"783",
"X-RateLimit-Reset",
"1618477631",
"Server",
"Plack::Handler::Starlet",
"ETag",
"W/\"80f1f0e96231d62805a0ac7d54414c6f\"",
"Access-Control-Allow-Origin",
"*",
"X-Cache-Status",
"STALE"
],
"reqheaders": {
"accept": "application/json",
"accept-encoding": "gzip, deflate, br",
"host": "musicbrainz.org"
},
"response": "{\"life-span\":{\"end\":null,\"ended\":false,\"begin\":\"2013\"},\"id\":\"c8da2e40-bd28-4d4e-813a-bd2f51958ba8\",\"type-id\":\"e431f5f6-b5d2-343d-8b36-72607fffb74b\",\"begin_area\":{\"type-id\":null,\"id\":\"10adc6b5-63bf-4b4e-993e-ed83b05c22fc\",\"sort-name\":\"Seattle\",\"name\":\"Seattle\",\"type\":null,\"disambiguation\":\"\"},\"country\":null,\"gender-id\":null,\"disambiguation\":\"Seattle trio\",\"isnis\":[],\"gender\":null,\"sort-name\":\"Lures\",\"name\":\"Lures\",\"end-area\":null,\"begin-area\":{\"type-id\":null,\"id\":\"10adc6b5-63bf-4b4e-993e-ed83b05c22fc\",\"sort-name\":\"Seattle\",\"name\":\"Seattle\",\"type\":null,\"disambiguation\":\"\"},\"end_area\":null,\"ipis\":[],\"type\":\"Group\",\"area\":{\"disambiguation\":\"\",\"type\":null,\"type-id\":null,\"id\":\"10adc6b5-63bf-4b4e-993e-ed83b05c22fc\",\"name\":\"Seattle\",\"sort-name\":\"Seattle\"}}",
"responseIsBinary": false,
"scope": "http://musicbrainz.org:80",
"status": 200
}
]
],
[
"rejects non-MusicBrainz errors",
[
]
],
[
"rejects the promise when the API returns an error",
[
{
"body": "",
"method": "GET",
"path": "/ws/2/artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da?inc=foobar&fmt=json",
"rawHeaders": [
"Date",
"Thu, 15 Apr 2021 09:07:10 GMT",
"Content-Type",
"application/json; charset=utf-8",
"Content-Length",
"144",
"Connection",
"close",
"X-RateLimit-Limit",
"1200",
"X-RateLimit-Remaining",
"664",
"X-RateLimit-Reset",
"1618477631",
"Server",
"Plack::Handler::Starlet",
"ETag",
"\"294308a5f1930ea2b39414c0b8ec853c\"",
"Access-Control-Allow-Origin",
"*"
],
"reqheaders": {
"accept": "application/json",
"accept-encoding": "gzip, deflate, br",
"host": "musicbrainz.org"
},
"response": {
"error": "foobar is not a valid inc parameter for the artist resource.",
"help": "For usage, please see: https://musicbrainz.org/development/mmd"
},
"responseIsBinary": false,
"scope": "http://musicbrainz.org:80",
"status": 400
}
]
],
[
"uses the default error impementation if there is no JSON error",
[
]
]
]

View file

@ -1,87 +1,84 @@
import test from 'ava'; import test from 'ava'
import MusicBrainz from '../../src/api/index.js'; import MusicBrainz, { MusicBrainzError } from '../../src/api'
import client from '../helpers/client/musicbrainz.js'; import client from '../helpers/client/musicbrainz'
test('getLookupURL() generates a lookup URL', (t) => { test('getLookupURL() generates a lookup URL', t => {
t.is( t.is(
client.getLookupURL('artist', 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8', { client.getLookupURL('artist', 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8', {
inc: ['recordings', 'release-groups'], inc: ['recordings', 'release-groups']
}), }),
'artist/c8da2e40-bd28-4d4e-813a-bd2f51958ba8?inc=recordings%2Brelease-groups' 'artist/c8da2e40-bd28-4d4e-813a-bd2f51958ba8?inc=recordings%2Brelease-groups'
); )
}); })
test('getBrowseURL() generates a browse URL', (t) => { test('getBrowseURL() generates a browse URL', t => {
t.is( t.is(
client.getBrowseURL('recording', { client.getBrowseURL('recording', {
artist: 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8', artist: 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8',
limit: null, limit: null,
offset: 0, offset: 0
}), }),
'recording?artist=c8da2e40-bd28-4d4e-813a-bd2f51958ba8&offset=0' 'recording?artist=c8da2e40-bd28-4d4e-813a-bd2f51958ba8&offset=0'
); )
}); })
test('getSearchURL() generates a search URL', (t) => { test('getSearchURL() generates a search URL', t => {
t.is( t.is(
client.getSearchURL('artist', 'Lures', { inc: null }), client.getSearchURL('artist', 'Lures', { inc: null }),
'artist?query=Lures' 'artist?query=Lures'
); )
}); })
test('lookup() sends a lookup query', async (t) => { test('lookup() sends a lookup query', t => {
const response = await client.lookup( return client
'artist', .lookup('artist', 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8')
'c8da2e40-bd28-4d4e-813a-bd2f51958ba8' .then(response => {
); t.is(response.id, 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8')
t.is(response.type, 'Group')
})
})
t.is(response.id, 'c8da2e40-bd28-4d4e-813a-bd2f51958ba8'); test('rejects the promise when the API returns an error', t => {
t.is(response.type, 'Group');
});
test('rejects the promise when the API returns an error', (t) => {
const req = client.lookup('artist', '5b11f4ce-a62d-471e-81fc-a69a8278c7da', { const req = client.lookup('artist', '5b11f4ce-a62d-471e-81fc-a69a8278c7da', {
inc: ['foobar'], inc: ['foobar']
}); })
return t.throwsAsync(req, { return t.throws(req, MusicBrainzError)
name: 'MusicBrainzError', })
message: 'foobar is not a valid inc parameter for the artist resource.',
});
});
test('rejects non-MusicBrainz errors', (t) => { test('shouldRetry() retries only 5xx responses from MusicBrainz', t => {
const client = new MusicBrainz({ baseURL: '$!@#$' }); t.true(client.shouldRetry(new MusicBrainzError('error', 500)))
return t.throwsAsync( t.true(client.shouldRetry(new MusicBrainzError('error', 501)))
t.true(client.shouldRetry(new MusicBrainzError('error', 598)))
t.true(client.shouldRetry(new MusicBrainzError('error', 599)))
t.false(client.shouldRetry(new MusicBrainzError('error', 404)))
t.false(client.shouldRetry(new MusicBrainzError('error', 499)))
t.false(client.shouldRetry(new MusicBrainzError('error', 600)))
})
test('shouldRetry() retries only transient local connection issues', t => {
t.true(client.shouldRetry({ code: 'ECONNRESET' }))
t.true(client.shouldRetry({ code: 'ENOTFOUND' }))
t.true(client.shouldRetry({ code: 'ESOCKETTIMEDOUT' }))
t.true(client.shouldRetry({ code: 'ETIMEDOUT' }))
t.true(client.shouldRetry({ code: 'ECONNREFUSED' }))
t.true(client.shouldRetry({ code: 'EHOSTUNREACH' }))
t.true(client.shouldRetry({ code: 'EPIPE' }))
t.true(client.shouldRetry({ code: 'EAI_AGAIN' }))
t.false(client.shouldRetry({ code: 'ENOENT' }))
t.false(client.shouldRetry({ code: 'EACCES' }))
t.false(client.shouldRetry({ code: 'EPERM' }))
})
test('rejects non-MusicBrainz errors', t => {
const client = new MusicBrainz({ baseURL: '$!@#$' })
return t.throws(
client.get('artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da'), client.get('artist/5b11f4ce-a62d-471e-81fc-a69a8278c7da'),
{ Error
name: 'TypeError', )
} })
);
});
test('uses the default error impementation if there is no JSON error', (t) => { test('uses the default error impementation if there is no JSON error', t => {
let error = { t.is(client.parseErrorMessage({ statusCode: 501 }, 'yikes'), 'yikes')
name: 'HTTPError', t.is(client.parseErrorMessage({ statusCode: 500 }, {}), '500')
response: { t.is(client.parseErrorMessage({ statusCode: 404 }, null), '404')
statusCode: 501, })
body: 'yikes',
},
};
t.is(client.parseErrorMessage(error), error);
error = {
name: 'HTTPError',
response: {
statusCode: 500,
body: {},
},
};
t.is(client.parseErrorMessage(error), error);
error = {
name: 'HTTPError',
response: {
statusCode: 404,
body: null,
},
};
t.is(client.parseErrorMessage(error), error);
});

View file

@ -1,8 +1,2 @@
import test from 'ava'; process.env.TEST_SCHEMA = 'baseSchema'
import { baseSchema } from '../src/schema.js'; require('./_schema')
test.before((t) => {
t.context.schema = baseSchema;
});
import './_schema.js';

View file

@ -1,13 +1,2 @@
import test from 'ava'; process.env.TEST_SCHEMA = 'extendedSchema'
import { baseSchema, createSchema } from '../src/schema.js'; require('./_schema')
import { defaultExtensions, loadExtension } from '../src/index.js';
test.before(async (t) => {
const extensions = await Promise.all(
defaultExtensions.map((extension) => loadExtension(extension))
);
const schema = createSchema(baseSchema, { extensions });
t.context.schema = schema;
});
import './_schema.js';

View file

@ -1,87 +1,58 @@
import test from 'ava'; import test from 'ava'
import client from '../../helpers/client/cover-art-archive.js'; import client from '../../helpers/client/cover-art-archive'
test('can retrieve a front image URL', async (t) => { test('can retrieve a front image URL', t => {
const url = await client.imageURL( return client
'release', .imageURL('release', '76df3287-6cda-33eb-8e9a-044b5e15ffdd', 'front')
'76df3287-6cda-33eb-8e9a-044b5e15ffdd', .then(url => {
'front' t.is(
); url,
'http://archive.org/download/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd-829521842.jpg'
)
})
})
t.is( test('can retrieve a back image URL', t => {
url, return client
'http://archive.org/download/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd-829521842.jpg' .imageURL('release', '76df3287-6cda-33eb-8e9a-044b5e15ffdd', 'back')
); .then(url => {
}); t.is(
url,
'http://archive.org/download/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd-5769317885.jpg'
)
})
})
test('can retrieve a back image URL', async (t) => { test('can retrieve a list of release images', t => {
const url = await client.imageURL( return client
'release', .images('release', '76df3287-6cda-33eb-8e9a-044b5e15ffdd')
'76df3287-6cda-33eb-8e9a-044b5e15ffdd', .then(data => {
'back' t.is(
); data.release,
'http://musicbrainz.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd'
)
t.true(data.images.length >= 3)
data.images.forEach(image => {
t.true(image.approved)
t.truthy(image.image)
t.truthy(image.id)
t.truthy(image.thumbnails.small)
t.truthy(image.thumbnails.large)
})
t.true(data.images.some(image => image.front))
t.true(data.images.some(image => image.back))
t.true(data.images.some(image => image.types.indexOf('Front') !== -1))
t.true(data.images.some(image => image.types.indexOf('Back') !== -1))
t.true(data.images.some(image => image.types.indexOf('Medium') !== -1))
})
})
t.is( test('throws an error if given an invalid MBID', t => {
url, return t.throws(client.images('release', 'xyz'), client.errorClass)
'http://archive.org/download/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd-5769317885.jpg' })
);
});
test('can retrieve a list of release images', async (t) => { test('uses the default error impementation if there is no HTML error', t => {
const data = await client.images( t.is(client.parseErrorMessage({ statusCode: 501 }, 'yikes'), 'yikes')
'release', t.is(client.parseErrorMessage({ statusCode: 500 }, ''), '500')
'76df3287-6cda-33eb-8e9a-044b5e15ffdd' t.is(client.parseErrorMessage({ statusCode: 404 }, null), '404')
); })
t.is(
data.release,
'https://musicbrainz.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd'
);
t.true(data.images.length >= 3);
data.images.forEach((image) => {
t.true(image.approved);
t.truthy(image.image);
t.truthy(image.id);
t.truthy(image.thumbnails.small);
t.truthy(image.thumbnails.large);
});
t.true(data.images.some((image) => image.front));
t.true(data.images.some((image) => image.back));
t.true(data.images.some((image) => image.types.indexOf('Front') !== -1));
t.true(data.images.some((image) => image.types.indexOf('Back') !== -1));
t.true(data.images.some((image) => image.types.indexOf('Medium') !== -1));
});
test('throws an error if given an invalid MBID', (t) => {
return t.throwsAsync(client.images('release', 'xyz'), {
name: 'CoverArtArchiveError',
message: 'Bad Request: invalid MBID specified',
});
});
test('uses the default error impementation if there is no HTML error', (t) => {
let error = {
name: 'HTTPError',
response: {
statusCode: 501,
body: 'yikes',
},
};
t.is(client.parseErrorMessage(error), error);
error = {
name: 'HTTPError',
response: {
statusCode: 500,
body: '',
},
};
t.is(client.parseErrorMessage(error), error);
error = {
name: 'HTTPError',
response: {
statusCode: 500,
body: null,
},
};
t.is(client.parseErrorMessage(error), error);
});

View file

@ -1,332 +0,0 @@
[
[
"can retrieve a back image URL",
[
{
"body": "",
"method": "HEAD",
"path": "/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/back",
"rawHeaders": [
"Date",
"Thu, 15 Apr 2021 09:07:12 GMT",
"Content-Type",
"text/plain; charset=utf-8",
"Content-Length",
"132",
"Connection",
"close",
"Location",
"http://archive.org/download/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd-5769317885.jpg",
"Access-Control-Allow-Origin",
"*"
],
"reqheaders": {
"accept": "application/json",
"accept-encoding": "gzip, deflate, br",
"host": "coverartarchive.org"
},
"response": "",
"responseIsBinary": false,
"scope": "http://coverartarchive.org:80",
"status": 307
}
]
],
[
"can retrieve a front image URL",
[
{
"body": "",
"method": "HEAD",
"path": "/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/front",
"rawHeaders": [
"Date",
"Thu, 15 Apr 2021 09:07:12 GMT",
"Content-Type",
"text/plain; charset=utf-8",
"Content-Length",
"131",
"Connection",
"close",
"Location",
"http://archive.org/download/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd-829521842.jpg",
"Access-Control-Allow-Origin",
"*"
],
"reqheaders": {
"accept": "application/json",
"accept-encoding": "gzip, deflate, br",
"host": "coverartarchive.org"
},
"response": "",
"responseIsBinary": false,
"scope": "http://coverartarchive.org:80",
"status": 307
}
]
],
[
"can retrieve a list of release images",
[
{
"body": "",
"method": "GET",
"path": "/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd",
"rawHeaders": [
"Date",
"Thu, 15 Apr 2021 09:07:12 GMT",
"Content-Type",
"text/plain; charset=utf-8",
"Content-Length",
"86",
"Connection",
"close",
"Location",
"http://archive.org/download/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd/index.json",
"Access-Control-Allow-Origin",
"*"
],
"reqheaders": {
"accept": "application/json",
"accept-encoding": "gzip, deflate, br",
"host": "coverartarchive.org"
},
"response": "See: http://archive.org/download/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd/index.json\n",
"responseIsBinary": false,
"scope": "http://coverartarchive.org:80",
"status": 307
},
{
"body": "",
"method": "GET",
"path": "/download/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd/index.json",
"rawHeaders": [
"Server",
"nginx/1.16.1 (Ubuntu)",
"Date",
"Thu, 15 Apr 2021 09:07:12 GMT",
"Content-Type",
"text/html; charset=UTF-8",
"Transfer-Encoding",
"chunked",
"Connection",
"close",
"Access-Control-Allow-Origin",
"*",
"Accept-Ranges",
"bytes",
"Location",
"http://ia802607.us.archive.org/32/items/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd/index.json",
"Strict-Transport-Security",
"max-age=15724800",
"Referrer-Policy",
"no-referrer-when-downgrade"
],
"reqheaders": {
"accept": "application/json",
"accept-encoding": "gzip, deflate, br",
"host": "archive.org"
},
"response": "",
"responseIsBinary": false,
"scope": "http://archive.org:80",
"status": 302
},
{
"body": "",
"method": "GET",
"path": "/32/items/mbid-76df3287-6cda-33eb-8e9a-044b5e15ffdd/index.json",
"rawHeaders": [
"Server",
"nginx/1.18.0 (Ubuntu)",
"Date",
"Thu, 15 Apr 2021 09:07:13 GMT",
"Content-Type",
"application/json",
"Content-Length",
"4445",
"Last-Modified",
"Tue, 12 Nov 2019 22:11:45 GMT",
"Connection",
"close",
"ETag",
"\"5dcb2e21-115d\"",
"Strict-Transport-Security",
"max-age=15724800",
"Expires",
"Thu, 15 Apr 2021 15:07:13 GMT",
"Cache-Control",
"max-age=21600",
"Access-Control-Allow-Origin",
"*",
"Accept-Ranges",
"bytes"
],
"reqheaders": {
"accept": "application/json",
"accept-encoding": "gzip, deflate, br",
"host": "ia802607.us.archive.org"
},
"response": {
"images": [
{
"approved": true,
"back": false,
"comment": "",
"edit": 17462565,
"front": true,
"id": 829521842,
"image": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/829521842.jpg",
"thumbnails": {
"1200": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/829521842-1200.jpg",
"250": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/829521842-250.jpg",
"500": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/829521842-500.jpg",
"large": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/829521842-500.jpg",
"small": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/829521842-250.jpg"
},
"types": [
"Front"
]
},
{
"approved": true,
"back": false,
"comment": "",
"edit": 65037291,
"front": false,
"id": 24546038908,
"image": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546038908.jpg",
"thumbnails": {
"1200": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546038908-1200.jpg",
"250": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546038908-250.jpg",
"500": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546038908-500.jpg",
"large": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546038908-500.jpg",
"small": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546038908-250.jpg"
},
"types": [
"Front",
"Booklet"
]
},
{
"approved": true,
"back": false,
"comment": "",
"edit": 65037298,
"front": false,
"id": 24546039829,
"image": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546039829.jpg",
"thumbnails": {
"1200": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546039829-1200.jpg",
"250": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546039829-250.jpg",
"500": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546039829-500.jpg",
"large": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546039829-500.jpg",
"small": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546039829-250.jpg"
},
"types": [
"Booklet"
]
},
{
"approved": true,
"back": false,
"comment": "",
"edit": 65037300,
"front": false,
"id": 24546040945,
"image": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546040945.jpg",
"thumbnails": {
"1200": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546040945-1200.jpg",
"250": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546040945-250.jpg",
"500": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546040945-500.jpg",
"large": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546040945-500.jpg",
"small": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/24546040945-250.jpg"
},
"types": [
"Booklet"
]
},
{
"approved": true,
"back": true,
"comment": "",
"edit": 24923554,
"front": false,
"id": 5769317885,
"image": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/5769317885.jpg",
"thumbnails": {
"1200": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/5769317885-1200.jpg",
"250": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/5769317885-250.jpg",
"500": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/5769317885-500.jpg",
"large": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/5769317885-500.jpg",
"small": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/5769317885-250.jpg"
},
"types": [
"Back"
]
},
{
"approved": true,
"back": false,
"comment": "",
"edit": 24923552,
"front": false,
"id": 5769316809,
"image": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/5769316809.jpg",
"thumbnails": {
"1200": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/5769316809-1200.jpg",
"250": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/5769316809-250.jpg",
"500": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/5769316809-500.jpg",
"large": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/5769316809-500.jpg",
"small": "http://coverartarchive.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd/5769316809-250.jpg"
},
"types": [
"Medium"
]
}
],
"release": "https://musicbrainz.org/release/76df3287-6cda-33eb-8e9a-044b5e15ffdd"
},
"responseIsBinary": false,
"scope": "http://ia802607.us.archive.org:80",
"status": 200
}
]
],
[
"throws an error if given an invalid MBID",
[
{
"body": "",
"method": "GET",
"path": "/release/xyz",
"rawHeaders": [
"Date",
"Thu, 15 Apr 2021 09:07:13 GMT",
"Content-Type",
"text/html",
"Content-Length",
"138",
"Connection",
"close",
"Access-Control-Allow-Origin",
"*"
],
"reqheaders": {
"accept": "application/json",
"accept-encoding": "gzip, deflate, br",
"host": "coverartarchive.org"
},
"response": "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n<title>400 Bad Request</title>\n<h1>Bad Request</h1>\n<p>invalid MBID specified</p>\n",
"responseIsBinary": false,
"scope": "http://coverartarchive.org:80",
"status": 400
}
]
],
[
"uses the default error impementation if there is no HTML error",
[
]
]
]

File diff suppressed because one or more lines are too long

View file

@ -1,27 +1,20 @@
import test from 'ava'; import test from 'ava'
import GraphQL from 'graphql'; import { graphql } from 'graphql'
import extension from '../../../src/extensions/cover-art-archive/index.js'; import extension from '../../../src/extensions/cover-art-archive'
import { baseSchema, applyExtension } from '../../../src/schema.js'; import baseSchema, { applyExtension } from '../../../src/schema'
import baseContext from '../../helpers/context.js'; import baseContext from '../../helpers/context'
const { graphql } = GraphQL; const schema = applyExtension(extension, baseSchema)
const context = extension.extendContext(baseContext)
const schema = applyExtension(extension, baseSchema);
const context = extension.extendContext(baseContext, {
coverArtArchive: {
limit: Infinity,
period: 0,
},
});
function testData(t, query, handler) { function testData(t, query, handler) {
return graphql(schema, query, null, context).then((result) => { return graphql(schema, query, null, context).then(result => {
if (result.errors !== undefined) { if (result.errors !== undefined) {
result.errors.forEach((error) => t.log(error)); console.log(result.errors)
} }
t.is(result.errors, undefined); t.is(result.errors, undefined)
return handler(t, result.data); return handler(t, result.data)
}); })
} }
test( test(
@ -40,11 +33,11 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { coverArtArchive } = data.lookup.release; const { coverArtArchive } = data.lookup.release
t.true(coverArtArchive.artwork); t.true(coverArtArchive.artwork)
t.true(coverArtArchive.count >= 10); t.true(coverArtArchive.count >= 10)
} }
); )
test( test(
'releases have a set of cover art images', 'releases have a set of cover art images',
@ -76,50 +69,48 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { coverArtArchive } = data.lookup.release; const { coverArtArchive } = data.lookup.release
t.is( t.is(
coverArtArchive.front, coverArtArchive.front,
'http://coverartarchive.org/release/b84ee12a-09ef-421b-82de-0441a926375b/1611507818.jpg' 'http://coverartarchive.org/release/b84ee12a-09ef-421b-82de-0441a926375b/1611507818.jpg'
); )
t.is( t.is(
coverArtArchive.back, coverArtArchive.back,
'http://coverartarchive.org/release/b84ee12a-09ef-421b-82de-0441a926375b/13536418798.jpg' 'http://coverartarchive.org/release/b84ee12a-09ef-421b-82de-0441a926375b/13536418798.jpg'
); )
t.true(coverArtArchive.images.length >= 10); t.true(coverArtArchive.images.length >= 10)
t.true(coverArtArchive.images.some((image) => image.front === true)); t.true(coverArtArchive.images.some(image => image.front === true))
t.true(coverArtArchive.images.some((image) => image.back === true)); t.true(coverArtArchive.images.some(image => image.back === true))
t.true( t.true(
coverArtArchive.images.some((image) => image.types.indexOf('Front') >= 0) coverArtArchive.images.some(image => image.types.indexOf('Front') >= 0)
); )
t.true( t.true(
coverArtArchive.images.some((image) => image.types.indexOf('Back') >= 0) coverArtArchive.images.some(image => image.types.indexOf('Back') >= 0)
); )
t.true( t.true(
coverArtArchive.images.some((image) => image.types.indexOf('Liner') >= 0) coverArtArchive.images.some(image => image.types.indexOf('Liner') >= 0)
); )
t.true( t.true(
coverArtArchive.images.some((image) => image.types.indexOf('Poster') >= 0) coverArtArchive.images.some(image => image.types.indexOf('Poster') >= 0)
); )
t.true( t.true(
coverArtArchive.images.some((image) => image.types.indexOf('Medium') >= 0) coverArtArchive.images.some(image => image.types.indexOf('Medium') >= 0)
); )
t.true(coverArtArchive.images.some((image) => image.edit === 18544122)); t.true(coverArtArchive.images.some(image => image.edit === 18544122))
t.true(coverArtArchive.images.some((image) => image.comment === '')); t.true(coverArtArchive.images.some(image => image.comment === ''))
t.true( t.true(coverArtArchive.images.some(image => image.fileID === '1611507818'))
coverArtArchive.images.some((image) => image.fileID === '1611507818')
);
t.true( t.true(
coverArtArchive.images.some( coverArtArchive.images.some(
(image) => image =>
image.image === image.image ===
'http://coverartarchive.org/release/b84ee12a-09ef-421b-82de-0441a926375b/13536422691.jpg' 'http://coverartarchive.org/release/b84ee12a-09ef-421b-82de-0441a926375b/13536422691.jpg'
) )
); )
t.true(coverArtArchive.images.every((image) => image.approved === true)); t.true(coverArtArchive.images.every(image => image.approved === true))
t.true(coverArtArchive.images.every((image) => image.thumbnails.small)); t.true(coverArtArchive.images.every(image => image.thumbnails.small))
t.true(coverArtArchive.images.every((image) => image.thumbnails.large)); t.true(coverArtArchive.images.every(image => image.thumbnails.large))
} }
); )
test( test(
'can request a size for front and back cover art', 'can request a size for front and back cover art',
@ -138,21 +129,21 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { coverArtArchive } = data.lookup.release; const { coverArtArchive } = data.lookup.release
t.is( t.is(
coverArtArchive.front, coverArtArchive.front,
'http://coverartarchive.org/release/b84ee12a-09ef-421b-82de-0441a926375b/1611507818-500.jpg' 'http://coverartarchive.org/release/b84ee12a-09ef-421b-82de-0441a926375b/1611507818-500.jpg'
); )
t.is( t.is(
coverArtArchive.back, coverArtArchive.back,
'http://coverartarchive.org/release/b84ee12a-09ef-421b-82de-0441a926375b/13536418798-250.jpg' 'http://coverartarchive.org/release/b84ee12a-09ef-421b-82de-0441a926375b/13536418798-250.jpg'
); )
t.is( t.is(
coverArtArchive.fullFront, coverArtArchive.fullFront,
'http://coverartarchive.org/release/b84ee12a-09ef-421b-82de-0441a926375b/1611507818.jpg' 'http://coverartarchive.org/release/b84ee12a-09ef-421b-82de-0441a926375b/1611507818.jpg'
); )
} }
); )
test( test(
'release groups have a front cover art image', 'release groups have a front cover art image',
@ -178,16 +169,18 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { coverArtArchive } = data.lookup.releaseGroup; const { coverArtArchive } = data.lookup.releaseGroup
const { front } = coverArtArchive; t.true(coverArtArchive.artwork)
t.true(coverArtArchive.artwork); t.is(
t.snapshot({ front }); coverArtArchive.front,
t.is(coverArtArchive.release.mbid, '6ef6e6cd-ad36-4c2f-816d-121bfb2f6774'); 'http://coverartarchive.org/release/25fbfbb4-b1ee-4448-aadf-ae3bc2e2dd27/1675312275.jpg'
t.is(coverArtArchive.release.title, 'The Dark Side of the Moon'); )
t.is(coverArtArchive.images.length, 1); t.is(coverArtArchive.release.mbid, '25fbfbb4-b1ee-4448-aadf-ae3bc2e2dd27')
t.true(coverArtArchive.images[0].front); t.is(coverArtArchive.release.title, 'The Dark Side of the Moon')
t.is(coverArtArchive.images.length, 1)
t.true(coverArtArchive.images[0].front)
} }
); )
test( test(
'release groups have different cover art sizes available', 'release groups have different cover art sizes available',
@ -205,11 +198,17 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const { coverArtArchive } = data.lookup.releaseGroup; const { coverArtArchive } = data.lookup.releaseGroup
const { small, large } = coverArtArchive; t.is(
t.snapshot({ small, large }); coverArtArchive.small,
'http://coverartarchive.org/release/25fbfbb4-b1ee-4448-aadf-ae3bc2e2dd27/1675312275-250.jpg'
)
t.is(
coverArtArchive.large,
'http://coverartarchive.org/release/25fbfbb4-b1ee-4448-aadf-ae3bc2e2dd27/1675312275-500.jpg'
)
} }
); )
test( test(
'can retrieve cover art in searches', 'can retrieve cover art in searches',
@ -235,15 +234,11 @@ test(
} }
`, `,
(t, data) => { (t, data) => {
const releases = data.search.releases.edges.map((edge) => edge.node); const releases = data.search.releases.edges.map(edge => edge.node)
t.is(releases.length, 25); t.is(releases.length, 25)
t.true( t.true(releases.some(release => release.coverArtArchive.artwork === true))
releases.some((release) => release.coverArtArchive.artwork === true) t.true(releases.some(release => release.coverArtArchive.images.length > 0))
); t.true(releases.some(release => release.coverArtArchive.front === null))
t.true( t.true(releases.some(release => release.coverArtArchive.back === null))
releases.some((release) => release.coverArtArchive.images.length > 0)
);
t.true(releases.some((release) => release.coverArtArchive.front === null));
t.true(releases.some((release) => release.coverArtArchive.back === null));
} }
); )

View file

@ -1,22 +0,0 @@
# Snapshot report for `test/extensions/cover-art-archive/schema.js`
The actual snapshot is saved in `schema.js.snap`.
Generated by [AVA](https://avajs.dev).
## release groups have a front cover art image
> Snapshot 1
{
front: 'http://coverartarchive.org/release/6ef6e6cd-ad36-4c2f-816d-121bfb2f6774/12699438648.jpg',
}
## release groups have different cover art sizes available
> Snapshot 1
{
large: 'http://coverartarchive.org/release/6ef6e6cd-ad36-4c2f-816d-121bfb2f6774/12699438648-500.jpg',
small: 'http://coverartarchive.org/release/6ef6e6cd-ad36-4c2f-816d-121bfb2f6774/12699438648-250.jpg',
}

Some files were not shown because too many files have changed in this diff Show more