Redwood: Custom GraphQL Directives not working

Created on 25 Mar 2020  路  8Comments  路  Source: redwoodjs/redwood

Directive

import { SchemaDirectiveVisitor } from 'graphql-tools'
import { defaultFieldResolver } from 'graphql'

export class AuthDirective extends SchemaDirectiveVisitor {
  visitFieldDefinition(type) {
    const { resolve = defaultFieldResolver } = type
    type.resolve = async function(...args) {
      const result = await resolve.apply(this, args)

      console.log('This log does not show!')

      return result
    }
  }
}

Handler

// api/src/functions/graphql.js

export const handler = createGraphQLHandler({
  schema: makeMergedSchema({
    schemas,
    services: makeServices({ services }),
  }),
  schemaDirectives: {
    auth: AuthDirective,
  },
})

Schema

type Post {
    author: String @auth
}

I haven't dug too far, but according to the types of createGraphQLHandler it should accept schemaDirectives

If this is unexpected I'm happy to dig further. If it's expected and needs work I'm happy to make a PR!

kinimprovement api

Most helpful comment

Sounds good, I'll give it a try!

All 8 comments

Unfortunately we're using mergeSchemas from graphql-tools which doesn't support directives. (Support was merged, but not released.)

I think Apollo may have opted for federation instead? 馃し鈥嶁檪

We might want to switch to an alternative:

We use mergeSchemas over here:
https://github.com/redwoodjs/redwood/blob/e842179762093b2589c9eedc2b7b123bdea30693/packages/api/src/makeMergedSchema/makeMergedSchema.js#L106-L122

Ah, that explains!

What are the considerations for switching?

@RobertBroersma This is actually a fairly decent first issue, here's the stitching documentation for graphql-tools-fork which would give us the most mileage without a ton (if any) code changes.

It offers a ton of background about Apollo's new federation approach, which was the impetus for creating the fork.

My gut feeling is that someone could switch out the graphql-tools imports in @redwoodjs/api with those of graphql-tools-fork and it'll "just work."

I think you could replace these two imports:
https://github.com/redwoodjs/redwood/blob/e842179762093b2589c9eedc2b7b123bdea30693/packages/api/src/makeMergedSchema/makeMergedSchema.js#L1

With the same that are available in graphql-tools-fork

Sounds good, I'll give it a try!

Thank you @RobertBroersma! If you want to test this you can follow the yarn link guidelines in CONTRIBUTING.md

Hi gents. Was this handled with #347 ?

Yep!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jeliasson picture jeliasson  路  3Comments

tmeasday picture tmeasday  路  4Comments

thedavidprice picture thedavidprice  路  3Comments

Tobbe picture Tobbe  路  4Comments

aldonline picture aldonline  路  3Comments