Graphql-shield: TypeError: definitions.trim is not a function

Created on 17 May 2019  路  11Comments  路  Source: maticzav/graphql-shield

Bug report

when running against the latest release (5.3.5) against the latest release of graphql(1.17.4) yoga. I receive the following error.

If I rollback yoga to (1.17.1) the error disappears. Seems to be caused by yoga upgrading their version of graphql-middleware from 3.0.1 to 3.0.2.

TypeError: definitions.trim is not a function
    at parseFragmentToInlineFragment (/Users/rorykelly/Desktop/Rockspin/CuratedServer/node_modules/graphql-middleware/src/fragments.ts:85:21)
    at /Users/rorykelly/Desktop/Rockspin/CuratedServer/node_modules/graphql-middleware/src/fragments.ts:41:24
    at Array.map (<anonymous>)
    at Object.extractFragmentReplacements (/Users/rorykelly/Desktop/Rockspin/CuratedServer/node_modules/graphql-middleware/src/fragments.ts:40:6)
    at addMiddlewareToSchema (/Users/rorykelly/Desktop/Rockspin/CuratedServer/node_modules/graphql-middleware/src/middleware.ts:40:32)
    at normalisedMiddlewares.reduceRight.schema (/Users/rorykelly/Desktop/Rockspin/CuratedServer/node_modules/graphql-middleware/src/middleware.ts:86:13)
    at Array.reduceRight (<anonymous>)
    at applyMiddlewareWithOptions (/Users/rorykelly/Desktop/Rockspin/CuratedServer/node_modules/graphql-middleware/src/middleware.ts:78:77)
    at applyMiddleware (/Users/rorykelly/Desktop/Rockspin/CuratedServer/node_modules/graphql-middleware/src/middleware.ts:129:36)
    at new GraphQLServer (/Users/rorykelly/Desktop/Rockspin/CuratedServer/node_modules/graphql-yoga/src/index.ts:137:13)

To Reproduce

Expected behavior

Yoga Server should start.

Actual behaviour

Receive above error.

kinbug

Most helpful comment

As suggested by @RoryKelly the issue is caused by graphql-middleware update to 3.0.2, a dependency of graphql-yoga 1.17.2+

It can be corrected by requiring graphql-middleware 3.0.1 as follows.
_Only works with yarn, npm install does not process resolutions_

package.json

"resolutions": {
  "graphql-middleware": "3.0.1"
}

See graphql-middleware #198

graphql-yoga latest (1.18.1) appears to work as expected with graphql-midleware 3.0.1

All 11 comments

I think this happens due to GraphQL version discrepancy between graphql-middleware and graphql-shield, I will take a look at it, but I believe I cannot do much on the graphql-shield side to fix it.

Thanks for reporting the issue though :slightly_smiling_face:

I'm using yarn with resolutions so I should be using the same version of graphql.

"resolutions": {
    "graphql": "14.3.0",
    "jest": "24.7.1"
  }

I'll see If I can slim my project down and make a demo repo. It's the monorepo for https://curated.app so it might take a while :(

@RoryKelly let me see what I can do, I'll check with graphql-yoga to see what has changed.

馃憢 could you verify that the issue is still relevant?

Yup, I got some clues.

https://github.com/maticzav/graphql-shield/blob/master/src/generator.ts#L91

fragments: rule.extractFragments()

In the normal case, rule.extractFragments() return [].

But for some unknown reason, there is a key flatten inside.

for (const fragmentKey in rule.extractFragments()) {
    console.log(fragmentKey) // return `flatten`
}

Then, The flatten key will be inserted in graphql-middleware function. Its type does not matched and lead to a bug.

This is my workaround.

const allow = rule()(() => true)
export const permissions = shield(obj, { fallbackRule: allow })

Indeed, I cannot confirm the root cause come from graphql-shield. But this happen to me when try to upgrade to prisma2 : )

@Cerberus : The workaround works only when you apply a single rule. If you combine rules like using and, we see the error happens again.

So glad that the issue is being worked on, hope to see this to be released soon so graphql-shield can be used with prisma2

Could someone compose a simple reproduction repository? This way, I can identify the issue more quickly.

Could someone compose a simple reproduction repository? This way, I can identify the issue more quickly.

https://github.com/prisma/photonjs/tree/master/examples/typescript/graphql-auth

As suggested by @RoryKelly the issue is caused by graphql-middleware update to 3.0.2, a dependency of graphql-yoga 1.17.2+

It can be corrected by requiring graphql-middleware 3.0.1 as follows.
_Only works with yarn, npm install does not process resolutions_

package.json

"resolutions": {
  "graphql-middleware": "3.0.1"
}

See graphql-middleware #198

graphql-yoga latest (1.18.1) appears to work as expected with graphql-midleware 3.0.1

This bug has been fixed though graphql-yoga 1.18.2.

This issue may be closed.

Was this page helpful?
0 / 5 - 0 ratings