Apollo-server: Add `graphql-middleware` support

Created on 2 Sep 2018  路  8Comments  路  Source: apollographql/apollo-server

Add out of the box support for GraphQL Middleware plugins.

http://github.com/prismagraphql/graphql-middleware

鉀诧笍 feature

Most helpful comment

Hey @Alvyre 馃憢

Since we still haven't implemented this, you could use the alternative approach we mentioned above. Instead of supplying resolvers and typeDefs to server directly you can provide a schema that you build upfront. Something like this;

const schema = makeExecutableSchema({ typeDefs, resolvers })

const schemaWithMiddleware = applyMiddleware(
  schema,
  metricsMiddleware,
  authMiddleware,
  beepMiddleware,
)

const server = new ApolloServer({
  mocks: false,
  schema: schemaWithMiddleware,,
});

You can find even more information about it in the link below. I hope this helps you solve the problem; I will try to make a PR by the end of the week 馃

https://github.com/prisma/graphql-middleware#standalone-usage

All 8 comments

@maticzav do you have an example of the "workaround" for this?

@notrab yeah, you can use it with makeExecutableSchema and then applyMiddleware like this;

const schema = makeExecutableSchema({ typeDefs, resolvers })

const schemaWithMiddleware = applyMiddleware(
  schema,
  metricsMiddleware,
  authMiddleware,
  beepMiddleware,
)

I believe it would be a lot more convenient though to have general support for it. I will try to make a PR in the coming days!

https://github.com/prisma/graphql-middleware#standalone-usage

Awesome thanks @maticzav - it's been a while since I looked at graphql-middleware. Great to see it's taking shape!

Hello guys,

How do you make it work when you pass your resolvers and schemas in the server constructor ?

for the moment I build my server like this :

import { importSchema } from 'graphql-import';
import resolvers from './resolvers';

const typeDefs  = importSchema('./schema.graphql'));

const server = new ApolloServer({
  mocks: false,
  resolvers,
  typeDefs
});

Hey @Alvyre 馃憢

Since we still haven't implemented this, you could use the alternative approach we mentioned above. Instead of supplying resolvers and typeDefs to server directly you can provide a schema that you build upfront. Something like this;

const schema = makeExecutableSchema({ typeDefs, resolvers })

const schemaWithMiddleware = applyMiddleware(
  schema,
  metricsMiddleware,
  authMiddleware,
  beepMiddleware,
)

const server = new ApolloServer({
  mocks: false,
  schema: schemaWithMiddleware,,
});

You can find even more information about it in the link below. I hope this helps you solve the problem; I will try to make a PR by the end of the week 馃

https://github.com/prisma/graphql-middleware#standalone-usage

Thank you for you reply and for the example. It works!

I'm waiting for your PR :+1:

Thank you for your work by the way!

I am having an issue with makeExecutableSchema, where I get "Error: Unknown directive "@cacheControl". This is a built in directive from apollo.

Was this page helpful?
0 / 5 - 0 ratings