graphql-shield
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.
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 });
},
};
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 addpriority
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 Thank you. It works for me.
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.
i appeared same issue ,