Skip to content

Instantly share code, notes, and snippets.

@timsuchanek
Created November 16, 2017 15:08
Show Gist options
  • Save timsuchanek/82640cdecd52f7ee699ccbadd36decdc to your computer and use it in GitHub Desktop.
Save timsuchanek/82640cdecd52f7ee699ccbadd36decdc to your computer and use it in GitHub Desktop.
import { GraphQLServer } from 'graphql-yoga'
import { createRemoteSchema, Delegate, collectTypeDefs, RemoteLink, RemoteSubscriptionsLink, giveMeASchemaFactoryNow, fetchSchema } from 'graphql-remote'
import * as jwt from 'jsonwebtoken'
import { GraphQLSchema } from 'graphql'
import { makeRemoteSchema } from '../src/createRemoteSchema/createRemoteSchema'
async function run() {
const makeLink = () => new HybridLink({
http: {
uri: 'https://swapi.graph.cool',
options: {
headers: {},
},
},
ws: {
uri: 'wss://swapi.graph.cool',
options: {
reconnect: true,
params: {},
},
},
})
const graphcoolSchema = await fetchSchema(makeLink())
const typeDefs = collectTypeDefs(graphcoolSchema)`
type Query {
me: User
}
type Subscription {
Post: PostSubscriptionPayload
}
`
const resolvers = {
Query: {
me: (parent, args, ctx, info) => {
const token = ctx.req.get('Authorization').replace('Bearer ', '')
const { userId } = jwt.verify(token, process.env.JWT_SECRET!) as {
userId: string
}
return ctx.delegate.query('User', { id: userId }, {}, info)
},
},
Subscription: {
Post: {
subscribe: (parent, args, ctx, info) => {
return ctx.delegate.subscription('Post', args, ctx, info)
},
},
},
}
const server = new GraphQLServer({
typeDefs,
resolvers,
context: req => ({ req, delegate: new Delegate({schema: graphcoolSchema, link: makeLink()})}),
})
server.start().then(() => console.log('Server is running on :3000'))
}
run()
//
//
// const fetcher = {
// wsLink: new WSLink('wss://swapi.graph.cool', {
// reconnect: true,
// params: {},
// }),
// httpLink: () => new BatchedHttpLink('https://swapi.graph.cool', {
// headers: {},
// }),
// }
//
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment