Skip to content

Instantly share code, notes, and snippets.

@frikille
Last active August 29, 2015 14:25

Revisions

  1. frikille revised this gist Jul 19, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions graphql-service.js
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@ import PostType from './PostType.js';
    import User from '../models/User.js';

    let QueryType = new GraphQLObjectType({
    name: 'Query',
    name: 'DataQuery',
    fields: () => ({
    post: {
    type: PostType,
    @@ -38,7 +38,7 @@ let QueryType = new GraphQLObjectType({
    });

    let SessionQueryType = new GraphQLObjectType({
    name: 'SessionQuery',
    name: 'Query',
    fields: () => ({
    data: {
    type: QueryType,
    @@ -77,6 +77,6 @@ export default {

    let loggedInUserId = (request.session) ? request.session.passport.user : null;

    return graphql(schema, query, 'SessionQuery', {loggedInUserId});
    return graphql(schema, query, 'Query', {loggedInUserId});
    }
    };
  2. frikille revised this gist Jul 19, 2015. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion graphql-service.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,10 @@
    import { graphql } from 'graphql';
    import {
    graphql,
    GraphQLObjectType,
    GraphQLInt,
    GraphQLString,
    GraphQLSchema
    } from 'graphql';
    import PostType from './PostType.js';
    import User from '../models/User.js';

  3. frikille revised this gist Jul 19, 2015. 1 changed file with 62 additions and 2 deletions.
    64 changes: 62 additions & 2 deletions graphql-service.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,65 @@
    import { graphql } from 'graphql';
    import schema from '../graphql/schema.js';
    import PostType from './PostType.js';
    import User from '../models/User.js';

    let QueryType = new GraphQLObjectType({
    name: 'Query',
    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: 'SessionQuery',
    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) {
    @@ -11,6 +71,6 @@ export default {

    let loggedInUserId = (request.session) ? request.session.passport.user : null;

    return graphql(schema, query, 'Query', {loggedInUserId});
    return graphql(schema, query, 'SessionQuery', {loggedInUserId});
    }
    };
  4. frikille created this gist Jul 19, 2015.
    16 changes: 16 additions & 0 deletions graphql-service.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    import { graphql } from 'graphql';
    import schema from '../graphql/schema.js';

    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});
    }
    };