@reaktivo/apollo-server-cloud-functions
TypeScript icon, indicating that this package has built-in type declarations

2.0.0 • Public • Published

title: Google Cloud Functions description: Setting up Apollo Server with Google Cloud Functions

npm version Build Status Coverage Status Get on Slack

Don't use! This is my own personal distribution of apollo-server-cloud-function while I wait for this PR to be merged

This is the Google Cloud Function integration of GraphQL Server. Apollo Server is a community-maintained open-source GraphQL server that works with many Node.js HTTP server frameworks. Read the docs. Read the CHANGELOG.

npm install apollo-server-cloud-function@rc graphql

Deploying with Google Cloud Function

1. Write the API handlers

In a file named graphql.js, place the following code:

const { ApolloServer, gql } = require('apollo-server-cloud-function');

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

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

exports.handler = server.createHandler();

2. Configure your Cloud function and deploy

Set the Function to execute option to handler and deploy

Getting request info

To read information about the current request from the API Gateway event (HTTP headers, HTTP method, body, path, ...) or the current Google Cloud Function (Function Name, Function Version, awsRequestId, time remaining, ...) use the options function. This way they can be passed to your schema resolvers using the context option.

const { ApolloServer, gql } = require('apollo-server-cloud-function');

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

const server = new ApolloServer({
  typeDefs,
  resolvers,
  context: ({ req, res }) => ({
    headers: req.headers,
    req,
    res,
  }),
});

exports.handler = server.createHandler();

Modifying the GCF Response (Enable CORS)

To enable CORS the response HTTP headers need to be modified. To accomplish this use the cors option.

const { ApolloServer, gql } = require('apollo-server-cloud-function');

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

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

exports.handler = server.createHandler({
  cors: {
    origin: '*',
    credentials: true,
  },
});

To enable CORS response for requests with credentials (cookies, http authentication) the allow origin header must equal the request origin and the allow credential header must be set to true.

const { ApolloServer, gql } = require('apollo-server-cloud-function');

// Construct a schema, using GraphQL schema language
const typeDefs = gql`
  type Query {
    hello: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    hello: () => 'Hello world!',
  },
};

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

exports.handler = server.createHandler({
  cors: {
    origin: true,
    credentials: true,
  },
});

Cors Options

The options correspond to the express cors configuration with the following fields(all are optional):

  • origin: boolean | string | string[]
  • methods: string | string[]
  • allowedHeaders: string | string[]
  • exposedHeaders: string | string[]
  • credentials: boolean
  • maxAge: number

Principles

GraphQL Server is built with the following principles in mind:

  • By the community, for the community: GraphQL Server's development is driven by the needs of developers
  • Simplicity: by keeping things simple, GraphQL Server is easier to use, easier to contribute to, and more secure
  • Performance: GraphQL Server is well-tested and production-ready - no modifications needed

Anyone is welcome to contribute to GraphQL Server, just read CONTRIBUTING.md, take a look at the roadmap and make your first PR!

Package Sidebar

Install

npm i @reaktivo/apollo-server-cloud-functions

Weekly Downloads

0

Version

2.0.0

License

MIT

Unpacked Size

27.4 kB

Total Files

18

Last publish

Collaborators

  • reaktivo