![]() * AER: Remove encodedTraces to prevent duplicates When there are multiple instances of apollo-engine-reporting, the `Trace.encode` method gets wrapped each time to add the `encodedTraces`. In order to prevent them from being added to the protobuf multiple times, we remove the encodedTraces after adding them once * Add changelog * Move incremental Trace encoding to a-e-r-protobuf To enable incrmental compilation of traces, we add a patch to the Trace.encode method generated by protobujs to accept and store encoded traces. Occassionally with multiple instances of apollo-engine-reporting that share the same version of the protobuf, the wrapper method gets applied more than once. In order to ensure that the wrapper only gets applied once, we patch the Trace.encode method inside of apollo-engine-protobuf. tsc hangs on the processing the generated protobuf.js files, so the tsconfig.json ignores the generated protobuf file. In order for the typescript index.ts file to compile the generated protobuf.js file is output to the src folder. To ensure the protobuf files are available to the production build, `npm run compile` copies the protobuf file manually from src to dist. * Reexport protobuf import after modification `export * from './protobuf'` exports the unmodified reference * Update comment on Trace.encode to point at a-e-r-p The override now occurs inside of apollo-engine-reporting-protobuf due to the case of having multiple reporting challenges, so we need to update the comments to help indicate that * Remove typescript build step In order to simplify the generation of this library, we move the change the index.ts file into index.js and remove the typescript build step. Since the type safety is created by the protobufjs generation, this seems acceptable. In general this portion of the code should remain relatively stable, so generating and copying the code with `prepare` remains reasonable. |
||
---|---|---|
.circleci | ||
.github | ||
.vscode | ||
__mocks__ | ||
docs | ||
packages | ||
types | ||
.gitignore | ||
.prettierignore | ||
.prettierrc.js | ||
CHANGELOG.md | ||
codecov.yml | ||
CONTRIBUTING.md | ||
DESIGN.md | ||
jest.config.base.js | ||
lerna.json | ||
LICENSE | ||
netlify.toml | ||
package-lock.json | ||
package.json | ||
README.md | ||
renovate.json | ||
ROADMAP.md | ||
tsconfig.base.json | ||
tsconfig.build.json | ||
tsconfig.json | ||
tsconfig.test.base.json | ||
tsconfig.test.json |
GraphQL Server for Express, Connect, Hapi, Cloudflare workers, and more
Apollo Server is a community-maintained open-source GraphQL server. It works with pretty much all Node.js HTTP server frameworks, and we're happy to take PRs for more! Apollo Server works with any GraphQL schema built with GraphQL.js, so you can build your schema with that directly or with a convenience library such as graphql-tools.
Documentation
Principles
Apollo Server is built with the following principles in mind:
- By the community, for the community: Apollo Server's development is driven by the needs of developers
- Simplicity: by keeping things simple, Apollo Server is easier to use, easier to contribute to, and more secure
- Performance: Apollo Server is well-tested and production-ready - no modifications needed
Anyone is welcome to contribute to Apollo Server, just read CONTRIBUTING.md, take a look at the roadmap and make your first PR!
Getting started
Apollo Server is super easy to set up. Just npm install apollo-server-<integration>
, write a GraphQL schema, and then use one of the following snippets to get started. For more info, read the Apollo Server docs.
Installation
Run npm install --save apollo-server-<integration>
and you're good to go!
const { ApolloServer, gql } = require('apollo-server');
// The GraphQL schema
const typeDefs = gql`
type Query {
"A simple type for getting started!"
hello: String
}
`;
// A map of functions which return data for the schema.
const resolvers = {
Query: {
hello: () => 'world',
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
});
While we recommend the use schema-definition language (SDL) for defining a GraphQL schema since we feel it's more human-readable and language-agnostic, Apollo Server can be configured with a GraphQLSchema
object:
const { ApolloServer, gql } = require('apollo-server');
const { GraphQLSchema, GraphQLObjectType, GraphQLString } = require('graphql');
// The GraphQL schema
const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'Query',
fields: {
hello: {
type: GraphQLString,
description: 'A simple type for getting started!',
resolve: () => 'world',
},
},
}),
});
const server = new ApolloServer({ schema });
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
Integrations
Often times, Apollo Server needs to be run with a particular integration. To start, run npm install --save apollo-server-<integration>
where apollo-server-<integration>
is one of the following:
apollo-server-express
apollo-server-koa
apollo-server-hapi
apollo-server-lambda
apollo-server-azure-functions
apollo-server-cloud-functions
apollo-server-cloudflare
Express
const express = require('express');
const { ApolloServer, gql } = require('apollo-server-express');
// 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 });
const app = express();
server.applyMiddleware({ app });
const port = 4000;
app.listen({ port }, () =>
console.log(`🚀 Server ready at http://localhost:${port}${server.graphqlPath}`),
);
Connect
const connect = require('connect');
const { ApolloServer, gql } = require('apollo-server-express');
const query = require('qs-middleware');
// 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 });
const app = connect();
const path = '/graphql';
app.use(query());
server.applyMiddleware({ app, path });
const port = 4000;
app.listen({ port }, () =>
console.log(`🚀 Server ready at http://localhost:${port}${server.graphqlPath}`),
);
Note;
qs-middleware
is only required if running outside of Meteor
Koa
const Koa = require('koa');
const { ApolloServer, gql } = require('apollo-server-koa');
// 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 });
const app = new Koa();
server.applyMiddleware({ app });
const port = 3000;
const host = 'localhost';
app.listen(port, host, () =>
console.log(`🚀 Server ready at http://${host}:${port}${server.graphqlPath}`),
);
Hapi
The code below requires Hapi 17 or higher.
const { ApolloServer, gql } = require('apollo-server-hapi');
const Hapi = require('hapi');
async function StartServer() {
const server = new ApolloServer({ typeDefs, resolvers });
const app = new Hapi.server({
port: 4000,
});
await server.applyMiddleware({
app,
});
await server.installSubscriptionHandlers(app.listener);
await app.start();
}
StartServer().catch(error => console.log(error));
Context
The context is created for each request. The following code snippet shows the creation of a context. The arguments are the request
, the request, and h
, the response toolkit.
new ApolloServer({
typeDefs,
resolvers,
context: async ({ request, h }) => {
return { ... };
},
})
AWS Lambda
Apollo Server can be run on Lambda and deployed with AWS Serverless Application Model (SAM). It requires an API Gateway with Lambda Proxy Integration.
const { ApolloServer, gql } = require('apollo-server-lambda');
// 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.graphqlHandler = server.createHandler();
Apollo Server Development
If you want to develop Apollo Server locally you must follow the following instructions:
-
Fork this repository
-
Install the Apollo Server project in your computer
git clone https://github.com/[your-user]/apollo-server
cd apollo-server
npm install
cd packages/apollo-server-<integration>/
npm link
- Install your local Apollo Server in other App
cd ~/myApp
npm link apollo-server-<integration>
Maintainers
- @martijnwalraven (Apollo)
- @abernix (Apollo)