Skip to content

Commit

Permalink
basic apollo-engine-reporting integration
Browse files Browse the repository at this point in the history
  • Loading branch information
glasser committed May 31, 2018
1 parent 6618965 commit be28483
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 4 deletions.
60 changes: 60 additions & 0 deletions foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
const { ApolloServer, gql } = require('./packages/apollo-server');

// This is a (sample) collection of books we'll be able to query
// the GraphQL server for. A more complete example might fetch
// from an existing data source like a REST API or database.
const books = [
{
title: 'Harry Potter and the Chamber of Secrets',
author: 'J.K. Rowling',
},
{
title: 'Jurassic Park',
author: 'Michael Crichton',
},
];

// Type definitions define the "shape" of your data and specify
// which ways the data can be fetched from the GraphQL server.
const typeDefs = gql`
# Comments in GraphQL are defined with the hash (#) symbol.
# This "Book" type can be used in other type declarations.
type Book {
title: String
author: String
}
# The "Query" type is the root of all GraphQL queries.
# (A "Mutation" type will be covered later on.)
type Query {
books: [Book]
}
`;

// Resolvers define the technique for fetching the types in the
// schema. We'll retrieve books from the "books" array above.
const resolvers = {
Query: {
books: () => books,
},
};

// In the most basic sense, the ApolloServer can be started
// by passing type definitions (typeDefs) and the resolvers
// responsible for fetching the data for those types.
const server = new ApolloServer({
typeDefs,
resolvers,
engine: {
apiKey: 'service:glasser-test-aer:hSAgHbxMg56FPgCRUDVznw',
endpointUrl: 'https://engine-staging-report.apollodata.com',
debugPrintReports: true,
},
});

// This `listen` method launches a web-server. Existing apps
// can utilize middleware options, which we'll discuss later.
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
2 changes: 2 additions & 0 deletions packages/apollo-server-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,12 @@
},
"dependencies": {
"apollo-cache-control": "^0.1.1",
"apollo-engine-reporting": "0.0.0-beta.8",
"apollo-tracing": "^0.2.0-beta.0",
"graphql-extensions": "0.1.0-beta.7",
"graphql-subscriptions": "^0.5.8",
"graphql-tools": "^3.0.2",
"node-fetch": "^2.1.2",
"subscriptions-transport-ws": "^0.9.9",
"ws": "^5.1.1"
}
Expand Down
17 changes: 16 additions & 1 deletion packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { GraphQLExtension } from 'graphql-extensions';
import { TracingExtension } from 'apollo-tracing';
import { CacheControlExtension } from 'apollo-cache-control';
import { EngineReportingAgent } from 'apollo-engine-reporting';

import { ApolloEngine } from 'apollo-engine';
import {
Expand Down Expand Up @@ -62,6 +63,7 @@ export class ApolloServerBase<Request = RequestInit> {
private schema: GraphQLSchema;
private context?: Context | ContextFunction;
private graphqlPath: string = '/graphql';
private engineReportingAgent?: EngineReportingAgent;
private engineProxy: ApolloEngine;
private extensions: Array<() => GraphQLExtension>;

Expand All @@ -79,6 +81,7 @@ export class ApolloServerBase<Request = RequestInit> {
introspection,
mocks,
extensions,
engine,
...requestOptions
} = config;

Expand Down Expand Up @@ -129,7 +132,19 @@ export class ApolloServerBase<Request = RequestInit> {

// Note: if we're using engineproxy (directly or indirectly), we will extend
// this when we listen.
this.extensions = [...(extensions || [])];
this.extensions = [];

if (engine || (engine !== false && process.env.ENGINE_API_KEY)) {
this.engineReportingAgent = new EngineReportingAgent(
engine === true ? {} : engine,
);
// Let's keep this extension first so it wraps everything.
this.extensions.push(() => this.engineReportingAgent.newExtension());
}

if (extensions) {
this.extensions = [...this.extensions, ...extensions];
}
}

public use({ getHttp, path }: RegistrationOptions) {
Expand Down
1 change: 1 addition & 0 deletions packages/apollo-server-core/src/nodeHttpToRequest.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IncomingMessage } from 'http';
import { Request, Headers } from 'node-fetch';

export function convertNodeHttpToRequest(req: IncomingMessage): Request {
const headers = new Headers();
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-core/src/runHttpQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface HttpQueryRequest {
options:
| GraphQLOptions
| ((...args: Array<any>) => Promise<GraphQLOptions> | GraphQLOptions);
request: Request;
request: Pick<Request, 'url' | 'method' | 'headers'>;
}

export class HttpQueryError extends Error {
Expand Down
8 changes: 6 additions & 2 deletions packages/apollo-server-core/src/runQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export interface QueryOptions {
tracing?: boolean;
// cacheControl?: boolean | CacheControlExtensionOptions;
cacheControl?: boolean | any;
request: Request;
request: Pick<Request, 'url' | 'method' | 'headers'>;
extensions?: Array<() => GraphQLExtension>;
}

Expand Down Expand Up @@ -126,7 +126,11 @@ function doRunQuery(options: QueryOptions): Promise<GraphQLResponse> {
}

const requestDidEnd = extensionStack.requestDidStart({
request: options.request,
// Since the Request interfacess are not the same between node-fetch and
// typescript's lib dom, we should limit the fields that need to be passed
// into requestDidStart to only the ones we need, currently just the
// headers, method, and url
request: options.request as any,
});
return Promise.resolve()
.then(() => {
Expand Down
2 changes: 2 additions & 0 deletions packages/apollo-server-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ConnectionContext } from 'subscriptions-transport-ws';
import { Server as HttpServer } from 'http';
import { ListenOptions as HttpListenOptions } from 'net';
import { GraphQLExtension } from 'graphql-extensions';
import { EngineReportingOptions } from 'apollo-engine-reporting';

import { GraphQLServerOptions as GraphQLOptions } from './graphqlOptions';

Expand Down Expand Up @@ -45,6 +46,7 @@ export interface Config<Server>
context?: Context<any> | ContextFunction<any>;
introspection?: boolean;
mocks?: boolean | IMocks;
engine?: boolean | EngineReportingOptions;
extensions?: Array<() => GraphQLExtension>;
}

Expand Down

0 comments on commit be28483

Please sign in to comment.