Skip to content

Commit

Permalink
Merge pull request #3988
Browse files Browse the repository at this point in the history
  • Loading branch information
abernix committed May 12, 2020
2 parents c8520ab + 1b98f87 commit b2073e8
Show file tree
Hide file tree
Showing 15 changed files with 1,225 additions and 85 deletions.
26 changes: 22 additions & 4 deletions docs/source/integrations/plugins.md
Expand Up @@ -121,17 +121,18 @@ The following diagram illustrates the sequence of events that fire for each requ

```mermaid
graph TB;
request(requestDidStart) --> parsing(parsingDidStart*);
request(requestDidStart) --> resolveSource(didResolveSource);
resolveSource --"Success"--> parsing(parsingDidStart*);
parsing --"Success"--> validation(validationDidStart*);
validation --"Success"--> resolve(didResolveOperation);
resolve --"Success"--> response(responseForOperation);
validation --"Success"--> resolveOperation(didResolveOperation);
resolveOperation --"Success"--> response(responseForOperation);
execution(executionDidStart*);
errors(didEncounterErrors);
response --"Response provided"--> send;
response --"No response provided"--> execution;
execution --"Success"--> send(willSendResponse);
execution & resolve & parsing & validation --"Failure"--> errors;
execution & resolveSource & resolveOperation & parsing & validation --"Failure"--> errors;
errors --> send;
class server,request secondary;
```
Expand Down Expand Up @@ -313,6 +314,23 @@ should not return a value.

> If you're using TypeScript to create your plugin, implement the [ `GraphQLRequestListener` interface](https://github.com/apollographql/apollo-server/blob/master/packages/apollo-server-plugin-base/src/index.ts) from the `apollo-server-plugin-base` module to define functions for request lifecycle events.
### `didResolveSource`

The `didResolveSource` event is invoked after Apollo Server has determined the
`String`-representation of the incoming operation that it will act upon. In the
event that this `String` was not directly passed in from the client, this
may be retrieved from a cache store (e.g., Automated Persisted Queries).

At this stage, there is not a guarantee that the operation is not malformed.

```typescript
didResolveSource?(
requestContext: WithRequired<
GraphQLRequestContext<TContext>, 'source' | 'logger'>,
>,
): ValueOrPromise<void>;
```

### `parsingDidStart`

The `parsingDidStart` event fires whenever Apollo Server will parse a GraphQL
Expand Down
6 changes: 3 additions & 3 deletions packages/apollo-engine-reporting/src/agent.ts
Expand Up @@ -12,7 +12,7 @@ import { fetch, RequestAgent, Response } from 'apollo-server-env';
import retry from 'async-retry';

import { EngineReportingExtension } from './extension';
import { GraphQLRequestContext, Logger } from 'apollo-server-types';
import { GraphQLRequestContext, Logger, SchemaHash } from 'apollo-server-types';
import { InMemoryLRUCache } from 'apollo-server-caching';
import { defaultEngineReportingSignature } from 'apollo-graphql';

Expand Down Expand Up @@ -250,7 +250,7 @@ export interface AddTraceArgs {
trace: Trace;
operationName: string;
queryHash: string;
schemaHash: string;
schemaHash: SchemaHash;
queryString?: string;
documentAST?: DocumentNode;
}
Expand Down Expand Up @@ -328,7 +328,7 @@ export class EngineReportingAgent<TContext = any> {
handleLegacyOptions(this.options);
}

