0.0.2 • Published 1 year ago

rise-graphql-foundation v0.0.2

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

RISE GraphQL Foundation

Install

npm i rise-graphql-foundation

Usage

const { makeGraphQLApp } = require('rise-graphql-foundation')

const schema = /* GraphQL */ `
    type Author {
        id: Int!
        firstName: String
        lastName: String
    }

    type Post {
        id: Int!
        title: String
        votes: Int
    }

    type Query {
        posts: [Post]
        author(id: Int!): Author
    }

    type Mutation {
        upvotePost(postId: Int!): Post
    }
`

const resolvers = {
    Query: {
        posts: (_, __, context) => {
            return [
                {
                    id: 1,
                    authorId: 1,
                    title: 'Introduction to GraphQL',
                    votes: 2
                },
                { id: 2, authorId: 2, title: 'Welcome to Meteor', votes: 3 },
                { id: 3, authorId: 2, title: 'Advanced GraphQL', votes: 1 },
                { id: 4, authorId: 3, title: 'Launchpad is Cool', votes: 7 }
            ]
        },
        author: (_, { id }, context) => [
            { id: 1, firstName: 'Tom', lastName: 'Coleman' },
            { id: 2, firstName: 'Sashko', lastName: 'Stubailo' },
            { id: 3, firstName: 'Mikhail', lastName: 'Novikov' }
        ]
    },

    Mutation: {
        upvotePost(_, { postId }, context) {
            return {
                id: 1,
                authorId: 1,
                title: 'Introduction to GraphQL',
                votes: 2
            }
        }
    }
}

const graphqlApp = makeGraphQLApp({
    schema,
    resolvers
})

module.exports.handler = async () => {
    const query = '{ author }'
    const res = await graphqlApp.execute({
        input: query,
        context: { id: '123' }
    })

    return {
        statusCode: 200,
        data: res
    }
}