Here's one way to do that
You can use an extended schema for the main endpoint and use the same schema without the extension for the graphiql endpoint.
Let's take this schema definition as an example:
// schemaDef
type Query {
anonQuery: QueryResult
adminQuery: AdminQueryResult @admin
}
And the executable schema:
const schema = makeExecutableSchema({
typeDefs: [schemaDef /* ... additional schema files */],
resolvers: merge(schemaResolvers/* ... additional resolvers */)
})
Now, let's split the schema definitions with the help of the extend
keyword.
Read here about Extending Types and the extend
keyword.
// anonymous part of the original schema definition:
type Query {
anonQuery: QueryResult
}
// admin extensions definitions:
extend type Query {
adminQuery: AdminQueryResult @admin
}
To avoid having some warnings about resolvers not defined in the schema, you'll probably want to split the admin related resolvers to another file or another resolver map.
Now you'll have 2 executable schemas:
const mainSchema = makeExecutableSchema({
typeDefs: [schemaDef /* ... additional schema files */],
resolvers: merge(schemaResolvers/* ... additional resolvers */)
})
const extendedSchema = makeExecutableSchema({
typeDefs: [schemaDef, adminSchemaExtensions /* ... additional schema files */],
resolvers: merge(schemaResolvers, adminSchemaResolvers /* ... additional resolvers */)
})
Your main endpoint should use the extended schema.
router.use('/graphql', /* some middlewares */, graphqlExpress({schema: extendedSchema}))
Since the GraphiQL endpoint expects a GraphQL endpoint, you'll have to create another one specifically for the second schema. Perhaps something like this:
router.use('/graphql-anon', /* some middlewares */, graphqlExpress({schema: mainSchema}))
router.use('/graphiql', /* some middlewares */, graphiqlExpress({endpointURL: '/graphql-anon'}))
That's it!
Now most of your code is shared and only part of the schema is accessible using the GraphiQL interface.
Putting the admin definitions in separate files could be seen as more convenient or less convenient, depending on your project, code and preferences.