Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphql language #4

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'transform-exponentiation-operator',
// Other
'transform-runtime',
'babel-plugin-inline-import'
],
presets: [
'es2015-node5',
Expand Down
25 changes: 15 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,24 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "babel-node ./src/index.js"
"start": "babel-node ./src/index.js",
"dev": "nodemon --exec 'npm start' -e js,jade,graphql,ts,json"
},
"author": "Reindex Software",
"license": "MIT",
"dependencies": {
"base64-url": "^1.3.2",
"bluebird": "^3.5.0",
"express": "^4.14.0",
"express-graphql": "^0.6.3",
"graphql": "^0.9.1",
"graphql-tools": "^0.10.1",
"mongodb": "^2.2.4"
},
"devDependencies": {
"babel-cli": "^6.11.4",
"babel-eslint": "^7.1.1",
"babel-plugin-inline-import": "^2.0.4",
"babel-plugin-syntax-trailing-function-commas": "^6.8.0",
"babel-plugin-transform-async-functions": "^6.8.0",
"babel-plugin-transform-async-to-module-method": "^6.8.0",
Expand All @@ -19,15 +31,8 @@
"babel-plugin-transform-object-rest-spread": "^6.8.0",
"babel-plugin-transform-runtime": "^6.12.0",
"babel-preset-es2015-node5": "^1.2.0",
"base64-url": "^1.3.2",
"express": "^4.14.0",
"express-graphql": "^0.5.3",
"graphql": "^0.6.2",
"mongodb": "^2.2.4"
},
"devDependencies": {
"babel-eslint": "^6.1.2",
"eslint": "^3.1.1",
"eslint-plugin-babel": "^3.3.0"
"eslint-plugin-babel": "^4.1.1",
"nodemon": "^1.11.0"
}
}
8 changes: 5 additions & 3 deletions src/Article.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ export async function getArticles(mongodb, {
}

function limitQueryWithId(query, before, after, order) {
const filter = {
_id: {},
};
const filter = {};

if (before || after) {
filter._id = {};
}

if (before) {
const op = order === 1 ? '$lt' : '$gt';
Expand Down
8 changes: 5 additions & 3 deletions src/Cursor.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ export function fromCursor(string) {
}
}

const CursorType = new GraphQLScalarType({
export const CursorTypeDef = `
scalar Cursor
`;

export const Cursor = new GraphQLScalarType({
name: 'Cursor',
serialize(value) {
if (value.value) {
Expand All @@ -35,5 +39,3 @@ const CursorType = new GraphQLScalarType({
return fromCursor(value);
},
});

export default CursorType;
149 changes: 35 additions & 114 deletions src/Schema.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,46 @@
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLID,
GraphQLString,
GraphQLNonNull,
GraphQLList,
GraphQLBoolean,
GraphQLInt,
} from 'graphql';
import Cursor from './Cursor';
import { makeExecutableSchema } from 'graphql-tools';
import { CursorTypeDef, Cursor } from './Cursor';
import { getArticles } from './Article';
import paginatorSchema from './schema.graphql';

export const PageInfo = new GraphQLObjectType({
name: 'PageInfo',
fields: {
hasNextPage: {
type: new GraphQLNonNull(GraphQLBoolean),
},
hasPreviousPage: {
type: new GraphQLNonNull(GraphQLBoolean),
const resolvers = {
Article: {
id({ _id }) {
return _id.toString();
},
},
});

export function createConnectionArguments() {
return {
first: {
type: GraphQLInt,
},
last: {
type: GraphQLInt,
},
before: {
type: Cursor,
},
after: {
type: Cursor,
},
};
}

const Article = new GraphQLObjectType({
name: 'Article',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLID),
resolve(parent) {
return parent._id.toString();
},
},
text: {
type: new GraphQLNonNull(GraphQLString),
},
}),
});

const ArticleConnection = new GraphQLObjectType({
name: 'ArticleConnection',
fields: () => ({
edges: {
type: new GraphQLList(ArticleEdge),
resolve(parent) {
return parent.query.toArray();
},
ArticleConnection: {
edges({ query }) {
return query.toArray();
},
pageInfo: {
type: new GraphQLNonNull(PageInfo),
},
}),
});

const ArticleEdge = new GraphQLObjectType({
name: 'ArticleEdge',
fields: () => ({
cursor: {
type: Cursor,
resolve(parent) {
return {
value: parent._id.toString(),
};
},
},
ArticleEdge: {
cursor({ _id }) {
return {
value: _id.toString(),
};
},
node: {
type: Article,
resolve(parent) {
return parent;
},
node(parent) {
return parent;
},
}),
});

const Viewer = new GraphQLObjectType({
name: 'Viewer',
fields: () => ({
id: {
type: new GraphQLNonNull(GraphQLID),
},
Viewer: {
allArticles(parent, { sortBy, order, ...args }, { mongodb }) {
const orderNum = order === 'ASC' ? 1 : -1;
return getArticles(mongodb, args, sortBy, orderNum);
},
allArticles: {
type: ArticleConnection,
args: createConnectionArguments(),
resolve(parent, args, { mongodb }) {
return getArticles(mongodb, args, 'text', -1);
},
},
Query: {
viewer() {
return {
id: 'VIEWER_ID',
};
},
}),
});
},
Cursor,
};

const Schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
viewer: {
type: Viewer,
resolve() {
return {
id: 'VIEWER_ID',
};
},
},
},
}),
export default makeExecutableSchema({
typeDefs: [paginatorSchema, CursorTypeDef],
resolvers,
});

export default Schema;
40 changes: 40 additions & 0 deletions src/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
type PageInfo {
hasNextPage: Boolean!
hasPreviousPage: Boolean!
}

type Article {
id: ID!
text: String!
}

type ArticleConnection {
edges: [ArticleEdge]
pageInfo: PageInfo!
}

type ArticleEdge {
cursor: Cursor
node: Article
}

enum sortOrder {
ASC
DESC
}

type Viewer {
id: ID!
allArticles (
sortBy: String = "text",
order: sortOrder = "ASC",
first: Int
last: Int
before: Cursor
after: Cursor
): ArticleConnection
}

type Query {
viewer: Viewer
}