Skip to content

Commit

Permalink
feat(graphql): initial v15 support.
Browse files Browse the repository at this point in the history
Does not yet support v15 specific features that require graphql version checking, i.e. interfaces that implement interfaces, but existing tests should pass.
A few unnecessary verbose tests deleted.
  • Loading branch information
yaacovCR committed Feb 17, 2020
1 parent c8b5b16 commit 2280eef
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 244 deletions.
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
language: node_js
node_js:
- "8"
- "13"
- "12"
- "10"

install:
- npm config set spin=false
- npm install -g mocha coveralls
- npm install
- npm install graphql@$GRAPHQL_VERSION
- npm install @types/graphql@$TYPES_GRAPHQL_VERSION
- npm install graphql@$GRAPHQL_CHANNEL

script:
- npm test
Expand All @@ -19,4 +19,5 @@ script:
sudo: false

env:
- GRAPHQL_VERSION='^14.0' TYPES_GRAPHQL_VERSION='^14.0'
- GRAPHQL_CHANNEL='latest'
- GRAPHQL_CHANNEL='rc'
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"uuid": "^3.4.0"
},
"peerDependencies": {
"graphql": "^14.2.0"
"graphql": "^14.2.0 || ^15.0.0-rc"
},
"devDependencies": {
"@types/apollo-upload-client": "^8.1.3",
Expand Down Expand Up @@ -85,7 +85,7 @@
"eslint-watch": "^6.0.1",
"express": "^4.17.1",
"express-graphql": "^0.9.0",
"graphql": "^14.5.8",
"graphql": "^14.6.0",
"graphql-subscriptions": "^1.1.0",
"graphql-type-json": "^0.3.1",
"graphql-upload": "^9.0.0",
Expand Down
17 changes: 8 additions & 9 deletions src/generate/buildSchemaFromTypeDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {

import { ITypeDefinitions, GraphQLParseOptions } from '../Interfaces';

import extractExtensionDefinitions from './extractExtensionDefinitions';
import {
extractExtensionDefinitions,
filterExtensionDefinitions,
} from './extensionDefinitions';
import concatenateTypeDefs from './concatenateTypeDefs';
import SchemaError from './SchemaError';

Expand Down Expand Up @@ -37,18 +40,14 @@ function buildSchemaFromTypeDefinitions(
astDocument = parse(myDefinitions, parseOptions);
}

const backcompatOptions = { commentDescriptions: true };
const typesAst = filterExtensionDefinitions(astDocument);

// TODO fix types https://github.com/apollographql/graphql-tools/issues/542
let schema: GraphQLSchema = (buildASTSchema as any)(
astDocument,
backcompatOptions,
);
const backcompatOptions = { commentDescriptions: true };
let schema: GraphQLSchema = buildASTSchema(typesAst, backcompatOptions);

const extensionsAst = extractExtensionDefinitions(astDocument);
if (extensionsAst.definitions.length > 0) {
// TODO fix types https://github.com/apollographql/graphql-tools/issues/542
schema = (extendSchema as any)(schema, extensionsAst, backcompatOptions);
schema = extendSchema(schema, extensionsAst, backcompatOptions);
}

return schema;
Expand Down
37 changes: 37 additions & 0 deletions src/generate/extensionDefinitions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { DocumentNode, DefinitionNode, Kind } from 'graphql';

export function extractExtensionDefinitions(ast: DocumentNode) {
const extensionDefs = ast.definitions.filter(
(def: DefinitionNode) =>
def.kind === Kind.OBJECT_TYPE_EXTENSION ||
def.kind === Kind.INTERFACE_TYPE_EXTENSION ||
def.kind === Kind.INPUT_OBJECT_TYPE_EXTENSION ||
def.kind === Kind.UNION_TYPE_EXTENSION ||
def.kind === Kind.ENUM_TYPE_EXTENSION ||
def.kind === Kind.SCALAR_TYPE_EXTENSION ||
def.kind === Kind.SCHEMA_EXTENSION,
);

return {
...ast,
definitions: extensionDefs,
};
}

export function filterExtensionDefinitions(ast: DocumentNode) {
const extensionDefs = ast.definitions.filter(
(def: DefinitionNode) =>
def.kind !== Kind.OBJECT_TYPE_EXTENSION &&
def.kind !== Kind.INTERFACE_TYPE_EXTENSION &&
def.kind !== Kind.INPUT_OBJECT_TYPE_EXTENSION &&
def.kind !== Kind.UNION_TYPE_EXTENSION &&
def.kind !== Kind.ENUM_TYPE_EXTENSION &&
def.kind !== Kind.SCALAR_TYPE_EXTENSION &&
def.kind !== Kind.SCHEMA_EXTENSION,
);

return {
...ast,
definitions: extensionDefs,
};
}
23 changes: 0 additions & 23 deletions src/generate/extractExtensionDefinitions.ts

This file was deleted.

5 changes: 4 additions & 1 deletion src/generate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ export { default as checkForResolveTypeResolver } from './checkForResolveTypeRes
export { default as concatenateTypeDefs } from './concatenateTypeDefs';
export { default as decorateWithLogger } from './decorateWithLogger';
export { default as extendResolversFromInterfaces } from './extendResolversFromInterfaces';
export { default as extractExtensionDefinitions } from './extractExtensionDefinitions';
export {
extractExtensionDefinitions,
filterExtensionDefinitions,
} from './extensionDefinitions';
export { default as SchemaError } from './SchemaError';

// for backwards compatibility
Expand Down
7 changes: 4 additions & 3 deletions src/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
GraphQLNamedType,
GraphQLFieldResolver,
GraphQLNonNull,
GraphQLNullableType,
} from 'graphql';
import { v4 as uuid } from 'uuid';

Expand Down Expand Up @@ -104,8 +105,7 @@ function addMocksToSchema({
info: GraphQLResolveInfo,
): any => {
// nullability doesn't matter for the purpose of mocking.
const fieldType = getNullableType(type);
const namedFieldType = getNamedType(fieldType);
const fieldType = getNullableType(type) as GraphQLNullableType;

if (fieldName && root && typeof root[fieldName] !== 'undefined') {
let result: any;
Expand All @@ -129,6 +129,7 @@ function addMocksToSchema({

// Now we merge the result with the default mock for this type.
// This allows overriding defaults while writing very little code.
const namedFieldType = getNamedType(fieldType);
if (mockFunctionMap.has(namedFieldType.name)) {
const mock = mockFunctionMap.get(namedFieldType.name);

Expand Down Expand Up @@ -368,7 +369,7 @@ function getResolveType(namedFieldType: GraphQLNamedType) {
}

function assignResolveType(type: GraphQLType, preserveResolvers: boolean) {
const fieldType = getNullableType(type);
const fieldType = getNullableType(type) as GraphQLNullableType;
const namedFieldType = getNamedType(fieldType);

const oldResolveType = getResolveType(namedFieldType);
Expand Down
2 changes: 0 additions & 2 deletions src/stitching/makeRemoteExecutableSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
BuildSchemaOptions,
DocumentNode,
} from 'graphql';
import { Options as PrintSchemaOptions } from 'graphql/utilities/schemaPrinter';

import { addResolversToSchema } from '../generate';
import { Fetcher, Operation } from '../Interfaces';
Expand Down Expand Up @@ -40,7 +39,6 @@ export default function makeRemoteExecutableSchema({
fetcher?: Fetcher;
createResolver?: (fetcher: Fetcher) => GraphQLFieldResolver<any, any>;
buildSchemaOptions?: BuildSchemaOptions;
printSchemaOptions?: PrintSchemaOptions;
}): GraphQLSchema {
let finalFetcher: Fetcher = fetcher;

Expand Down
1 change: 1 addition & 0 deletions src/stitching/mergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export default function mergeSchemas({
typeof schemaLikeObject === 'string'
? parse(schemaLikeObject)
: (schemaLikeObject as DocumentNode);

parsedSchemaDocument.definitions.forEach(def => {
const type = typeFromAST(def);
if (type instanceof GraphQLDirective && mergeDirectives) {
Expand Down

0 comments on commit 2280eef

Please sign in to comment.