n1ru4l / envelop

Envelop is a lightweight library allowing developers to easily develop, share, collaborate and extend their GraphQL execution layer. Envelop is the missing GraphQL plugin system.
https://envelop.dev
MIT License
769 stars 117 forks source link

Apollo client support (community contribution) #2256

Open vladinator1000 opened 2 weeks ago

vladinator1000 commented 2 weeks ago

Hi there, I have a server-side rendering use-case and I wrote a thing for it. It lets you use Envelop with Apollo client.

Example

import * as graphqlJs from 'graphql'
import { ApolloClient, gql, InMemoryCache } from '@apollo/client'
import { EnvelopSchemaLink } from '@envelop/apollo-client'
import { envelop, useEngine, useSchema } from '@envelop/core'
import { makeExecutableSchema } from '@graphql-tools/schema'

let schema = makeExecutableSchema({
  typeDefs: `type Query { hello: String! }`,
  resolvers: {
    Query: {
      hello: () => 'world'
    }
  }
})

// Use any enveloped setup
let getEnveloped = envelop({
  plugins: [useEngine(graphqlJs), useSchema(schema)]
})
let envelope = getEnveloped()

let apollo = new ApolloClient({
  cache: new InMemoryCache(),
  // Pass it to EnvelopSchemaLink
  link: new EnvelopSchemaLink(envelope)
})

// Use Apollo
let result = await apollo.query({
  query: gql`
    query {
      hello
    }
  `
})

console.log(result)

Contribution

Here's the diff, would you accept a PR similar to this? https://github.com/n1ru4l/envelop/compare/main...vladinator1000:envelop:apollo-schema-link?expand=1

EmrysMyrddin commented 2 weeks ago

Wow that looks awesome ! A PR would be very welcomed !

vladinator1000 commented 2 weeks ago

@EmrysMyrddin 🚀 https://github.com/n1ru4l/envelop/pull/2257