GraphQL Apollo Server

Till now we have been using a simple graphql-express middleware as a gql server.

https://github.com/apollographql/apollo-server 

Apollo-Server provides a full feature gql server which is production ready and should be used for most applications.

Let’s move our code base to apollo-server

First install via

$ npm install --save apollo-server graphql

Also we don’t need graphql-tools anymore as it is inbuilt in apollo-server

So final code looks like this

const { ApolloServer } = require('apollo-server');



import { typeDefs, resolvers } from "./schema"

// var app = express();

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

server.listen(4000);

Here is the full source https://github.com/nodeexcel/gql_tutorial/commit/add111593fb542006f2ddae86b47fd1aecd9f680

After apollo-server is running your playground looks like this

There is another feature with apollo-server the ‘gql’ literal tag. https://www.apollographql.com/docs/apollo-server/migration-two-dot.html#gql-tag

The main reason for this for syntax highlighting of gql. If you using vscode, you can install graphql-prisma extension and if use the gql tag your code will get highlighted.

e.g

syntax is highlighter in this due to ‘gql’ literal.

syntax is not highlighted

this is also a cool feature!

There are many more things which can be done with apollo-server which you can read on there official docs

https://www.apollographql.com/docs/apollo-server/features/mocking.html#Default-mock-example

https://www.apollographql.com/docs/apollo-server/features/authentication.html

https://www.apollographql.com/docs/apollo-server/essentials/server.html#middleware

https://www.apollographql.com/docs/apollo-server/essentials/server.html#serverless

excellence-social-linkdin
excellence-social-facebook
excellence-social-instagram
excellence-social-skype