0.0.1 • Published 3 years ago

better-schema-link v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 years ago

better-schema-link

Size badge NPM

Apollo SchemaLink with context support. Useful for Next.js apps with universal Apollo GraphQL client that require context (for example field authorization).

Install

pnpm i better-schema-link @apollo/client graphql

Example

import { ApolloClient, InMemoryCache, ApolloLink, ServerError } from '@apollo/client'

const createIsomorphLink = () => {
  if (typeof window === 'undefined') {
    // Works on the server
    const { SchemaLink } = require('better-schema-link')
    const { schema } = require('../graphql/schema')
    const context = require('../graphql/context')
    return new SchemaLink({ schema, context })
  } else {
    // Works on the client
    const { HttpLink } = require('@apollo/client/link/http')

    return new HttpLink({
      uri: '/api/graphql',
      credentials: 'same-origin'
    })
  }
}

// Now your client works on both server and client!
export const createApolloClient = () =>
  new ApolloClient({
    ssrMode: typeof window === 'undefined',
    link: createIsomorphLink(),
    cache: new InMemoryCache()
  })