2
votes

Im a beginner trying to set up a graphql API with apollo-express and prisma

All was going well, but than I decided to use this library to add input validation: graphql-constraint-directive

It requires me to use makeExecutableSchema to build my schema so I can use the schemaDirectives param

My code to start the server was like this:

const server = new ApolloServer({
    typeDefs,
    resolvers,
    context: ({req}) => {
        const userId = getUserID(req);

        return {
            userId,
            prisma
        }
    }
});

And all was working perfectly

But to use that lib I had it refactored it like this:

const schema = makeExecutableSchema({
    typeDefs,
    resolvers,
    schemaDirectives: {constraint: ConstraintDirective}
});
const server = new ApolloServer({
    schema,
    context: ({req}) => {
        const userId = getUserID(req);

        return {
            userId,
            prisma
        }
    }
});

And it works too, all my queries and mutations are working, and the validation too.

But it broke the graphql-playground: its no longer able to load my schema and docs, both tabs are empty. And it displays the following error: Server cannot be reached

It still works: Im able to send my querys and mutations and all, but I no longer have code completion and the auto docs, since it doenst knows my schema, and so is no longer as useful

If I replace the executable schema for the pure typeDefs ans resolvers, than it works alright again, playground loads everything again

Am I supposed to do something different when using makeExecutableSchema, for playgroun to work?

1

1 Answers

3
votes

Whether you use makeExecutableSchema or pass the typeDefs and resolvers to the ApolloServer constructor directly shouldn't matter -- Apollo Server uses makeExecutableSchema under the hood anyway. In fact, you can just pass the directive map to the constructor directly:

const server = new ApolloServer({
  typeDefs,
  resolvers,
  schemaDirectives: {constraint: ConstraintDirective}
  context: ({req}) => {
    const userId = getUserID(req);

    return {
      userId,
      prisma
    }
  }
});

This is a bug with the library you're using. The directive replaces the built-in scalars with custom ones, but doesn't actually add those custom scalars to the schema. When Playground tries to introspect the schema, it can't find the custom scalars in the introspection results and errors out. I would avoid that particular library -- it doesn't look like it's actively maintained.