# Unknown Directive in Netlify Production using GRANDstack

**URL:** <https://community.neo4j.com/t/unknown-directive-in-netlify-production-using-grandstack/37666>\
**Category:** GraphQL & GRANDstack\
**Tags:** auth, graphql\
**Created:** [May 1, 2021, 1:12am UTC](https://community.neo4j.com/t/unknown-directive-in-netlify-production-using-grandstack/37666 "2021-05-01T01:12:05Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jaedag](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaedag/32/12923_2.png) [@jaedag](https://community.neo4j.com/u/jaedag)\
**Post date:** [May 1, 2021, 1:12am UTC](https://community.neo4j.com/t/unknown-directive-in-netlify-production-using-grandstack/37666/1 "2021-05-01T01:12:05Z")

</div>

Hi, has anyone encountered the following  
`errorMessage: "Unknown directive "isAuthenticated".↵↵Unknown directive "isAuthenticated"."`

Everything works fine in development. It happens in production when serving through netlify functions.

```auto
const schema = makeAugmentedSchema({
  typeDefs,
  resolvers,
  config: {
    query: true,
    mutation: true,
    auth: {
      isAuthenticated: true,
      hasScope: true,
    },
  },
})

```

This is the code from my index.js file.

UPDATE:  
Here is the code from my functions/graphql/graphql.js file

```auto
`// This module can be used to serve the GraphQL endpoint
// as a lambda function

const { ApolloServer } = require('apollo-server-lambda')
const { makeAugmentedSchema } = require('neo4j-graphql-js')
const neo4j = require('neo4j-driver')

// This module is copied during the build step
// Be sure to run `npm run build`
const { typeDefs } = require('./graphql-schema')

const driver = neo4j.driver(
  process.env.NEO4J_URI || 'bolt://localhost:7687',
  neo4j.auth.basic(
    process.env.NEO4J_USER || 'neo4j',
    process.env.NEO4J_PASSWORD || 'neo4j'
  ),
  {
    encrypted: process.env.NEO4J_ENCRYPTED ? 'ENCRYPTION_ON' : 'ENCRYPTION_OFF',
  }
)

const server = new ApolloServer({
  schema: makeAugmentedSchema({
    typeDefs,
    config: {
      query: true,
      mutation: true,
      auth: {
        isAuthenticated: true,
        hasScope: true,
      },
    },
  }),
  context: ({ req }) => {
    return { req, driver, neo4jDatabase: process.env.NEO4J_DATABASE }
  },
})

exports.handler = server.createHandler()
`

```

After including the config object in makeAugmentedSchema, the error has now changed to say `no authorisation token`

I'm not sure if there's anything else I should be doing. Everything works fine in development.

---

<div class="post-metadata">

**Author:** ![lyonwj](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/lyonwj/32/27358_2.png) [@lyonwj](https://community.neo4j.com/u/lyonwj)\
**Post date:** [May 3, 2021, 5:56pm UTC](https://community.neo4j.com/t/unknown-directive-in-netlify-production-using-grandstack/37666/2 "2021-05-03T17:56:39Z")

</div>

I think we chatted about this on Discord, but posting here as well for others:

Netlify/lambda function invocations have a different signature (an event object is passed instead of req), but the graphql-auth-directive package will be looking for a `req` object to find the authorization header. So I don't think the headers are being captured in your example. Try returning the event object under the `req` key in the context function:

```auto
const server = new ApolloServer({
  schema,
  context: ({ event }) => {
    return {
      driver,
      req: event,
    };
  },
  introspection: true,
  playground: true,
});

```

Here's a longer example if you also want to decode a JWT and add claims from the JWT to cypherParams (NOTE: the new official @neo4j/graphql library does this automatically)

```auto
const server = new ApolloServer({
  schema,
  context: async ({ event }) => {
    const token = event.headers?.authorization?.slice(7);
    let userId;

    if (!token) {
      return {
        driver,
      };
    }

    const authResult = new Promise((resolve, reject) => {
      jwt.verify(
        token,
        getPublicKey,
        {
          algorithms: ["RS256"],
        },
        (error, decoded) => {
          if (error) {
            reject({ error });
          }
          if (decoded) {
            resolve(decoded);
          }
        }
      );
    });

    const decoded = await authResult;

    return {
      driver,
      req: event,
      cypherParams: {
        userId: decoded.sub,
      },
    };
  },
  introspection: true,
  playground: true,
});

```

Note that this is using the old `neo4j-graphql-js`, you may want to switch to the newer official `@neo4j/graphql` library which has a [more powerful authorization model.](https://neo4j.com/docs/graphql-manual/current/auth/) The [GRANDstack starter project](https://github.com/grand-stack/grand-stack-starter) has been updated to use this library instead of neo4j-graphql-js.

---

<div class="post-metadata">

**Author:** ![jaedag](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaedag/32/12923_2.png) [@jaedag](https://community.neo4j.com/u/jaedag)\
**Post date:** [May 3, 2021, 9:05pm UTC](https://community.neo4j.com/t/unknown-directive-in-netlify-production-using-grandstack/37666/3 "2021-05-03T21:05:39Z")

</div>

> [@lyonwj](#):
>
> @neo4j/graphql

I was looking into making the switch to the new @neo4j/graphql library, but my code currently has a number of relationship properties in it. So I'm patiently waiting for support to come for relays and then I'll make the switch.

---

<div class="post-metadata">

**Author:** ![jaedag](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaedag/32/12923_2.png) [@jaedag](https://community.neo4j.com/u/jaedag)\
**Post date:** [May 3, 2021, 9:55pm UTC](https://community.neo4j.com/t/unknown-directive-in-netlify-production-using-grandstack/37666/4 "2021-05-03T21:55:01Z")

</div>

The first code snippet doesn't seem to work. I get the error `You are not authorised for this resource`

I tried the second snippet of code, but I'm not sure what the set up is for the variables `jwt.verify` as well as the `getPublicKey`.

---

<div class="post-metadata">

**Author:** ![jaedag](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaedag/32/12923_2.png) [@jaedag](https://community.neo4j.com/u/jaedag)\
**Post date:** [May 6, 2021, 1:29am UTC](https://community.neo4j.com/t/unknown-directive-in-netlify-production-using-grandstack/37666/5 "2021-05-06T01:29:57Z")

</div>

Hi Will I implemented your second solution as the first one wasn't working, I'm not sure what the issue is, but I'm currently getting an error that says `"Context creation failed: undefined"`

Here is what my graphql.js file currently looks like.

```auto
// This module can be used to serve the GraphQL endpoint
// as a lambda function

const { ApolloServer } = require('apollo-server-lambda')
const { makeAugmentedSchema, assertSchema } = require('neo4j-graphql-js')
const neo4j = require('neo4j-driver')
const jwt = require('jsonwebtoken')

// This module is copied during the build step
// Be sure to run `npm run build`
const { typeDefs } = require('./graphql-schema')

const driver = neo4j.driver(
  process.env.NEO4J_URI || 'bolt://localhost:7687',
  neo4j.auth.basic(
    process.env.NEO4J_USER || 'neo4j',
    process.env.NEO4J_PASSWORD || 'neo4j'
  ),
  {
    encrypted: process.env.NEO4J_ENCRYPTED ? 'ENCRYPTION_ON' : 'ENCRYPTION_OFF',
  }
)

const schema = makeAugmentedSchema({
  typeDefs,
  config: {
    query: true,
    mutation: true,
    auth: {
      isAuthenticated: true,
      hasScope: true,
    },
  },
})

assertSchema({ schema, driver, debug: true })

const server = new ApolloServer({
  schema: schema,
  context: async ({ event }) => {
    const token = event.headers?.authorization?.slice(7)
    // let userId

    if (!token) {
      return {
        driver,
      }
    }

    const authResult = new Promise((resolve, reject) => {
      jwt.verify(
        token,
        process.env.JWT_SECRET,
        {
          algorithms: ['RS256'],
        },
        (error, decoded) => {
          if (error) {
            reject({ error })
          }
          if (decoded) {
            resolve(decoded)
          }
        }
      )
    })

    const decoded = await authResult

    return {
      driver,
      req: event,
      cypherParams: {
        userId: decoded.sub,
      },
      neo4jDatabase: process.env.NEO4J_DATABASE,
    }
  },
  introspection: true,
  playground: true,
})

exports.handler = server.createHandler()

```

At this point I'm wondering if sharing a link to my github repo would help any? I really want to get this app deployed as I'm using it for a project at work.

---

<div class="post-metadata">

**Author:** ![luwembamugumya](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/luwembamugumya/32/18182_2.png) [@luwembamugumya](https://community.neo4j.com/u/luwembamugumya)\
**Post date:** [May 8, 2021, 10:53am UTC](https://community.neo4j.com/t/unknown-directive-in-netlify-production-using-grandstack/37666/6 "2021-05-08T10:53:29Z")

</div>

> [@jaedag](#):
>
> You are not authorised for this resource

I am facing the same issue in my netlify deployment "You are not authorised for this resource". I have a similar config.auth object in the schema variable. Please let me know if you managed to fix this.

---

<div class="post-metadata">

**Author:** ![jaedag](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaedag/32/12923_2.png) [@jaedag](https://community.neo4j.com/u/jaedag)\
**Post date:** [May 8, 2021, 12:59pm UTC](https://community.neo4j.com/t/unknown-directive-in-netlify-production-using-grandstack/37666/7 "2021-05-08T12:59:45Z")

</div>

Hi  
I've not been able to fix this, I'm actively looking for a solution to this as I'm using the `@hasScope` and `@isAuthenticated` directives in my project.

I shared my code with Will but this was his response on discord:

> I don't see anything in `functions/graphql/graphql.js` that jumps out. Based on that error it sounds like something is causing an error in the context function. I wonder if maybe something with the JWT verification? I would suggest implementing the same thing in `/api/src/index.js` and testing locally which will make it easier to troubleshoot without the lambda deployment / overhead.

As far as I can see, it seems to be the JWT verification that is causing the issue. I'm still searching for a solution and would be glad to share if I find it. You can also share a solution if you find one.

---

<div class="post-metadata">

**Author:** ![jaedag](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaedag/32/12923_2.png) [@jaedag](https://community.neo4j.com/u/jaedag)\
**Post date:** [May 9, 2021, 4:55am UTC](https://community.neo4j.com/t/unknown-directive-in-netlify-production-using-grandstack/37666/8 "2021-05-09T04:55:02Z")

</div>

I've been able to isolate the problem.

It seems that the event object is not getting returned in the context function.

```auto
const server = new ApolloServer({
  schema: schema,
  context: ({ event }) => {
    return {
      driver,
      req: event,
    }
  },
  introspection: true,
  playground: true,
})

```

I'm able to log the event object before the return statement but after that it just disappears after that. I'm guessing this is a problem with Apollo?

---

<div class="post-metadata">

**Author:** ![jaedag](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/jaedag/32/12923_2.png) [@jaedag](https://community.neo4j.com/u/jaedag)\
**Post date:** [May 31, 2021, 10:53am UTC](https://community.neo4j.com/t/unknown-directive-in-netlify-production-using-grandstack/37666/9 "2021-05-31T10:53:55Z")

</div>

Hi

Were you ever able to solve this problem? I would be interested to know the solution if you found it
