Skip to content

Commit

Permalink
Preparations for graphql@15.x. (#1743)
Browse files Browse the repository at this point in the history
This PR makes the minimal number of changes necessary for the apollo-tooling's
repository's tests to pass for the planned graphql@15.x release.

Notable changes to implementations are as follows:

an adjustment to account for the new onError callback introduced by
graphql/graphql-js#2074, which deprecated the previously used
ValidationContext.prototype.getErrors method.

utilize the GraphQLEnumValueConfig type instead of GraphQLEnumValue
(which lead to the removal of the name field, which isn't actually needed when
building the values for the GraphQLEnumType constructor.

Ref: graphql/graphql-js#2303

Co-authored-by: Trevor Scheer <trevor.scheer@gmail.com>
  • Loading branch information
abernix and trevor-scheer committed Jul 7, 2020
1 parent cbf732f commit 56c37d8
Show file tree
Hide file tree
Showing 16 changed files with 65 additions and 46 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Expand Up @@ -3,7 +3,7 @@
## Upcoming

- `apollo`
- <First `apollo` related entry goes here>
- Add support for graphql version ^15 [#1743](https://github.com/apollographql/apollo-tooling/pull/1743)
- `apollo-codegen-flow`
- <First `apollo-codegen-flow` related entry goes here>
- `apollo-codegen-scala`
Expand All @@ -17,9 +17,10 @@
- `apollo-env`
- <First `apollo-env` related entry goes here>
- `apollo-graphql`
- <First `apollo-graphql` related entry goes here>
- Add support for graphql version ^15 [#1743](https://github.com/apollographql/apollo-tooling/pull/1743)
- `apollo-language-server`
- Fix definition navigation for vscode using localSchemaFile [#1996](https://github.com/apollographql/apollo-tooling/pull/1996)
- Add support for graphql version ^15 [#1743](https://github.com/apollographql/apollo-tooling/pull/1743)
- `apollo-tools`
- <First `apollo-tools` related entry goes here>
- `vscode-apollo`
Expand Down
18 changes: 5 additions & 13 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -76,7 +76,7 @@
"@types/node-fetch": "2.5.7",
"@types/table": "4.0.7",
"cross-env": "^7.0.2",
"graphql": "14.5.7",
"graphql": "15.2.0",
"heroku-cli-util": "8.0.12",
"husky": "3.1.0",
"jest": "24.9.0",
Expand Down
Expand Up @@ -49,7 +49,8 @@ exports[`JSON output should generate JSON output for a mutation with an enum and
\\"responseName\\": \\"__typename\\",
\\"fieldName\\": \\"__typename\\",
\\"type\\": \\"String!\\",
\\"isConditional\\": false
\\"isConditional\\": false,
\\"isDeprecated\\": false
},
{
\\"responseName\\": \\"stars\\",
Expand Down Expand Up @@ -176,7 +177,8 @@ exports[`JSON output should generate JSON output for a query with a fragment spr
\\"responseName\\": \\"__typename\\",
\\"fieldName\\": \\"__typename\\",
\\"type\\": \\"String!\\",
\\"isConditional\\": false
\\"isConditional\\": false,
\\"isDeprecated\\": false
},
{
\\"responseName\\": \\"id\\",
Expand Down Expand Up @@ -217,7 +219,8 @@ exports[`JSON output should generate JSON output for a query with a fragment spr
\\"responseName\\": \\"__typename\\",
\\"fieldName\\": \\"__typename\\",
\\"type\\": \\"String!\\",
\\"isConditional\\": false
\\"isConditional\\": false,
\\"isDeprecated\\": false
},
{
\\"responseName\\": \\"name\\",
Expand All @@ -240,7 +243,8 @@ exports[`JSON output should generate JSON output for a query with a fragment spr
\\"responseName\\": \\"__typename\\",
\\"fieldName\\": \\"__typename\\",
\\"type\\": \\"String!\\",
\\"isConditional\\": false
\\"isConditional\\": false,
\\"isDeprecated\\": false
},
{
\\"responseName\\": \\"name\\",
Expand Down Expand Up @@ -271,7 +275,8 @@ exports[`JSON output should generate JSON output for a query with a fragment spr
\\"responseName\\": \\"__typename\\",
\\"fieldName\\": \\"__typename\\",
\\"type\\": \\"String!\\",
\\"isConditional\\": false
\\"isConditional\\": false,
\\"isDeprecated\\": false
},
{
\\"responseName\\": \\"name\\",
Expand Down Expand Up @@ -321,7 +326,8 @@ exports[`JSON output should generate JSON output for a query with a nested selec
\\"responseName\\": \\"__typename\\",
\\"fieldName\\": \\"__typename\\",
\\"type\\": \\"String!\\",
\\"isConditional\\": false
\\"isConditional\\": false,
\\"isDeprecated\\": false
},
{
\\"responseName\\": \\"name\\",
Expand All @@ -343,7 +349,8 @@ exports[`JSON output should generate JSON output for a query with a nested selec
\\"responseName\\": \\"__typename\\",
\\"fieldName\\": \\"__typename\\",
\\"type\\": \\"String!\\",
\\"isConditional\\": false
\\"isConditional\\": false,
\\"isDeprecated\\": false
},
{
\\"responseName\\": \\"name\\",
Expand Down Expand Up @@ -411,7 +418,8 @@ exports[`JSON output should generate JSON output for a query with an enum variab
\\"responseName\\": \\"__typename\\",
\\"fieldName\\": \\"__typename\\",
\\"type\\": \\"String!\\",
\\"isConditional\\": false
\\"isConditional\\": false,
\\"isDeprecated\\": false
},
{
\\"responseName\\": \\"name\\",
Expand Down Expand Up @@ -500,7 +508,8 @@ exports[`JSON output should generate JSON output for a subscription 1`] = `
\\"responseName\\": \\"__typename\\",
\\"fieldName\\": \\"__typename\\",
\\"type\\": \\"String!\\",
\\"isConditional\\": false
\\"isConditional\\": false,
\\"isDeprecated\\": false
},
{
\\"responseName\\": \\"id\\",
Expand Down
1 change: 0 additions & 1 deletion packages/apollo-codegen-scala/src/codeGeneration.ts
Expand Up @@ -48,7 +48,6 @@ import {
import { GraphQLType } from "graphql";
import { Property } from "./language";
import { GraphQLCompositeType } from "graphql";
import { createLexer } from "graphql/language";

export function generateSource(context: LegacyCompilerContext) {
const generator = new CodeGenerator(context);
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-graphql/package.json
Expand Up @@ -15,7 +15,7 @@
"lodash.sortby": "^4.7.0"
},
"peerDependencies": {
"graphql": "^14.2.1"
"graphql": "^14.2.1 || ^15.0.0"
},
"jest": {
"preset": "ts-jest",
Expand Down
Expand Up @@ -282,7 +282,7 @@ type MutationRoot {
`
)
).toThrowErrorMatchingInlineSnapshot(
`"There can be only one directive named \\"something\\"."`
`"There can be only one directive named \\"@something\\"."`
);
});

Expand All @@ -303,7 +303,7 @@ type MutationRoot {
})
)
).toThrowErrorMatchingInlineSnapshot(
`"Directive \\"something\\" already exists in the schema. It cannot be redefined."`
`"Directive \\"@something\\" already exists in the schema. It cannot be redefined."`
);
});

Expand Down Expand Up @@ -348,7 +348,7 @@ type MutationRoot {
`
)
).toThrowErrorMatchingInlineSnapshot(
`"Unknown directive \\"something\\"."`
`"Unknown directive \\"@something\\"."`
);
});
});
Expand Down
6 changes: 3 additions & 3 deletions packages/apollo-graphql/src/schema/buildSchemaFromSDL.ts
Expand Up @@ -18,7 +18,7 @@ import {
isAbstractType,
isScalarType,
isEnumType,
GraphQLEnumValue
GraphQLEnumValueConfig
} from "graphql";
import { validateSDL } from "graphql/validation/validate";
import { isDocumentNode, isNode } from "../utilities/graphql";
Expand Down Expand Up @@ -247,18 +247,18 @@ export function addResolversToSchema(

if (isEnumType(type)) {
const values = type.getValues();
const newValues: { [key: string]: GraphQLEnumValue } = {};
const newValues: { [key: string]: GraphQLEnumValueConfig } = {};
values.forEach(value => {
let newValue = (fieldConfigs as any)[value.name];
if (newValue === undefined) {
newValue = value.name;
}

newValues[value.name] = {
value: newValue,
deprecationReason: value.deprecationReason,
description: value.description,
astNode: value.astNode,
name: value.name,
extensions: undefined
};
});
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-language-server/package.json
Expand Up @@ -34,7 +34,7 @@
"cosmiconfig": "^5.0.6",
"dotenv": "^8.0.0",
"glob": "^7.1.3",
"graphql": "14.0.2 - 14.2.0 || ^14.3.1",
"graphql": "14.0.2 - 14.2.0 || ^14.3.1 || ^15.0.0",
"graphql-tag": "^2.10.1",
"lodash.debounce": "^4.0.8",
"lodash.merge": "^4.6.1",
Expand Down
22 changes: 20 additions & 2 deletions packages/apollo-language-server/src/errors/validation.ts
Expand Up @@ -51,7 +51,18 @@ export function getValidationErrors(
rules: ValidationRule[] = defaultValidationRules
) {
const typeInfo = new TypeInfo(schema);
const context = new ValidationContext(schema, document, typeInfo);

// The 4th argument to `ValidationContext` is an `onError` callback. This was
// introduced by https://github.com/graphql/graphql-js/pull/2074 and first
// published in graphql@14.5.0. It is meant to replace the `getErrors` method
// which was previously used. Since we support versions of graphql older than
// that, it's possible that this callback will not be invoked and we'll need
// to resort to using `getErrors`. Therefore, although we'll collect errors
// via this callback, if `getErrors` is present on the context we create,
// we'll go ahead and use that instead.
const errors: GraphQLError[] = [];
const onError = (err: GraphQLError) => errors.push(err);
const context = new ValidationContext(schema, document, typeInfo, onError);

if (fragments) {
(context as any)._fragments = fragments;
Expand All @@ -60,7 +71,14 @@ export function getValidationErrors(
const visitors = rules.map(rule => rule(context));
// Visit the whole document with each instance of all provided rules.
visit(document, visitWithTypeInfo(typeInfo, visitInParallel(visitors)));
return context.getErrors();

// @ts-ignore
// `getErrors` is gone in `graphql@15`, but we still support older versions.
if (typeof context.getErrors === "function") return context.getErrors();

// If `getErrors` doesn't exist, we must be on a `graphql@15` or higher,
// so we'll use the errors we collected via the `onError` callback.
return errors;
}

export function validateQueryDocument(
Expand Down
7 changes: 3 additions & 4 deletions packages/apollo-language-server/src/project/client.ts
Expand Up @@ -18,12 +18,9 @@ import {
FieldNode,
ObjectTypeDefinitionNode,
GraphQLObjectType,
DefinitionNode,
DirectiveDefinitionNode
DefinitionNode
} from "graphql";
import { ValidationRule } from "graphql/validation/ValidationContext";
import Maybe from "graphql/tsutils/Maybe";

import { NotificationHandler, DiagnosticSeverity } from "vscode-languageserver";

import { rangeForASTNode } from "../utilities/source";
Expand All @@ -50,6 +47,8 @@ import {
} from "../diagnostics";
import URI from "vscode-uri";

type Maybe<T> = null | undefined | T;

function schemaHasASTNodes(schema: GraphQLSchema): boolean {
const queryType = schema && schema.getQueryType();
return !!(queryType && queryType.astNode);
Expand Down
Expand Up @@ -71,7 +71,7 @@ export class FileSchemaProvider implements GraphQLSchemaProvider {

const ext = extname(path);

// an actual introspectionQuery result, convert to DocumentNode
// an actual introspection query result, convert to DocumentNode
if (ext === ".json") {
const parsed = JSON.parse(result);
const __schema = parsed.data
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo/package.json
Expand Up @@ -56,7 +56,7 @@
"git-rev-sync": "2.0.0",
"git-url-parse": "^11.1.2",
"glob": "7.1.5",
"graphql": "14.0.2 - 14.2.0 || ^14.3.1",
"graphql": "14.0.2 - 14.2.0 || ^14.3.1 || ^15.0.0",
"graphql-tag": "2.10.3",
"listr": "0.14.3",
"lodash.identity": "3.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo/src/Command.ts
Expand Up @@ -75,7 +75,7 @@ export abstract class ProjectCommand extends Command {
return JSON.stringify({ [key]: value });
},
description:
"Additional header to send to server for introspectionQuery. May be used multiple times to add multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the `--header` flag."
"Additional header to send during introspection. May be used multiple times to add multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the `--header` flag."
}),
endpoint: flags.string({
description: "The URL for the CLI use to introspect your service"
Expand Down
Expand Up @@ -4,7 +4,7 @@ import path from "path";
import fs from "fs";
import { test as setup } from "apollo-cli-test";
import {
introspectionQuery,
getIntrospectionQuery,
print,
execute,
buildSchema,
Expand Down Expand Up @@ -46,7 +46,8 @@ const {

// introspection results of a schema, JSON.stringified
const fullSchemaJsonString = JSON.stringify(
execute(buildSchema(graphQLSchema), gql(introspectionQuery)).data.__schema
execute(buildSchema(graphQLSchema), gql(getIntrospectionQuery())).data
.__schema
);

// to be used for sample js files that contain client side schema definitions
Expand Down
Expand Up @@ -103,7 +103,7 @@ function uncaptureApplicationOutput(): string | null {
function sdlToIntrospectionQueryResult(schemaSdl: string) {
return graphql.graphqlSync(
graphql.buildSchema(schemaSdl),
graphql.introspectionQuery
graphql.getIntrospectionQuery()
).data;
}

Expand Down

0 comments on commit 56c37d8

Please sign in to comment.