Skip to content

Instantly share code, notes, and snippets.

@frikille
Last active August 29, 2015 14:25
Show Gist options
  • Save frikille/9f36b50f1baef017dc0d to your computer and use it in GitHub Desktop.
Save frikille/9f36b50f1baef017dc0d to your computer and use it in GitHub Desktop.
import {
graphql,
GraphQLObjectType,
GraphQLInt,
GraphQLString,
GraphQLSchema
} from 'graphql';
import PostType from './PostType.js';
import User from '../models/User.js';
let QueryType = new GraphQLObjectType({
name: 'DataQuery',
fields: () => ({
post: {
type: PostType,
args: {
id: {
name: 'id',
type: GraphQLInt
},
journal: {
name: 'journal',
description: 'The journal slug',
type: GraphQLString
},
post: {
name: 'post',
description: 'The post slug',
type: GraphQLString
}
},
resolve: (user, {id, journal, post}) => {
return Post.authorise(user, {id, journal, post})
.then(post => post && post.toJSON());
}
}
})
});
let SessionQueryType = new GraphQLObjectType({
name: 'Query',
fields: () => ({
data: {
type: QueryType,
args: {
id: {
name: 'loggedInUserId',
type: GraphQLInt
}
},
resolve: (root, {loggedInUserId}) => {
return User.forge({id: loggedInUserId})
.fetch()
.then(user => {
if (user) {
return user.toJSON();
} else {
return {};
}
});
}
}
})
});
let schema = new GraphQLSchema({
query: SessionQueryType
});
export default {
query(body, request) {
let query = `query Query($loggedInUserId: Int) {
data(id: $loggedInUserId) {
${body}
}
}`;
let loggedInUserId = (request.session) ? request.session.passport.user : null;
return graphql(schema, query, 'Query', {loggedInUserId});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment