1.1.0 • Published 1 year ago

mercurius-fetch v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

mercurius-fetch

Mercurius fetch is Plugin for adds fetch to a rest api directly on query or properties of query.

Undici fetch is being used for requests to rest apis.

Define the fetch directive in the queries or in properties of the query to consume apis without using a resolver

Requiriment

Use nodejs >= 16.x

Install

npm i fastify mercurius mercurius-fetch

or

yarn add fastify mercurius mercurius-fetch

Quick Start

const Fastify = require('fastify')
const mercurius = require('mercurius')
const mercuriusFetch = require('mercurius-fetch')

const app = Fastify({
  logger: true,
})

const schema = `
  directive @fetch(
    url: String!
    extractFromResponse: String
  ) on OBJECT | FIELD_DEFINITION

  type Response {
    id: Int
    code: String
    name: String
  }

  type Query {
    info: [Response] @fetch(url:"http://localhost:3000/info", extractFromResponse:"data")
  }`

app.register(mercurius, {
  schema,
})

app.get('/info', async function (request, reply) {
  return { data: [{ id: 1, code: 'code', name: 'name' }] }
})

app.register(mercuriusFetch)

app.listen(3000)

Mutations

const Fastify = require('fastify')
const mercurius = require('mercurius')
const mercuriusFetch = require('mercurius-fetch')

const app = Fastify({
  logger: true,
})

const schema = `
  directive @mutate(
      url: String!
      extractFromResponse: String
      method: String
  ) on OBJECT | FIELD_DEFINITION

  type Response {
    id: Int
    code: String
    name: String
  }

  type Mutation {
    addInfo(user: String, date: String): Response @mutate(url:"http://localhost:3000/info", extractFromResponse:"data", method:"POST")
  }`

app.register(mercurius, {
  schema,
})

app.post('/info', async function (request, reply) {
  return { data: { id: 2, code: request.body.code, name: request.body.name } }
})

app.register(mercuriusFetch)

app.listen(3000)

License

MIT

1.1.0

1 year ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago