Skip to content

Commit

Permalink
Merge pull request #13 from ricardocasares/feat/station-query
Browse files Browse the repository at this point in the history
feat: pagination parameters
  • Loading branch information
ricardocasares authored Apr 16, 2019
2 parents 0eaec39 + a4c39d2 commit d93bef5
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 22 deletions.
40 changes: 22 additions & 18 deletions client.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@
const { encode } = require("querystring");
const fetch = require("isomorphic-unfetch");

const URL = "http://www.radio-browser.info/webservice";
const STATIONS = `/json/stations`;
const BY_ID = `${STATIONS}/byid`;
const BY_NAME = `${STATIONS}/byname`;
const BY_ID = `${URL}${STATIONS}/byid`;
const SEARCH = `${URL}${STATIONS}/search`;

const cfg = {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"User-Agent": "bemba/radio-gql"
},
body: JSON.stringify({ order: "votes", reverse: "true", limit: 50 })
};

const get = async id =>
fetch(`${URL}${BY_ID}/${id}`, cfg)
.then(res => res.json())
.then(([station]) => station);
const request = (url, query = {}, pagination = {}) =>
console.log([url, "?", encode(query)].join("")) ||
fetch([url, "?", encode(query)].join(""), {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
"User-Agent": "bemba/radio-gql"
},
body: JSON.stringify({
order: "votes",
reverse: "true",
limit: 50,
...pagination
})
}).then(res => res.json());

const search = async name =>
fetch(`${URL}${BY_NAME}/${name}`, cfg).then(res => res.json());
const get = id => request(`${BY_ID}/${id}`).then(([station]) => station);
const search = ({ query, pagination }) =>
request(`${SEARCH}`, query, pagination);

module.exports = {
get,
Expand Down
54 changes: 50 additions & 4 deletions schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,32 @@ const {
GraphQLList,
GraphQLSchema,
GraphQLString,
GraphQLBoolean,
GraphQLNonNull,
GraphQLObjectType
GraphQLEnumType,
GraphQLObjectType,
GraphQLInputObjectType
} = require("graphql");

var OrderType = new GraphQLEnumType({
name: "Order",
values: {
NAME: { value: "name" },
URL: { value: "url" },
TAGS: { value: "tags" },
COUNTRY: { value: "country" },
STATE: { value: "state" },
LANGUAGE: { value: "language" },
VOTES: { value: "votes" },
CODEC: { value: "codec" },
BITRATE: { value: "bitrate" },
LAST_CHECK_OK: { value: "lastcheckok" },
LAST_CHECK_TIME: { value: "lastchecktime" },
CLICK_TIMESTAMP: { value: "clicktimestamp" },
CLICK_COUNT: { value: "clickcount" }
}
});

const StationType = new GraphQLObjectType({
name: "Station",
description: "Station object",
Expand Down Expand Up @@ -52,6 +74,29 @@ const StationType = new GraphQLObjectType({
})
});

const QueryParams = new GraphQLInputObjectType({
name: "QueryParams",
description: "Query parameters",
fields: {
tag: { type: GraphQLString },
name: { type: GraphQLString },
country: { type: GraphQLString },
state: { type: GraphQLString },
language: { type: GraphQLString }
}
});

const PaginationParams = new GraphQLInputObjectType({
name: "PaginationParams",
description: "Pagination parameters",
fields: {
order: { type: OrderType },
limit: { type: GraphQLInt },
offset: { type: GraphQLInt },
reverse: { type: GraphQLBoolean }
}
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: "Query",
Expand All @@ -62,14 +107,15 @@ const schema = new GraphQLSchema({
args: {
id: { type: new GraphQLNonNull(GraphQLInt) }
},
resolve: async (root, { id }) => get(id)
resolve: (root, { id }) => get(id)
},
stations: {
type: new GraphQLList(StationType),
args: {
query: { type: new GraphQLNonNull(GraphQLString) }
query: { type: QueryParams },
pagination: { type: PaginationParams }
},
resolve: async (root, { query }) => search(query)
resolve: (root, query) => search(query)
}
})
})
Expand Down

1 comment on commit d93bef5

@vercel
Copy link

@vercel vercel bot commented on d93bef5 Apr 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.