question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Aliasing not working with graphql-shield permissions middleware and customFieldResolver

See original GitHub issue

Bug report

  • I have checked other issues to make sure this is not a duplicate.

Describe the bug

When using aliasing in my graphQL query, the aliased field is returned as null. If I do return the field + its aliased value, the aliased value (as well as the non-aliased field value) are returned.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. This is my GraphQL Schema.
type Query {
  book: Book!
}

type Book {
  id: ID!
  name: String!
  content: String!
  similarBooks: [Book!]!
}
  1. This is the invoked query that doesn’t work

Query A):

book {
  id
  otherBooks: similarBooks {
    id
  }
}

It works with:

Query B):

book {
  id
  similarBooks {
    id
  }
}

or

Query C):

book {
  id
  similarBooks {
    id
  }
  otherBooks: similarBooks {
    id
  }
}
  1. I use these permissions
const permissions = shield({
  Query: allow,
})
  1. This is the error I see

Actual behavior

In Query A), I am getting null for otherBooks.

Expected behavior

I expected to see a value for otherBooks (assuming the data is set this way). Expected query results are observed for queries B) and C)

Additional context

Here is my graphQL server setup:

import { ApolloServer } from 'apollo-server-lambda';
import { applyMiddleware } from 'graphql-middleware';
import { makeExecutableSchema } from 'graphql-tools';

const schemaWoMiddleware = makeExecutableSchema({
  typeDefs,
  resolvers,
});

const middleWare = applyMiddleware(
  schemaWoMiddleware,
  permissions,
);

const resolverFragmentReplacements = middleWare.fragmentReplacements;
const schema = middleWare.schema;

...

    const server = new ApolloServer({
      // ref: https://github.com/apollographql/apollo-server/issues/865
      fieldResolver: customFieldResolver,
      schema,
      tracing: false,
      cacheControl: false,
    });

    return server.createHandler();

The customFieldResolver is a direct copy of https://github.com/akadop/graphql-yoga/blob/ee0c03fa8d0e9578f4589e19da6beff1d8112a67/src/customFieldResolver.ts

I use those versions:

    "apollo-server-lambda": "2.1.0",
    "graphql": "0.13.2",
    "graphql-middleware": "1.7.3",
    "graphql-playground-middleware-lambda": "1.7.3",
    "graphql-shield": "3.2.3",
    "graphql-tag": "2.9.2",
    "graphql-tools": "3.1.1",

If I remove the permissions in the middleWare, aliasing works (and my customFieldResolver is invoked). With permissions defined in middleWare, the customFieldResolver is never invoked.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
morgothulhucommented, Oct 1, 2018

@maticzav - all good. Fortunately, this is not blocking yet - so any time this week will be perfect. Thanks for the great work with graphql-shield - loving it!

0reactions
stale[bot]commented, Apr 16, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Aliasing not working with graphql-shield permissions ... - GitHub
If I remove the permissions in the middleWare , aliasing works (and my customFieldResolver is invoked). With permissions defined in middleWare , ...
Read more >
graphql-shield - Bountysource
I need the values transformed by yup (like .lowercase() ) as my resolver args . ... Aliasing not working with graphql-shield permissions middleware...
Read more >
Errors – GraphQL Shield
Custom Errors​​ Shield, by default, catches all errors thrown during resolver execution. This way we can be 100% sure none of your internal...
Read more >
Authorization with GraphQL Shield - YouTube
In this video we'll explore some of the logic and input rules that come with GraphQL Shield to protect against unwanted requests.
Read more >
9 Ways to Secure Your Graph - Khalil Stemmler
Custom directives. (e.g @auth (requires: ADMIN)). • Wrap resolver functions. • Put auth rules into middleware layer. (e.g. graphql-shield).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found