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

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

Open Baloc22m opened this issue 3 years ago • 5 comments

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 });
  },
};

Baloc22m avatar Jan 08 '22 01:01 Baloc22m

Hey @Baloc22m :wave:,

Thank you for opening an issue. We will get back to you as soon as we can. Have you seen our Open Collective page? Please consider contributing financially to our project. This will help us involve more contributors and get to issues like yours faster.

https://opencollective.com/graphql-shield

We offer priority support for all financial contributors. Don't forget to add priority label once you become one! :smile:

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. 😉

MAMISHO avatar Jan 22 '22 07:01 MAMISHO

@MAMISHO Thank you. It works for me.

yhunglee avatar Feb 12 '22 16:02 yhunglee

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]

i appeared same issue ,

parksusung avatar Jun 15 '22 02:06 parksusung