graphile-build
Version:
Build a GraphQL schema from plugins
84 lines (83 loc) • 2.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _language = require("graphql/language");
var StandardTypesPlugin = function StandardTypesPlugin(builder) {
// XXX: this should be in an "init" plugin, but PgTypesPlugin requires it in build - fix that, then fix this
builder.hook("build", build => {
const stringType = (name, description) => new build.graphql.GraphQLScalarType({
name,
description,
serialize: value => String(value),
parseValue: value => String(value),
parseLiteral: ast => {
if (ast.kind !== _language.Kind.STRING) {
throw new Error("Can only parse string values");
}
return ast.value;
}
});
const Cursor = stringType("Cursor", "A location in a connection that can be used for resuming pagination.");
build.addType(Cursor, "graphile-build built-in");
return build;
}, ["StandardTypes"]);
builder.hook("init", (_, build) => {
const {
newWithHooks,
graphql: {
GraphQLNonNull,
GraphQLObjectType,
GraphQLBoolean
},
inflection
} = build;
// https://facebook.github.io/relay/graphql/connections.htm#sec-undefined.PageInfo
/* const PageInfo = */
newWithHooks(GraphQLObjectType, {
name: inflection.builtin("PageInfo"),
description: build.wrapDescription("Information about pagination in a connection.", "type"),
fields: ({
fieldWithHooks
}) => ({
hasNextPage: fieldWithHooks("hasNextPage", ({
addDataGenerator
}) => {
addDataGenerator(() => {
return {
calculateHasNextPage: true
};
});
return {
description: build.wrapDescription("When paginating forwards, are there more items?", "field"),
type: new GraphQLNonNull(GraphQLBoolean)
};
}, {
isPageInfoHasNextPageField: true
}),
hasPreviousPage: fieldWithHooks("hasPreviousPage", ({
addDataGenerator
}) => {
addDataGenerator(() => {
return {
calculateHasPreviousPage: true
};
});
return {
description: build.wrapDescription("When paginating backwards, are there more items?", "field"),
type: new GraphQLNonNull(GraphQLBoolean)
};
}, {
isPageInfoHasPreviousPageField: true
})
})
}, {
__origin: `graphile-build built-in`,
isPageInfo: true
});
return _;
}, ["StandardTypes", "PageInfo"]);
};
exports.default = StandardTypesPlugin;
//# sourceMappingURL=StandardTypesPlugin.js.map