public newExtension(schemaHash: string): EngineReportingExtension<TContext> {
public newExtension(schemaHash: SchemaHash): EngineReportingExtension<TContext> {
return new EngineReportingExtension<TContext>(
this.options,
this.addTrace.bind(this),
Expand Down
9 changes: 7 additions & 2 deletions packages/apollo-engine-reporting/src/extension.ts
@@ -1,4 +1,9 @@
import { GraphQLRequestContext, WithRequired, Logger } from 'apollo-server-types';
import {
GraphQLRequestContext,
WithRequired,
Logger,
SchemaHash,
} from 'apollo-server-types';
import { Request, Headers } from 'apollo-server-env';
import {
GraphQLResolveInfo,
Expand Down Expand Up @@ -42,7 +47,7 @@ export class EngineReportingExtension<TContext = any>
public constructor(
options: EngineReportingOptions<TContext>,
addTrace: (args: AddTraceArgs) => Promise<void>,
private schemaHash: string,
private schemaHash: SchemaHash,
) {
this.options = {
...options,
Expand Down
8 changes: 3 additions & 5 deletions packages/apollo-gateway/src/executeQueryPlan.ts
Expand Up @@ -89,11 +89,9 @@ export async function executeQueryPlan<TContext>(
},
rootValue: data,
variableValues: requestContext.request.variables,
// FIXME: GraphQL extensions currently wraps every field and creates
// a field resolver. Because of this, when using with ApolloServer
// the defaultFieldResolver isn't called. We keep this here
// because it is the correct solution and when ApolloServer removes
// GraphQLExtensions this will be how alias support is maintained
// We have a special field resolver which ensures we support aliases.
// FIXME: It's _possible_ this will change after `graphql-extensions` is
// deprecated, though not certain. See here, also: https://git.io/Jf8cS.
fieldResolver: defaultFieldResolverWithAliasSupport,
}));
} catch (error) {
Expand Down
4 changes: 4 additions & 0 deletions packages/apollo-gateway/src/index.ts
Expand Up @@ -476,6 +476,10 @@ export class ApolloGateway implements GraphQLService {

this.logger.debug('Schema loaded and ready for execution');

// FIXME: The comment below may change when `graphql-extensions` is
// removed, as it will be soon. It's not clear if this will be temporary,
// as is suggested, after that time, because we still very much need to
// do this special alias resolving. Original comment:
// this is a temporary workaround for GraphQLFieldExtensions automatic
// wrapping of all fields when using ApolloServer. Here we wrap all fields
// with support for resolving aliases as part of the root value which
Expand Down
18 changes: 11 additions & 7 deletions packages/apollo-server-core/src/ApolloServer.ts
Expand Up @@ -71,8 +71,8 @@ import {

import { Headers } from 'apollo-server-env';
import { buildServiceDefinition } from '@apollographql/apollo-tools';
import { Logger, SchemaHash } from "apollo-server-types";
import { getEngineApiKey, getEngineGraphVariant } from "apollo-engine-reporting/dist/agent";
import { Logger } from "apollo-server-types";

const NoIntrospection = (context: ValidationContext) => ({
Field(node: FieldDefinitionNode) {
Expand Down Expand Up @@ -109,7 +109,7 @@ type SchemaDerivedData = {
// on the same operation to be executed immediately.
documentStore?: InMemoryLRUCache<DocumentNode>;
schema: GraphQLSchema;
schemaHash: string;
schemaHash: SchemaHash;
extensions: Array<() => GraphQLExtension>;
};

Expand Down Expand Up @@ -759,12 +759,16 @@ export class ApolloServerBase {
return sdlFieldType.name == 'String';
}

private ensurePluginInstantiation(plugins?: PluginDefinition[]): void {
if (!plugins || !plugins.length) {
return;
}
private ensurePluginInstantiation(plugins: PluginDefinition[] = []): void {
const pluginsToInit: PluginDefinition[] = [];

// Internal plugins should be added to `pluginsToInit` here.
// User's plugins, provided as an argument to this method, will be added
// at the end of that list so they take precedence.
// A follow-up commit will actually introduce this.

this.plugins = plugins.map(plugin => {
pluginsToInit.push(...plugins);
this.plugins = pluginsToInit.map(plugin => {
if (typeof plugin === 'function') {
return plugin();
}
Expand Down

0 comments on commit b2073e8

Please sign in to comment.