Add client usage section to README

This commit is contained in:
Brian Beck 2016-12-20 18:04:08 -08:00
parent 1d63867e07
commit 53d7d84397
2 changed files with 40 additions and 5 deletions

View file

@ -1,12 +1,12 @@
# graphbrainz
# GraphBrainz
[![build status](https://img.shields.io/travis/exogen/graphbrainz.svg)](https://travis-ci.org/exogen/graphbrainz)
[![coverage](https://img.shields.io/codecov/c/github/exogen/graphbrainz.svg)](https://codecov.io/gh/exogen/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)
An [Express][] server and middleware for querying [MusicBrainz][] using
[GraphQL][].
A [GraphQL][] schema, [Express][] server, and middleware for querying the
[MusicBrainz][] API.
```sh
npm install graphbrainz --save
@ -90,6 +90,40 @@ The `graphbrainz` middleware function accepts the following options:
* Any remaining options are passed along to the standard GraphQL middleware.
See the [express-graphql][] documentation for more information.
### As a client
If you just want to make queries from your app without running a separate server
or exposing a GraphQL endpoint, use the GraphBrainz schema with a library like
[GraphQL.js][graphql-js]. You just need to create the `context` object that the
GraphBrainz resolvers expect, like so:
```js
import { graphql } from 'graphql';
import { MusicBrainz, CoverArtArchive } from 'graphbrainz/api';
import createLoaders from 'graphbrainz/loaders';
import schema from 'graphbrainz/schema';
const client = new MusicBrainz();
const coverArtClient = new CoverArtArchive();
const loaders = createLoaders(client, coverArtClient);
const context = { client, coverArtClient, loaders };
graphql(schema, `
{
lookup {
releaseGroup(mbid: "99599db8-0e36-4a93-b0e8-350e9d7502a9") {
title
}
}
}
`, null, context).then(result => {
const { releaseGroup } = result.data.lookup;
console.log(`The album title is “${releaseGroup.title}”.`);
}).catch(err => {
console.error(err);
});
```
### Environment Variables
* **`MUSICBRAINZ_BASE_URL`**: The base MusicBrainz API URL to use. Change this
@ -317,7 +351,7 @@ the fields you need and a reasonable level of nested entities  unless you ar
willing to wait.
You can also set up a [local MusicBrainz mirror][mirror] and configure
graphbrainz to use that with no rate limiting.
GraphBrainz to use that with no rate limiting.
## Schema
@ -331,6 +365,7 @@ See the [GraphQL schema][schema] or the [types][] documentation.
[dotenv]: https://www.npmjs.com/package/dotenv
[debug]: https://www.npmjs.com/package/debug
[GraphiQL]: https://github.com/graphql/graphiql
[graphql-js]: https://github.com/graphql/graphql-js
[Relay]: https://facebook.github.io/relay/
[schema]: docs/schema.md
[types]: docs/types.md

View file

@ -27,7 +27,7 @@
"deploy": "./scripts/deploy.sh",
"lint": "standard --verbose | snazzy",
"prepublish": "npm run clean:lib && npm run build:lib",
"preversion": "npm run build:docs && git add schema.json docs",
"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)\"",