Graphql-tools: addSchemaLevelResolveFunction resolves only once

Created on 5 Jul 2017  ·  9Comments  ·  Source: ardatan/graphql-tools

Code:

addSchemaLevelResolveFunction(schema, () => {
  throw new Error('Can\'t resolve')
})

Expected:

I expect addSchemaLevelResolveFunction to throw error on every field request

Current:

addSchemaLevelResolveFunction throws on the first requested field, next are executed

Example:

Query:

{
  user(id: 3) {
    username
  }
}

// result 

{
  "data": {
    "user": null
  },
  "errors": [
    {
      "message": "Can't resolve",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "user"
      ]
    }
  ]
}

Another query:

{
  user(id: 3) {
    username
  }

  sameUser: user(id: 3) {
    username
  }
}

// result 

{
  "data": {
    "user": null,
    "sameUser": {
      "username": "two"
    }
  },
  "errors": [
    {
      "message": "Can't resolve",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "user"
      ]
    }
  ]
}

Packages:

  • graphql 0.10.3
  • graphql-server-koa 0.8.5
  • graphql-tools 1.1.0

Is it supposed to work this way?

Most helpful comment

@stubailo I was planning on using addSchemaLevelResolveFunction as a validation layer based on the recommendation found on https://stackoverflow.com/questions/53838475/apollo-server-2-validation-middleware . If its going to removed from future versions, where do you recommend I place my validation layer?

All 9 comments

hey @VladShcherbin this looks like a bug to me...
i think there was some change for the function for subscriptions that it should run only on first evaluation and not every time..
@helfer can you confirm this is a bug or not?

This seems like the expected behavior to me, addSchemaLevelResolveFunction is meant to run once per request.

Some operations, such as authentication, need to be done only once per query.

I think we should deprecate this function entirely, since its use case is best served by context.

Either way, there hasn't been any activity here for a while so going to close it.

@stubailo yeah, but it's still not solved and I guess it works the same way.

Why close before it's fixed in any way?

Because I think it's going to be better to remove the function in future versions and suggest other approaches that do work well.

Is there some use case you have that requires addSchemaLevelResolveFunction that can't be better solved via setting the context with a function?

@stubailo no, I solved it the other way since it wasn't fixed and had no activity. Just wanted to make sure this's solved somehow (fixed/removed/etc) or linked to track progress.

So, it has moved here: https://github.com/apollographql/graphql-tools/issues/887 😉🙌

@stubailo I was planning on using addSchemaLevelResolveFunction as a validation layer based on the recommendation found on https://stackoverflow.com/questions/53838475/apollo-server-2-validation-middleware . If its going to removed from future versions, where do you recommend I place my validation layer?

@stubailo Currently i use this for pre-validating the request which is pretty useful function , not sure why do you want to deprecate it.
As a workaround for the mentioned above i just delete the __runAtMostOnce key every time this function starts:

delete info.operation['__runAtMostOnce'];

otherwise where do you advice running the validation process ( a custom one ) ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

flippidippi picture flippidippi  ·  3Comments

MehrdadKhnzd picture MehrdadKhnzd  ·  3Comments

BassT picture BassT  ·  3Comments

dcworldwide picture dcworldwide  ·  4Comments

Adherentman picture Adherentman  ·  4Comments