Make GraphQL Modules work without useGraphQLModules() plugin #2107
Answered
by
n1ru4l
kensukesaito
asked this question in
Q&A
-
Describe the solution you'd like import { createYoga } from 'graphql-yoga'
import { createApplication } from 'graphql-modules'
const application = createApplication({
modules: [xxx, yyy, zzz]
})
// Current
const yoga = createYoga({
plugins: [
useGraphQLModules(application)
]
})
// Wish
const yoga = createYoga({
schema: application.schema
}) Why I would like to use @n1ru4l/graphql-public-schema-filter with GraphQL Modules. |
Beta Was this translation helpful? Give feedback.
Answered by
n1ru4l
Nov 15, 2022
Replies: 1 comment 1 reply
-
Hey @kensukesaito, you can use the following recipe for serving a public/private schema with https://codesandbox.io/s/bold-napier-iqcjgp?file=/src/main.ts import { createYoga, Plugin } from "graphql-yoga";
import { createModule, gql, createApplication } from "graphql-modules";
import { useGraphQLModules } from "@envelop/graphql-modules";
import {
buildPublicSchema,
publicDirectiveSDL
} from "@n1ru4l/graphql-public-schema-filter";
import { parse } from "graphql";
const myModule = createModule({
id: "my-module",
dirname: __dirname,
typeDefs: [
gql`
type Query {
private: String!
public: String! @public
}
`,
parse(publicDirectiveSDL)
]
});
const application = createApplication({
modules: [myModule]
});
const publicSchema = buildPublicSchema({
schema: application.schema
});
const plugin: Plugin = {
// set the filtered (public) schema based on headers
onEnveloped(ctx) {
const accessAs = ctx.context.request.headers.get("x-access-as");
if (accessAs !== "private") {
ctx.setSchema(publicSchema);
}
}
};
const yoga = createYoga({
plugins: [useGraphQLModules(application), plugin]
}); |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
n1ru4l
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @kensukesaito, you can use the following recipe for serving a public/private schema with
graphql-modules
and@n1ru4l/graphql-public-schema-filter
.https://codesandbox.io/s/bold-napier-iqcjgp?file=/src/main.ts