Skip to content

Commit

Permalink
Merge pull request #19 from clayallsopp/feature/public-api-changes
Browse files Browse the repository at this point in the history
better public API
  • Loading branch information
clayallsopp committed Mar 11, 2016
2 parents 60836a9 + a41ef62 commit 1caf6ac
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 72 deletions.
11 changes: 7 additions & 4 deletions graphqlhub-schemas/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
# graphqlhub-schemas

A collection of GraphQL schemas for existing HTTP APIs.

## Usage

```
$ npm i graphqlhub-schemas --save
import { Reddit } from 'graphqlhub-schemas';
import { GraphQLSchema, graphql } from 'graphql';
let schema = new GraphQLSchema({
query: Reddit.query.type
query: Reddit.QueryObjectType
});
let query = ' { user(username: "kn0thing") { username } } ';
Expand All @@ -23,9 +27,8 @@ Each schema file exports an object that looks like:
```
import { <Schema> as Schema } from 'graphqlhub-schemas';
let { query } = Schema;
let { type } = query;
// type is a GraphQLObjectType
let { QueryObjectType } = Schema;
// QueryObjectType is an instance of GraphQLObjectType
```

See [src/index.js](src/index.js) from available schemas.
2 changes: 1 addition & 1 deletion graphqlhub-schemas/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var GraphQLSchema = _graphql.GraphQLSchema;
var graphql = _graphql.graphql;

var schema = new GraphQLSchema({
query: Reddit.query.type
query: Reddit.QueryObjectType
});

var query = ' { user(username: "kn0thing") { username } }';
Expand Down
2 changes: 1 addition & 1 deletion graphqlhub-schemas/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "graphqlhub-schemas",
"repository": "clayallsopp/graphqlhub",
"version": "0.1.0-rc7",
"version": "0.1.0-rc8",
"description": "GraphQL Schemas for REST APIs like Github, Hacker News, Reddit, and Twitter",
"main": "lib/index.js",
"files": [
Expand Down
9 changes: 1 addition & 8 deletions graphqlhub-schemas/src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,4 @@ let githubType = new GraphQLObjectType({
}
});

export const Schema = {
query : {
type : githubType,
resolve() {
return {};
}
}
};
export const QueryObjectType = githubType;
44 changes: 26 additions & 18 deletions graphqlhub-schemas/src/graphqlhub.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
GraphQLString
} from 'graphql';

import { Schema as HN } from './hn';
import { Schema as REDDIT } from './reddit';
import { Schema as KEYVALUE } from './keyvalue';
import { Schema as GITHUB } from './github';
import { Schema as TWITTER } from './twitter';
import * as HN from './hn';
import * as REDDIT from './reddit';
import * as KEYVALUE from './keyvalue';
import * as GITHUB from './github';
import * as TWITTER from './twitter';

let schemas = {
hn : HN,
Expand All @@ -31,25 +31,33 @@ let FIELDS = {
let MUTATION_FIELDS = {};

Object.keys(schemas).forEach((schemaName) => {
let { mutations } = schemas[schemaName];
let { Mutations } = schemas[schemaName];
let mutations = Mutations;
if (mutations) {
Object.keys(mutations).forEach((mutationName) => {
let fixedName = `${schemaName}_${mutationName}`;
MUTATION_FIELDS[fixedName] = mutations[mutationName];
});
}
FIELDS[schemaName] = schemas[schemaName].query;
FIELDS[schemaName] = {
type : schemas[schemaName].QueryObjectType,
resolve() {
return {};
}
};
});

export let Schema = new GraphQLSchema({
query: new GraphQLObjectType({
name : 'GraphQLHubAPI',
description : 'APIs exposed as GraphQL',
fields : () => FIELDS,
}),
mutation: new GraphQLObjectType({
name : 'GraphQLHubMutationAPI',
description : 'APIs exposed as GraphQL mutations',
fields : () => MUTATION_FIELDS,
}),
let queryObjectType = new GraphQLObjectType({
name : 'GraphQLHubAPI',
description : 'APIs exposed as GraphQL',
fields : () => FIELDS,
});

let mutationsType = new GraphQLObjectType({
name : 'GraphQLHubMutationAPI',
description : 'APIs exposed as GraphQL mutations',
fields : () => MUTATION_FIELDS,
});

export const QueryObjectType = queryObjectType;
export const MutationsType = mutationsType;
9 changes: 1 addition & 8 deletions graphqlhub-schemas/src/hn.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,4 @@ let hnType = new GraphQLObjectType({
}
})

export const Schema = {
query: {
type : hnType,
resolve() {
return {};
},
},
};
export const QueryObjectType = hnType;
21 changes: 15 additions & 6 deletions graphqlhub-schemas/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
export { Schema as HackerNews } from './hn';
export { Schema as Reddit } from './reddit';
export { Schema as Twitter } from './twitter';
export { Schema as Github } from './github';
export { Schema as GraphQLHub } from './graphqlhub';
export { Schema as KeyValue } from './keyvalue';
import * as HackerNews from './hn';
import * as Reddit from './reddit';
import * as Twitter from './twitter';
import * as Github from './github';
import * as GraphQLHub from './graphqlhub';
import * as KeyValue from './keyvalue';

export {
HackerNews,
Reddit,
Twitter,
Github,
GraphQLHub,
KeyValue,
};
11 changes: 2 additions & 9 deletions graphqlhub-schemas/src/keyvalue.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,5 @@ let mutations = {
setValue: SetValueForKeyMutation
};

export const Schema = {
query : {
type : keyvalueType,
resolve() {
return {};
}
},
mutations
};
export const QueryObjectType = keyvalueType;
export const Mutations = mutations;
9 changes: 1 addition & 8 deletions graphqlhub-schemas/src/reddit.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,4 @@ let redditType = new GraphQLObjectType({
}
});

export const Schema = {
query: {
type : redditType,
resolve() {
return {};
}
},
};
export const QueryObjectType = redditType;
9 changes: 1 addition & 8 deletions graphqlhub-schemas/src/twitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,4 @@ let twitterType = new GraphQLObjectType({
}
});

export const Schema = {
query: {
type: twitterType,
resolve() {
return {};
}
}
};
export const QueryObjectType = twitterType;
9 changes: 8 additions & 1 deletion src/server.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import express from 'express';
import graphqlHTTP from 'express-graphql';
import { GraphQLSchema } from 'graphql';
import cors from 'cors';
import fs from 'fs';

import Handlebars from 'handlebars';

import { GraphQLHub as Schema } from 'graphqlhub-schemas';
import { GraphQLHub } from 'graphqlhub-schemas';
import instrumentationMiddleware from './graphQLInstrumentation';

let Schema = new GraphQLSchema({
query : GraphQLHub.QueryObjectType,
mutation : GraphQLHub.MutationsType,
});


import path from 'path';

import timingCallback from './timingCallback';
Expand Down

0 comments on commit 1caf6ac

Please sign in to comment.