Skip to content

Instantly share code, notes, and snippets.

@danstarns
Created December 26, 2019 13:15
Show Gist options
  • Select an option

  • Save danstarns/2fcfa8b560e7b47784d59648a0ec229d to your computer and use it in GitHub Desktop.

Select an option

Save danstarns/2fcfa8b560e7b47784d59648a0ec229d to your computer and use it in GitHub Desktop.
idio-graphql directives
const { combineNodes, GraphQLNode, IdioDirective } = require("idio-graphql");
const { ApolloServer } = require("apollo-server");
const { HasScopeDirective } = require("graphql-auth-directives");
const gql = require("graphql-tag");
const hasScopeDirective = new IdioDirective({
name: "hasScope",
typeDefs: gql`
directive @hasScope(scopes: [String]!) on FIELD_DEFINITION
`,
resolver: HasScopeDirective
});
const User = new GraphQLNode({
name: "User",
typeDefs: gql`
type User {
id: ID
name: String
age: Int
}
type Query {
user(id: ID!): User @hasScope(scopes: ["user:read"])
}
`,
resolvers: {
Query: {
user: (parent, { id }) => {
// get user from source
return user;
}
}
}
});
async function main() {
const { typeDefs, resolvers, schemaDirectives } = await combineNodes(
[User],
{
directives: [hasScopeDirective]
}
);
const server = new ApolloServer({ typeDefs, resolvers, schemaDirectives });
await server.listen(4000);
console.log(`Server up on port 4000 🚀`);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment