1.13.3 • Published 2 years ago

@hydre/graphql-http v1.13.3

Weekly downloads
1
License
UNLICENSE
Repository
github
Last release
2 years ago

Install

npm i @hydre/graphql-http

It is recommended to use @hydre/make_schema too,

npm i @hydre/make_schema

Usage

This is an example with Koa but using others is pretty much the same. Import them by path

import graphqlHTTP from '@hydre/graphql-http/koa'
import graphqlHTTP from '@hydre/graphql-http/lambda'
import graphqlHTTP from '@hydre/graphql-http/tinyhttp'
import graphqlHTTP from '@hydre/graphql-http/fastify'
import Koa from 'koa'
import { readFileSync } from 'fs'
import bodyParser from 'koa-bodyparser'
import { readFile } from 'fs/promises'
import graphql from 'graphql'
import graphqlHTTP from '@hydre/graphql-http/koa'
import makeSchema from '@hydre/make_schema'

const { buildSchema } = graphql
const schema = buildSchema(readFileSync('/path/to/schema.gql', 'utf-8'))
const app = new Koa().use(bodyParser()).use(
  graphqlHTTP({
    schema: make_schema({
      document: await readFile('test/schema.gql', 'utf8'),
      resolvers: {
        Query: {
          hello({ name }) {
            return `Hello ${name} !`
          },
          me() {
            return { friend: 'Bob', name: 'Alice' }
          },
          animal() {
            return { speak: () => 'jajaja', __typename: 'Cat' }
          },
        },
        User: {
          sayHello({ friend }, { to }) {
            return `Hello ${to}, I'm ${friend}'s friend`
          },
        },
        // as we don't define the Spanish, by default it will say 'jajaja'
        Cat: {
          speak() {
            return 'miaou'
          },
        },
        Subscription: {
          async *onMessage() {
            while (true) {
              await setTimeout(1000)
              yield 'Hello'
            }
          },
        },
      },
    }),
    // rootValue is what comes as a first argument in your resolvers, it is an object resolved by default
    // a bit like how Promise.resolve() works, meaning that your subsequent resolvers won't be called
    // if something is already present in this rootValue.
    // This is useful for the first layer of calls and can contains functions but it can become confusing
    // and is not made for nested layers like { user { language: string, speak: function } } as `speak()` won't contains previously resolved
    // fields of the user object so won't know about the language for example.
    // If you're not advanced in Graphql, you should stick to the built schema only
    rootValue: {},
    buildContext: async ctx => {},
    formatError: error => error,
  })
)

app.listen(3000)
1.13.3

2 years ago

1.13.2

2 years ago

1.13.1

2 years ago

1.13.0

2 years ago

1.12.0

2 years ago

1.11.0

2 years ago

1.10.1

2 years ago

1.10.0

2 years ago

1.9.1

3 years ago

1.9.0

3 years ago

1.9.3

3 years ago

1.9.2

3 years ago

1.8.0

3 years ago

1.7.0

4 years ago

1.6.2

4 years ago

1.6.1

4 years ago

1.6.0

4 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.3

5 years ago

1.4.2

5 years ago

1.4.1

5 years ago

1.3.0

5 years ago

1.2.3

5 years ago

1.1.0

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.0.13

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago