From 33046082b265c25400edbcbf962a44e708a8d9a8 Mon Sep 17 00:00:00 2001 From: Clay Allsopp Date: Thu, 10 Mar 2016 17:20:02 -0800 Subject: [PATCH 1/2] better public API --- graphqlhub-schemas/README.md | 11 ++++--- graphqlhub-schemas/example/index.js | 2 +- graphqlhub-schemas/src/github.js | 9 +----- graphqlhub-schemas/src/graphqlhub.js | 44 ++++++++++++++++------------ graphqlhub-schemas/src/hn.js | 9 +----- graphqlhub-schemas/src/index.js | 21 +++++++++---- graphqlhub-schemas/src/keyvalue.js | 11 ++----- graphqlhub-schemas/src/reddit.js | 9 +----- graphqlhub-schemas/src/twitter.js | 9 +----- src/server.js | 9 +++++- 10 files changed, 63 insertions(+), 71 deletions(-) diff --git a/graphqlhub-schemas/README.md b/graphqlhub-schemas/README.md index 98a25d4..444c15e 100644 --- a/graphqlhub-schemas/README.md +++ b/graphqlhub-schemas/README.md @@ -1,5 +1,9 @@ # graphqlhub-schemas +A collection of GraphQL schemas for existing HTTP APIs. + +## Usage + ``` $ npm i graphqlhub-schemas --save @@ -7,7 +11,7 @@ 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 } } '; @@ -23,9 +27,8 @@ Each schema file exports an object that looks like: ``` import { 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. diff --git a/graphqlhub-schemas/example/index.js b/graphqlhub-schemas/example/index.js index 46e6524..2433f6f 100644 --- a/graphqlhub-schemas/example/index.js +++ b/graphqlhub-schemas/example/index.js @@ -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 } }'; diff --git a/graphqlhub-schemas/src/github.js b/graphqlhub-schemas/src/github.js index d70ca84..931d0d7 100644 --- a/graphqlhub-schemas/src/github.js +++ b/graphqlhub-schemas/src/github.js @@ -186,11 +186,4 @@ let githubType = new GraphQLObjectType({ } }); -export const Schema = { - query : { - type : githubType, - resolve() { - return {}; - } - } -}; +export const QueryObjectType = githubType; diff --git a/graphqlhub-schemas/src/graphqlhub.js b/graphqlhub-schemas/src/graphqlhub.js index 5961319..e53d45b 100644 --- a/graphqlhub-schemas/src/graphqlhub.js +++ b/graphqlhub-schemas/src/graphqlhub.js @@ -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, @@ -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; diff --git a/graphqlhub-schemas/src/hn.js b/graphqlhub-schemas/src/hn.js index 86d2e22..75b3627 100644 --- a/graphqlhub-schemas/src/hn.js +++ b/graphqlhub-schemas/src/hn.js @@ -292,11 +292,4 @@ let hnType = new GraphQLObjectType({ } }) -export const Schema = { - query: { - type : hnType, - resolve() { - return {}; - }, - }, -}; +export const QueryObjectType = hnType; diff --git a/graphqlhub-schemas/src/index.js b/graphqlhub-schemas/src/index.js index 3f086f0..7e34ec3 100644 --- a/graphqlhub-schemas/src/index.js +++ b/graphqlhub-schemas/src/index.js @@ -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, +}; diff --git a/graphqlhub-schemas/src/keyvalue.js b/graphqlhub-schemas/src/keyvalue.js index 4c08d5a..d00c979 100644 --- a/graphqlhub-schemas/src/keyvalue.js +++ b/graphqlhub-schemas/src/keyvalue.js @@ -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; diff --git a/graphqlhub-schemas/src/reddit.js b/graphqlhub-schemas/src/reddit.js index 9c9fe46..9995f61 100644 --- a/graphqlhub-schemas/src/reddit.js +++ b/graphqlhub-schemas/src/reddit.js @@ -350,11 +350,4 @@ let redditType = new GraphQLObjectType({ } }); -export const Schema = { - query: { - type : redditType, - resolve() { - return {}; - } - }, -}; +export const QueryObjectType = redditType; diff --git a/graphqlhub-schemas/src/twitter.js b/graphqlhub-schemas/src/twitter.js index 8c4fb14..81db345 100644 --- a/graphqlhub-schemas/src/twitter.js +++ b/graphqlhub-schemas/src/twitter.js @@ -176,11 +176,4 @@ let twitterType = new GraphQLObjectType({ } }); -export const Schema = { - query: { - type: twitterType, - resolve() { - return {}; - } - } -}; +export const QueryObjectType = twitterType; diff --git a/src/server.js b/src/server.js index b94418e..cc60513 100644 --- a/src/server.js +++ b/src/server.js @@ -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'; From a41ef629ffab8aea06076cfac52220fdcf0a4c6b Mon Sep 17 00:00:00 2001 From: Clay Allsopp Date: Thu, 10 Mar 2016 17:20:54 -0800 Subject: [PATCH 2/2] version bump --- graphqlhub-schemas/package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphqlhub-schemas/package.json b/graphqlhub-schemas/package.json index 6963b87..7109b77 100644 --- a/graphqlhub-schemas/package.json +++ b/graphqlhub-schemas/package.json @@ -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": [