0.1.21 • Published 6 years ago

graphql-remote v0.1.21

Weekly downloads
7
License
MIT
Repository
github
Last release
6 years ago

graphql-remote

A toolbelt for creating remote GraphQL schemas with built-in subscriptions and dataloader support.

Install

yarn add graphql-remote

Usage

API

import { GraphQLServer } from 'graphql-yoga'
import { fetchTypeDefs, Remote, collectTypeDefs, GraphcoolLink } from 'graphql-remote'
import * as jwt from 'jsonwebtoken'

async function run() {

  const makeLink = () => new GraphcoolLink(process.env.GRAPHCOOL_SERVICE_ID, process.env.GRAPHCOOL_TOKEN)

  const remoteTypeDefs = await fetchTypeDefs(makeLink())

  const typeDefs = collectTypeDefs(remoteTypeDefs, `
    type Query {
      me: User
    }
    type Subscription {
      Post: PostSubscriptionPayload
    }
    type Post {
      id: ID!
      title: String!
      secret: Boolean
      extra: String
      allPosts: [Post!]!
      comments(filter: CommentFilter): [Comment!]
    }
    
    input CommentFilter {
      text_not: String
    }
  `)

  const resolvers = {
    Query: {
      me: (parent, args, ctx, info) => {
        const token = ctx.request.get('Authorization').replace('Bearer ', '')
        const { userId } = jwt.verify(token, process.env.JWT_SECRET!) as {
          userId: string
        }
        return ctx.remote.delegateQuery('User', { id: userId }, {}, info)
      },
    },
    Post: {
      title: (parent) =>  {
        return parent.title + ' - Post Title'
      },
      extra: () => 'extra field',
      allPosts: (_, _2, ctx, info) => {
        return ctx.remote.delegateQuery('allPosts', {}, {}, info)
      }
    },
    Subscription: {
      Post: {
        subscribe: async (parent, args, ctx, info) => {
          return ctx.remote.delegateSubscription('Post', args, ctx, info)
        },
      },
    },
  }

  const server = new GraphQLServer({
    typeDefs,
    resolvers,
    context: params => ({ ...params, remote: new Remote(makeLink()) }),
  })

  server.start().then(() => console.log('Server is running on :4000'))
}

run()

Credits

graphql-remote is an extension of the awesome graphql-tools

0.1.21

6 years ago

0.1.20

6 years ago

0.1.19

6 years ago

0.1.18

6 years ago

0.1.17

6 years ago

0.1.16

6 years ago

0.1.15

6 years ago

0.1.14

6 years ago

0.1.13

6 years ago

0.1.12

6 years ago

0.1.11

6 years ago

0.1.10

6 years ago

0.1.9

6 years ago

0.1.8

6 years ago

0.1.5-alpha.1

6 years ago

0.1.7

6 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago

0.0.1-demo3

6 years ago

0.0.1-demo2

6 years ago

0.0.1-demo1

6 years ago

0.0.0

6 years ago