Skip to content

Commit

Permalink
buildSchemaFromSDL - support meta fields on abstract types (#1330)
Browse files Browse the repository at this point in the history
* buildSchemaFromSDL - support meta fields on abstract types

Add support for meta resolvers on abstract types.
  • Loading branch information
trevor-scheer authored Jun 12, 2019
1 parent 8c246ce commit 25838b6
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
- `apollo-env`
- <First `apollo-env` related entry goes here>
- `apollo-graphql`
- <First `apollo-graphql` related entry goes here>
- buildSchemaFromSDL - support meta fields on abstract types [#1330](https://github.com/apollographql/apollo-tooling/pull/1330)
- `apollo-language-server`
- <First `apollo-codegen-core` related entry goes here>
- `apollo-tools`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
GraphQLSchema,
GraphQLDirective,
DirectiveLocation,
GraphQLObjectType
GraphQLObjectType,
GraphQLAbstractType
} from "graphql";

import astSerializer from "./snapshotSerializers/astSerializer";
Expand Down Expand Up @@ -345,6 +346,7 @@ type MutationRoot {
);
});
});

describe(`resolvers`, () => {
it(`should add a resolver for a field`, () => {
const name = () => {};
Expand Down Expand Up @@ -372,5 +374,53 @@ type MutationRoot {

expect(nameField.resolve).toEqual(name);
});

it(`should add meta fields to abstract types`, () => {
const typeDefs = gql`
union Animal = Dog
interface Creature {
name: String!
legs: Int!
}
type Dog {
id: ID!
}
type Cat implements Creature {
meow: Boolean!
}
`;

const resolveTypeUnion = (obj: any) => {
return "Dog";
};

const resolveTypeInterface = (obj: any) => {
if (obj.meow) {
return "Cat";
}
throw Error("Couldn't resolve interface");
};

const resolvers = {
Animal: {
__resolveType: resolveTypeUnion
},
Creature: {
__resolveType: resolveTypeInterface
}
};

const schema = buildSchemaFromSDL([{ typeDefs, resolvers }]);
const animalUnion = schema.getType("Animal") as GraphQLAbstractType;
const creatureInterface = schema.getType(
"Creature"
) as GraphQLAbstractType;

expect(animalUnion.resolveType).toBe(resolveTypeUnion);
expect(creatureInterface.resolveType).toBe(resolveTypeInterface);
});
});
});
12 changes: 11 additions & 1 deletion packages/apollo-graphql/src/schema/buildSchemaFromSDL.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import {
SchemaDefinitionNode,
SchemaExtensionNode,
OperationTypeNode,
GraphQLObjectType
GraphQLObjectType,
isAbstractType
} from "graphql";
import { validateSDL } from "graphql/validation/validate";
import { isDocumentNode, isNode } from "../utilities/graphql";
Expand Down Expand Up @@ -206,6 +207,15 @@ export function addResolversToSchema(
) {
for (const [typeName, fieldConfigs] of Object.entries(resolvers)) {
const type = schema.getType(typeName);

if (isAbstractType(type)) {
for (const [fieldName, fieldConfig] of Object.entries(fieldConfigs)) {
if (fieldName.startsWith("__")) {
(type as any)[fieldName.substring(2)] = fieldConfig;
}
}
}

if (!isObjectType(type)) continue;

const fieldMap = type.getFields();
Expand Down

1 comment on commit 25838b6

@Pruxis
Copy link

@Pruxis Pruxis commented on 25838b6 Jun 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @trevor-scheer what package version is this? So I can bump my dependency in federation of apollo-graphql

Please sign in to comment.