graphql-shield icon indicating copy to clipboard operation
graphql-shield copied to clipboard

Aliasing not working with graphql-shield permissions middleware and customFieldResolver

Open morgothulhu opened this issue 7 years ago • 8 comments

Bug report

  • [X] 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.

morgothulhu avatar Sep 29 '18 04:09 morgothulhu

Hey @morgothulhu 👋,

Thanks for reporting this. I am not entirely sure but it seems like this might be a graphql-middleware bug. I'll dig deeper into the problem later this week and let you know about the progress. Is this in any way blocking for your progress?

Let me know! 🙂

maticzav avatar Oct 01 '18 09:10 maticzav

@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!

morgothulhu avatar Oct 01 '18 12:10 morgothulhu

@maticzav any path forward with this? Thx in advance!

morgothulhu avatar Oct 10 '18 15:10 morgothulhu

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.

stale[bot] avatar Nov 24 '18 16:11 stale[bot]

any progress on this?

devdudeio avatar Feb 09 '21 19:02 devdudeio

@dude-awesome could you compose a small reproduction CodeSandbox? I can work on this issue much sooner if I have a reproduction I can work on right away.

maticzav avatar Feb 10 '21 13:02 maticzav

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.

stale[bot] avatar Jun 11 '21 02:06 stale[bot]

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.

stale[bot] avatar Apr 16 '22 10:04 stale[bot]