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.

Help fixing: ValidationError: It seems like you have applied rules to Query types but Shield cannot find them in your schema.

See original GitHub issue

Bug report

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

I tried integrating graphql-apollo with graphql shield, However, I get a validation error as below:

ValidationError: It seems like you have applied rules to Query types but Shield cannot find them in your schema.
    at generateMiddlewareFromSchemaAndRuleTree
 (/node_modules/graphql-shield/src/generator.ts:250:13)

My code:

export default shield(
  {
    Query: {
      viewer: isAuthenticated,
    },
  }
);

#Graphql schema definition:
const RootQuery = new GraphQLObjectType({
  name: "RootQuery",
  fields: {
    viewer: VIEWER,
  },
});


export const schema = new GraphQLSchema({
  query: RootQuery,
  mutation: Mutation,
});

#Integration of graphql shield:

const apolloServer = new ApolloServer({
    schema: applyMiddleware(schema, permissions),
    context: async ({ req }) => {
     
      const user = await getUserContextIsAuthenticated(req);

      return { user };
    },
  });

Viewer’s query:

export const VIEWER = {
  type: UserType,
  args: {},
  resolve: async (_parent: any, args: any, { user }: any) => {
    const manager = getConnection("main_db");

    console.log("viwer context", user);
    const id = args.id;
    return await manager.getRepository(Users).findOne({ id: user.id });
  },
};

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
MAMISHOcommented, Aug 21, 2022

hello Baloc22m.

If you use GraphQLObjectType to define your schema and you want to apply some rules, you should to use the property name (defined in GraphQLObjectType ) on permissions.

const permisions = shield(
  {
    RootQuery: {
      viewer: isAuthenticated,
    },
  }
);

const RootQuery = new GraphQLObjectType({
  name: "RootQuery", // this identify on shield
  fields: {
    viewer: VIEWER,
  },
});

let me know if its working. 😉

1reaction
yhungleecommented, Feb 12, 2022

@MAMISHO Thank you. It works for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

graphql-shield - Bountysource
ValidationError : It seems like you have applied rules to Query types but Shield cannot find them in your schema. at generateMiddlewareFromSchemaAndRuleTree ......
Read more >
GraphQL Shield - Medium
The process of implementing Shield into existing applications consists of two steps — defining rules and assigning them to schema, types or ...
Read more >
Troubleshooting CloudFormation - AWS Documentation
Troubleshoot issues that might occur when you create, update, or delete an AWS CloudFormation stack.
Read more >
Thoughts about Using GraphQL Shield in a layer above ...
Now let's consider a query that we actually want to run. Let's get the 5 most recent posts. This should obey the auth...
Read more >
Common Problems Found in RAML 1.0 API Specifications
To resolve the problem, you would replace schema and schemas with type and types : ... Any name that is used in the...
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